PubApiExtractor.java revision 3294:9adfb22ff08f
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 26package com.sun.tools.sjavac; 27 28import java.io.IOException; 29import java.io.PrintWriter; 30import java.util.Arrays; 31 32import javax.tools.JavaCompiler.CompilationTask; 33import javax.tools.JavaFileManager; 34 35import com.sun.tools.javac.api.JavacTool; 36import com.sun.tools.javac.code.ClassFinder; 37import com.sun.tools.javac.code.Symbol.ClassSymbol; 38import com.sun.tools.javac.code.Symtab; 39import com.sun.tools.javac.main.JavaCompiler; 40import com.sun.tools.javac.util.Context; 41import com.sun.tools.javac.util.Convert; 42import com.sun.tools.javac.util.Name; 43import com.sun.tools.javac.util.Names; 44import com.sun.tools.sjavac.comp.PubapiVisitor; 45import com.sun.tools.sjavac.comp.SmartFileManager; 46import com.sun.tools.sjavac.options.Options; 47import com.sun.tools.sjavac.pubapi.PubApi; 48 49public class PubApiExtractor { 50 // Setup a compiler context for finding classes in the classpath 51 // and to execute annotation processors. 52 final Context context; 53 final CompilationTask task; 54 55 final SmartFileManager fileManager; 56 57 /** 58 * Setup a compilation context, used for reading public apis of classes on the classpath 59 * as well as annotation processors. 60 */ 61 public PubApiExtractor(Options options) { 62 JavacTool compiler = com.sun.tools.javac.api.JavacTool.create(); 63 fileManager = new SmartFileManager(compiler.getStandardFileManager(null, null, null)); 64 context = new com.sun.tools.javac.util.Context(); 65 String[] args = options.prepJavacArgs(); 66 task = compiler.getTask(new PrintWriter(System.err), 67 fileManager, 68 null, 69 Arrays.asList(args), 70 null, 71 null, 72 context); 73 // Trigger a creation of the JavaCompiler, necessary to get a sourceCompleter for ClassFinder. 74 // The sourceCompleter is used for build situations where a classpath class references other classes 75 // that happens to be on the sourcepath. 76 JavaCompiler.instance(context); 77 78// context.put(JavaFileManager.class, fileManager); 79 } 80 81 public PubApi getPubApi(String fullyQualifiedClassName) { 82 Symtab syms = Symtab.instance(context); 83 ClassFinder cr = ClassFinder.instance(context); 84 Names ns = Names.instance(context); 85 Name n = ns.fromString(fullyQualifiedClassName); 86 ClassSymbol cs = cr.loadClass(syms.inferModule(Convert.packagePart(n)), n); 87 PubapiVisitor v = new PubapiVisitor(); 88 v.visit(cs); 89 return v.getCollectedPubApi(); 90 } 91 92 public void close() throws IOException { 93 fileManager.close(); 94 } 95} 96