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