1 /* 2 * Copyright (c) 2012-2014, 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 26 package com.sun.tools.sjavac; 27 28 import java.io.PrintWriter; 29 import java.util.Arrays; 30 import java.util.List; 31 import java.util.LinkedList; 32 import java.util.Set; 33 import com.sun.tools.javac.file.ZipArchive.ZipFileObject; 34 import com.sun.tools.javac.file.ZipFileIndexArchive.ZipFileIndexFileObject; 35 import com.sun.tools.sjavac.options.Options; 36 import com.sun.tools.sjavac.comp.JavaCompilerWithDeps; 37 38 public class Compile { 39 // Setup a compiler context for finding classes in the classpath 40 // and to execute annotation processors. 41 com.sun.tools.javac.util.Context context; 42 javax.tools.JavaCompiler.CompilationTask task; 43 44 /** 45 * Setup a compilation context, used for reading public apis of classes on the classpath 46 * as well as annotation processors. 47 */ 48 public Compile(Options options) { 49 com.sun.tools.javac.api.JavacTool compiler = com.sun.tools.javac.api.JavacTool.create(); 50 javax.tools.StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); 51 context = new com.sun.tools.javac.util.Context(); 52 String[] args = options.prepJavacArgs(); 53 task = compiler.getTask(new PrintWriter(System.err), 54 fileManager, 55 null, Arrays.asList(args), 56 null, null, context); 57 // Trigger a creation of the JavaCompiler, necessary to get a sourceCompleter for ClassFinder. 58 // The sourceCompleter is used for build situations where a classpath class references other classes 59 // that happens to be on the sourcepath. 60 com.sun.tools.javac.main.JavaCompiler.instance(context); 61 } 62 63 public String getClassLocInfo(String fn) { 64 try { 65 com.sun.tools.javac.code.ClassFinder cr = com.sun.tools.javac.code.ClassFinder.instance(context); 66 com.sun.tools.javac.util.Names ns = com.sun.tools.javac.util.Names.instance(context); 67 com.sun.tools.javac.util.Name n = ns.fromString(fn); 68 com.sun.tools.javac.code.Symbol.ClassSymbol cs = cr.loadClass(n); 69 return cs.classfile.toString()+" "+cs.classfile.getLastModified(); 70 } catch (Exception e) { 71 e.printStackTrace(System.err); 72 } 73 return ""; 74 } 75 76 /** 77 * Extract the pubapi for a class fn. If the class was fetched from an archive, 78 * store the archive in the set archives. 79 */ 80 public List<String> getPubapi(String fn, Set<String> archives) { 81 try { 82 com.sun.tools.javac.code.ClassFinder cr = com.sun.tools.javac.code.ClassFinder.instance(context); 83 com.sun.tools.javac.util.Names ns = com.sun.tools.javac.util.Names.instance(context); 84 com.sun.tools.javac.util.Name n = ns.fromString(fn); 85 com.sun.tools.javac.code.Symbol.ClassSymbol cs = cr.loadClass(n); 86 if (cs.classfile instanceof ZipFileIndexFileObject) { 87 String s = cs.classfile.toString(); 88 int p = s.indexOf('['); 89 int pp = s.indexOf('(', p+1); 90 archives.add(s.substring(p+1,pp)); 91 } 92 93 List<String> api = new LinkedList<>(); 94 if (cs.classfile != null) { 95 api = com.sun.tools.sjavac.comp.Dependencies.constructPubapi(cs, 96 cs.classfile.toString()+" "+cs.classfile.getLastModified()); 97 } 98 else { 99 System.err.println("Classfile is null for "+fn); 100 } 101 return api; 102 } catch (Exception e) { 103 e.printStackTrace(System.err); 104 } 105 return new LinkedList<String>(); 106 } 107 }