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