1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.tools.sjavac.comp;
  26 
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 import java.util.HashSet;
  30 import java.util.Iterator;
  31 import java.util.Set;
  32 
  33 import javax.tools.JavaFileObject;
  34 
  35 import com.sun.source.tree.CompilationUnitTree;
  36 import com.sun.source.util.TaskEvent;
  37 import com.sun.source.util.TaskListener;
  38 import com.sun.tools.javac.tree.JCTree;
  39 import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
  40 import com.sun.tools.javac.tree.JCTree.JCIdent;
  41 import com.sun.tools.javac.util.DefinedBy;
  42 import com.sun.tools.javac.util.DefinedBy.Api;
  43 import com.sun.tools.javac.util.Name;
  44 import com.sun.tools.sjavac.Log;
  45 
  46 public class PathAndPackageVerifier implements TaskListener {
  47 
  48     // Stores the set of compilation units whose source file path does not
  49     // match the package declaration.
  50     Set<CompilationUnitTree> misplacedCompilationUnits = new HashSet<>();
  51 
  52     @Override
  53     @DefinedBy(Api.COMPILER_TREE)
  54     public void finished(TaskEvent e) {
  55         if (e.getKind() == TaskEvent.Kind.ANALYZE) {
  56             
  57             
  58             CompilationUnitTree cu = e.getCompilationUnit();
  59             if (cu == null)
  60                 return;
  61             
  62             JavaFileObject jfo = cu.getSourceFile();
  63             if (jfo == null)
  64                 return; // No source file -> package doesn't matter
  65             
  66             JCTree pkg = (JCTree) cu.getPackageName();
  67             if (pkg == null)
  68                 return; // Default package. See JDK-8048144.
  69             
  70             Path dir = Paths.get(jfo.toUri()).normalize().getParent();
  71             if (!checkPathAndPackage(dir, pkg))
  72                 misplacedCompilationUnits.add(cu);
  73         }
  74         
  75         if (e.getKind() == TaskEvent.Kind.COMPILATION) {
  76             
  77             for (CompilationUnitTree cu : misplacedCompilationUnits) {
  78                 Log.error("Misplaced compilation unit.");
  79                 Log.error("    Directory: " + Paths.get(cu.getSourceFile().toUri()).getParent());
  80                 Log.error("    Package:   " + cu.getPackageName());
  81             }
  82         }
  83     }
  84     
  85     public boolean errorsDiscovered() {
  86         return misplacedCompilationUnits.size() > 0;
  87     }
  88 
  89     /* Returns true if dir matches pkgName.
  90      *
  91      * Examples:
  92      *     (a/b/c, a.b.c) gives true
  93      *     (i/j/k, i.x.k) gives false
  94      *
  95      * Currently (x/a/b/c, a.b.c) also gives true. See JDK-8059598.
  96      */
  97     private boolean checkPathAndPackage(Path dir, JCTree pkgName) {
  98         Iterator<String> pathIter = new ParentIterator(dir);
  99         Iterator<String> pkgIter = new EnclosingPkgIterator(pkgName);
 100         while (pathIter.hasNext() && pkgIter.hasNext()) {
 101             if (!pathIter.next().equals(pkgIter.next()))
 102                 return false;
 103         }
 104         return !pkgIter.hasNext(); /*&& !pathIter.hasNext() See JDK-8059598 */
 105     }
 106 
 107     /* Iterates over the names of the parents of the given path:
 108      * Example: dir1/dir2/dir3  results in  dir3 -> dir2 -> dir1
 109      */
 110     private static class ParentIterator implements Iterator<String> {
 111         Path next;
 112         ParentIterator(Path initial) {
 113             next = initial;
 114         }
 115         @Override
 116         public boolean hasNext() {
 117             return next != null;
 118         }
 119         @Override
 120         public String next() {
 121             String tmp = next.getFileName().toString();
 122             next = next.getParent();
 123             return tmp;
 124         }
 125     }
 126 
 127     /* Iterates over the names of the enclosing packages:
 128      * Example: pkg1.pkg2.pkg3  results in  pkg3 -> pkg2 -> pkg1
 129      */
 130     private static class EnclosingPkgIterator implements Iterator<String> {
 131         JCTree next;
 132         EnclosingPkgIterator(JCTree initial) {
 133             next = initial;
 134         }
 135         @Override
 136         public boolean hasNext() {
 137             return next != null;
 138         }
 139         @Override
 140         public String next() {
 141             Name name;
 142             if (next instanceof JCIdent) {
 143                 name = ((JCIdent) next).name;
 144                 next = null;
 145             } else {
 146                 JCFieldAccess fa = (JCFieldAccess) next;
 147                 name = fa.name;
 148                 next = fa.selected;
 149             }
 150             return name.toString();
 151         }
 152     }
 153 }