src/share/classes/com/sun/tools/sjavac/comp/JavaCompilerWithDeps.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2227
998b10c43157
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 package com.sun.tools.sjavac.comp;
aoqi@0 26
aoqi@0 27 import java.util.StringTokenizer;
aoqi@0 28
aoqi@0 29 import com.sun.tools.javac.main.JavaCompiler;
aoqi@0 30 import com.sun.tools.javac.util.Context;
aoqi@0 31 import com.sun.tools.javac.code.Symbol.ClassSymbol;
aoqi@0 32 import com.sun.tools.sjavac.server.CompilerThread;
aoqi@0 33 import java.io.File;
aoqi@0 34
aoqi@0 35 /** Subclass to Resolve that overrides collect.
aoqi@0 36 *
aoqi@0 37 * <p><b>This is NOT part of any supported API.
aoqi@0 38 * If you write code that depends on this, you do so at your own
aoqi@0 39 * risk. This code and its internal interfaces are subject to change
aoqi@0 40 * or deletion without notice.</b></p>
aoqi@0 41 */
aoqi@0 42 public class JavaCompilerWithDeps extends JavaCompiler {
aoqi@0 43
aoqi@0 44 /** The dependency database
aoqi@0 45 */
aoqi@0 46 protected Dependencies deps;
aoqi@0 47 protected CompilerThread compilerThread;
aoqi@0 48
aoqi@0 49 public JavaCompilerWithDeps(Context context, CompilerThread t) {
aoqi@0 50 super(context);
aoqi@0 51 deps = Dependencies.instance(context);
aoqi@0 52 compilerThread = t;
aoqi@0 53 needRootClasses = true;
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 public static void preRegister(Context context, final CompilerThread t) {
aoqi@0 57 context.put(compilerKey, new Context.Factory<JavaCompiler>() {
aoqi@0 58 public JavaCompiler make(Context c) {
aoqi@0 59 JavaCompiler instance = new JavaCompilerWithDeps(c, t);
aoqi@0 60 c.put(JavaCompiler.class, instance);
aoqi@0 61 return instance;
aoqi@0 62 }
aoqi@0 63 });
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 /** Collect the public apis of classes supplied explicitly for compilation.
aoqi@0 67 * @param sym The class to visit.
aoqi@0 68 */
aoqi@0 69 @Override
aoqi@0 70 public void reportPublicApi(ClassSymbol sym) {
aoqi@0 71 // The next test will catch when source files are located in the wrong directory!
aoqi@0 72 // This ought to be moved into javac as a new warning, or perhaps as part
aoqi@0 73 // of the auxiliary class warning.
aoqi@0 74
aoqi@0 75 // For example if sun.swing.BeanInfoUtils
aoqi@0 76 // is in fact stored in: /mybuild/jdk/gensrc/javax/swing/beaninfo/BeanInfoUtils.java
aoqi@0 77
aoqi@0 78 // We do not need to test that BeanInfoUtils is stored in a file named BeanInfoUtils
aoqi@0 79 // since this is checked earlier.
aoqi@0 80 if (sym.sourcefile != null) {
aoqi@0 81 // Rewrite sun.swing.BeanInfoUtils into /sun/swing/
aoqi@0 82 StringBuilder pathb = new StringBuilder();
aoqi@0 83 StringTokenizer qn = new StringTokenizer(sym.packge().toString(), ".");
aoqi@0 84 boolean first = true;
aoqi@0 85 while (qn.hasMoreTokens()) {
aoqi@0 86 String o = qn.nextToken();
aoqi@0 87 pathb.append("/");
aoqi@0 88 pathb.append(o);
aoqi@0 89 first = false;
aoqi@0 90 }
aoqi@0 91 pathb.append("/");
aoqi@0 92 String path = pathb.toString();
aoqi@0 93
aoqi@0 94 // Now cut the uri to be: file:///mybuild/jdk/gensrc/javax/swing/beaninfo/
aoqi@0 95 String p = sym.sourcefile.toUri().getPath();
aoqi@0 96 // Do not use File.separatorChar here, a URI always uses slashes /.
aoqi@0 97 int i = p.lastIndexOf("/");
aoqi@0 98 String pp = p.substring(0,i+1);
aoqi@0 99
aoqi@0 100 // Now check if the truncated uri ends with the path. (It does not == failure!)
aoqi@0 101 if (path.length() > 0 && !path.equals("/unnamed package/") && !pp.endsWith(path)) {
aoqi@0 102 compilerThread.logError("Error: The source file "+sym.sourcefile.getName()+
aoqi@0 103 " is located in the wrong package directory, because it contains the class "+
aoqi@0 104 sym.getQualifiedName());
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107 deps.visitPubapi(sym);
aoqi@0 108 }
aoqi@0 109 }

mercurial