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