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

Thu, 15 Aug 2013 17:24:35 +0200

author
erikj
date
Thu, 15 Aug 2013 17:24:35 +0200
changeset 1953
71b0089b146f
parent 1504
22e417cdddee
child 2227
998b10c43157
permissions
-rw-r--r--

8015145: Smartjavac needs more flexibility with linking to sources
Reviewed-by: jjg, ohrstrom

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

mercurial