--- /dev/null 2014-08-08 09:27:00.413288948 +0200 +++ new/src/share/classes/com/sun/tools/sjavac/Compile.java 2014-08-09 00:28:51.583745882 +0200 @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2012-2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.sun.tools.sjavac; + +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.List; +import java.util.LinkedList; +import java.util.Set; +import com.sun.tools.javac.file.ZipArchive.ZipFileObject; +import com.sun.tools.javac.file.ZipFileIndexArchive.ZipFileIndexFileObject; +import com.sun.tools.sjavac.options.Options; +import com.sun.tools.sjavac.comp.JavaCompilerWithDeps; + +public class Compile { + // Setup a compiler context for finding classes in the classpath + // and to execute annotation processors. + com.sun.tools.javac.util.Context context; + javax.tools.JavaCompiler.CompilationTask task; + + /** + * Setup a compilation context, used for reading public apis of classes on the classpath + * as well as annotation processors. + */ + public Compile(Options options) { + com.sun.tools.javac.api.JavacTool compiler = com.sun.tools.javac.api.JavacTool.create(); + javax.tools.StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); + context = new com.sun.tools.javac.util.Context(); + String[] args = options.prepJavacArgs(); + task = compiler.getTask(new PrintWriter(System.err), + fileManager, + null, Arrays.asList(args), + null, null, context); + // Trigger a creation of the JavaCompiler, necessary to get a sourceCompleter for ClassFinder. + // The sourceCompleter is used for build situations where a classpath class references other classes + // that happens to be on the sourcepath. + com.sun.tools.javac.main.JavaCompiler.instance(context); + } + + public String getClassLocInfo(String fn) { + try { + com.sun.tools.javac.code.ClassFinder cr = com.sun.tools.javac.code.ClassFinder.instance(context); + com.sun.tools.javac.util.Names ns = com.sun.tools.javac.util.Names.instance(context); + com.sun.tools.javac.util.Name n = ns.fromString(fn); + com.sun.tools.javac.code.Symbol.ClassSymbol cs = cr.loadClass(n); + return cs.classfile.toString()+" "+cs.classfile.getLastModified(); + } catch (Exception e) { + e.printStackTrace(System.err); + } + return ""; + } + + /** + * Extract the pubapi for a class fn. If the class was fetched from an archive, + * store the archive in the set archives. + */ + public List getPubapi(String fn, Set archives) { + try { + com.sun.tools.javac.code.ClassFinder cr = com.sun.tools.javac.code.ClassFinder.instance(context); + com.sun.tools.javac.util.Names ns = com.sun.tools.javac.util.Names.instance(context); + com.sun.tools.javac.util.Name n = ns.fromString(fn); + com.sun.tools.javac.code.Symbol.ClassSymbol cs = cr.loadClass(n); + if (cs.classfile instanceof ZipFileIndexFileObject) { + String s = cs.classfile.toString(); + int p = s.indexOf('['); + int pp = s.indexOf('(', p+1); + archives.add(s.substring(p+1,pp)); + } + + List api = new LinkedList<>(); + if (cs.classfile != null) { + api = com.sun.tools.sjavac.comp.Dependencies.constructPubapi(cs, + cs.classfile.toString()+" "+cs.classfile.getLastModified()); + } + else { + System.err.println("Classfile is null for "+fn); + } + return api; + } catch (Exception e) { + e.printStackTrace(System.err); + } + return new LinkedList(); + } +} \ No newline at end of file