langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/PubApiExtractor.java
author vasya
Mon, 14 Dec 2015 20:18:19 +0100
changeset 34752 9c262a013456
parent 33553 9a742b6b95d3
child 36526 3b41f1c69604
permissions -rw-r--r--
8145342: Some copyright notices are inconsistently and ill formatted Summary: Adjusted formatting of copyright notices. Reviewed-by: darcy, alundblad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     1
/*
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 33553
diff changeset
     2
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     4
 *
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    10
 *
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    15
 * accompanied this code).
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    16
 *
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    20
 *
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    23
 * questions.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    24
 */
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    25
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    26
package com.sun.tools.sjavac;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    27
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    28
import java.io.IOException;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    29
import java.io.PrintWriter;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    30
import java.util.Arrays;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    31
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    32
import javax.tools.JavaCompiler.CompilationTask;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    33
import javax.tools.JavaFileManager;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    34
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTool;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    36
import com.sun.tools.javac.code.ClassFinder;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    37
import com.sun.tools.javac.code.Symbol.ClassSymbol;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    38
import com.sun.tools.javac.main.JavaCompiler;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    39
import com.sun.tools.javac.util.Context;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    40
import com.sun.tools.javac.util.Name;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    41
import com.sun.tools.javac.util.Names;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    42
import com.sun.tools.sjavac.comp.PubapiVisitor;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    43
import com.sun.tools.sjavac.comp.SmartFileManager;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    44
import com.sun.tools.sjavac.options.Options;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    45
import com.sun.tools.sjavac.pubapi.PubApi;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    46
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    47
public class PubApiExtractor {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    48
    // Setup a compiler context for finding classes in the classpath
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    49
    // and to execute annotation processors.
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    50
    final Context context;
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    51
    final CompilationTask task;
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    52
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    53
    final SmartFileManager fileManager;
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    54
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    55
    /**
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    56
     * Setup a compilation context, used for reading public apis of classes on the classpath
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    57
     * as well as annotation processors.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    58
     */
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    59
    public PubApiExtractor(Options options) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    60
        JavacTool compiler = com.sun.tools.javac.api.JavacTool.create();
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    61
        fileManager = new SmartFileManager(compiler.getStandardFileManager(null, null, null));
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    62
        context = new com.sun.tools.javac.util.Context();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    63
        String[] args = options.prepJavacArgs();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    64
        task = compiler.getTask(new PrintWriter(System.err),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    65
                                fileManager,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    66
                                null,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    67
                                Arrays.asList(args),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    68
                                null,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    69
                                null,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    70
                                context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    71
        // Trigger a creation of the JavaCompiler, necessary to get a sourceCompleter for ClassFinder.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    72
        // The sourceCompleter is used for build situations where a classpath class references other classes
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    73
        // that happens to be on the sourcepath.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    74
        JavaCompiler.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    75
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    76
//        context.put(JavaFileManager.class, fileManager);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    77
    }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    78
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    79
    public PubApi getPubApi(String fullyQualifiedClassName) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    80
        ClassFinder cr = ClassFinder.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    81
        Names ns = Names.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    82
        Name n = ns.fromString(fullyQualifiedClassName);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    83
        ClassSymbol cs = cr.loadClass(n);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    84
        PubapiVisitor v = new PubapiVisitor();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    85
        v.visit(cs);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    86
        return v.getCollectedPubApi();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    87
    }
33553
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    88
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    89
    public void close() throws IOException {
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    90
        fileManager.close();
9a742b6b95d3 8137075: Sjavac tests are leaking file managers
alundblad
parents: 31115
diff changeset
    91
    }
31115
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    92
}