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

changeset 1570
f91144b7da75
parent 1504
22e417cdddee
child 2227
998b10c43157
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/sjavac/comp/JavaCompilerWithDeps.java	Mon Feb 04 18:08:53 2013 -0500
     1.3 @@ -0,0 +1,109 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package com.sun.tools.sjavac.comp;
    1.29 +
    1.30 +import java.util.StringTokenizer;
    1.31 +
    1.32 +import com.sun.tools.javac.main.JavaCompiler;
    1.33 +import com.sun.tools.javac.util.Context;
    1.34 +import com.sun.tools.javac.code.Symbol.ClassSymbol;
    1.35 +import com.sun.tools.sjavac.server.CompilerThread;
    1.36 +import java.io.File;
    1.37 +
    1.38 +/** Subclass to Resolve that overrides collect.
    1.39 + *
    1.40 + * <p><b>This is NOT part of any supported API.
    1.41 + * If you write code that depends on this, you do so at your own
    1.42 + * risk.  This code and its internal interfaces are subject to change
    1.43 + * or deletion without notice.</b></p>
    1.44 + */
    1.45 +public class JavaCompilerWithDeps extends JavaCompiler {
    1.46 +
    1.47 +    /** The dependency database
    1.48 +     */
    1.49 +    protected Dependencies deps;
    1.50 +    protected CompilerThread compilerThread;
    1.51 +
    1.52 +    public JavaCompilerWithDeps(Context context, CompilerThread t) {
    1.53 +        super(context);
    1.54 +        deps = Dependencies.instance(context);
    1.55 +        compilerThread = t;
    1.56 +        needRootClasses = true;
    1.57 +    }
    1.58 +
    1.59 +    public static void preRegister(Context context, final CompilerThread t) {
    1.60 +        context.put(compilerKey, new Context.Factory<JavaCompiler>() {
    1.61 +            public JavaCompiler make(Context c) {
    1.62 +                JavaCompiler instance = new JavaCompilerWithDeps(c, t);
    1.63 +                c.put(JavaCompiler.class, instance);
    1.64 +                return instance;
    1.65 +            }
    1.66 +        });
    1.67 +    }
    1.68 +
    1.69 +    /** Collect the public apis of classes supplied explicitly for compilation.
    1.70 +     * @param sym The class to visit.
    1.71 +     */
    1.72 +    @Override
    1.73 +    public void reportPublicApi(ClassSymbol sym) {
    1.74 +        // The next test will catch when source files are located in the wrong directory!
    1.75 +        // This ought to be moved into javac as a new warning, or perhaps as part
    1.76 +        // of the auxiliary class warning.
    1.77 +
    1.78 +        // For example if sun.swing.BeanInfoUtils
    1.79 +        // is in fact stored in: /mybuild/jdk/gensrc/javax/swing/beaninfo/BeanInfoUtils.java
    1.80 +
    1.81 +        // We do not need to test that BeanInfoUtils is stored in a file named BeanInfoUtils
    1.82 +        // since this is checked earlier.
    1.83 +        if (sym.sourcefile != null) {
    1.84 +            // Rewrite sun.swing.BeanInfoUtils into /sun/swing/
    1.85 +            StringBuilder pathb = new StringBuilder();
    1.86 +            StringTokenizer qn = new StringTokenizer(sym.packge().toString(), ".");
    1.87 +            boolean first = true;
    1.88 +            while (qn.hasMoreTokens()) {
    1.89 +                String o = qn.nextToken();
    1.90 +                pathb.append("/");
    1.91 +                pathb.append(o);
    1.92 +                first = false;
    1.93 +            }
    1.94 +            pathb.append("/");
    1.95 +            String path = pathb.toString();
    1.96 +
    1.97 +            // Now cut the uri to be: file:///mybuild/jdk/gensrc/javax/swing/beaninfo/
    1.98 +            String p = sym.sourcefile.toUri().getPath();
    1.99 +            // Do not use File.separatorChar here, a URI always uses slashes /.
   1.100 +            int i = p.lastIndexOf("/");
   1.101 +            String pp = p.substring(0,i+1);
   1.102 +
   1.103 +            // Now check if the truncated uri ends with the path. (It does not == failure!)
   1.104 +            if (path.length() > 0 && !path.equals("/unnamed package/") && !pp.endsWith(path)) {
   1.105 +                compilerThread.logError("Error: The source file "+sym.sourcefile.getName()+
   1.106 +                                        " is located in the wrong package directory, because it contains the class "+
   1.107 +                                        sym.getQualifiedName());
   1.108 +            }
   1.109 +        }
   1.110 +        deps.visitPubapi(sym);
   1.111 +    }
   1.112 +}

mercurial