langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/JavaCompilerWithDeps.java
changeset 26992 92e69fa21956
parent 26991 88d998b3bb4b
child 26993 513b2cae81c3
equal deleted inserted replaced
26991:88d998b3bb4b 26992:92e69fa21956
     1 /*
       
     2  * Copyright (c) 2012, 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.util.StringTokenizer;
       
    28 
       
    29 import com.sun.tools.javac.main.JavaCompiler;
       
    30 import com.sun.tools.javac.util.Context;
       
    31 import com.sun.tools.javac.code.Symbol.ClassSymbol;
       
    32 
       
    33 /** Subclass to Resolve that overrides collect.
       
    34  *
       
    35  *  <p><b>This is NOT part of any supported API.
       
    36  *  If you write code that depends on this, you do so at your own risk.
       
    37  *  This code and its internal interfaces are subject to change or
       
    38  *  deletion without notice.</b>
       
    39  */
       
    40 public class JavaCompilerWithDeps extends JavaCompiler {
       
    41 
       
    42     /** The dependency database
       
    43      */
       
    44     protected Dependencies deps;
       
    45     protected SjavacErrorHandler errorHandler;
       
    46 
       
    47     public JavaCompilerWithDeps(Context context, SjavacErrorHandler eh) {
       
    48         super(context);
       
    49         deps = Dependencies.instance(context);
       
    50         errorHandler = eh;
       
    51         needRootClasses = true;
       
    52     }
       
    53 
       
    54     public static void preRegister(Context context, final SjavacErrorHandler eh) {
       
    55         context.put(compilerKey, new Context.Factory<JavaCompiler>() {
       
    56             public JavaCompiler make(Context c) {
       
    57                 JavaCompiler instance = new JavaCompilerWithDeps(c, eh);
       
    58                 c.put(JavaCompiler.class, instance);
       
    59                 return instance;
       
    60             }
       
    61         });
       
    62     }
       
    63 
       
    64     /** Collect the public apis of classes supplied explicitly for compilation.
       
    65      * @param sym The class to visit.
       
    66      */
       
    67     @Override
       
    68     public void reportPublicApi(ClassSymbol sym) {
       
    69         // The next test will catch when source files are located in the wrong directory!
       
    70         // This ought to be moved into javac as a new warning, or perhaps as part
       
    71         // of the auxiliary class warning.
       
    72 
       
    73         // For example if sun.swing.BeanInfoUtils
       
    74         // is in fact stored in: /mybuild/jdk/gensrc/javax/swing/beaninfo/BeanInfoUtils.java
       
    75 
       
    76         // We do not need to test that BeanInfoUtils is stored in a file named BeanInfoUtils
       
    77         // since this is checked earlier.
       
    78         if (sym.sourcefile != null) {
       
    79             // Rewrite sun.swing.BeanInfoUtils into /sun/swing/
       
    80             StringBuilder pathb = new StringBuilder();
       
    81             StringTokenizer qn = new StringTokenizer(sym.packge().toString(), ".");
       
    82             boolean first = true;
       
    83             while (qn.hasMoreTokens()) {
       
    84                 String o = qn.nextToken();
       
    85                 pathb.append("/");
       
    86                 pathb.append(o);
       
    87                 first = false;
       
    88             }
       
    89             pathb.append("/");
       
    90             String path = pathb.toString();
       
    91 
       
    92             // Now cut the uri to be: file:///mybuild/jdk/gensrc/javax/swing/beaninfo/
       
    93             String p = sym.sourcefile.toUri().getPath();
       
    94             // Do not use File.separatorChar here, a URI always uses slashes /.
       
    95             int i = p.lastIndexOf("/");
       
    96             String pp = p.substring(0,i+1);
       
    97 
       
    98             // Now check if the truncated uri ends with the path. (It does not == failure!)
       
    99             if (path.length() > 0 && !path.equals("/unnamed package/") && !pp.endsWith(path)) {
       
   100                 errorHandler.logError("Error: The source file "+sym.sourcefile.getName()+
       
   101                                         " is located in the wrong package directory, because it contains the class "+
       
   102                                         sym.getQualifiedName());
       
   103             }
       
   104         }
       
   105         deps.visitPubapi(sym);
       
   106     }
       
   107 }