langtools/src/jdk.compiler/share/classes/com/sun/tools/sjavac/PubApiExtractor.java
author alundblad
Tue, 09 Jun 2015 15:57:45 +0200
changeset 31115 8d8e98052d5d
child 33553 9a742b6b95d3
permissions -rw-r--r--
8054717: SJavac should track changes in the public apis of classpath classes! Summary: Added functionality for tracking changes in public APIs of classpath classes. Reviewed-by: jlahoda, erikj
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
/*
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2012-2014, Oracle and/or its affiliates. All rights reserved.
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
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    28
import java.io.PrintWriter;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    29
import java.util.Arrays;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    30
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    31
import javax.tools.JavaCompiler.CompilationTask;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    32
import javax.tools.JavaFileManager;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    33
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    34
import com.sun.tools.javac.api.JavacTool;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    35
import com.sun.tools.javac.code.ClassFinder;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    36
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
    37
import com.sun.tools.javac.main.JavaCompiler;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    38
import com.sun.tools.javac.util.Context;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    39
import com.sun.tools.javac.util.Name;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    40
import com.sun.tools.javac.util.Names;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    41
import com.sun.tools.sjavac.comp.PubapiVisitor;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    42
import com.sun.tools.sjavac.comp.SmartFileManager;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    43
import com.sun.tools.sjavac.options.Options;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    44
import com.sun.tools.sjavac.pubapi.PubApi;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    45
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    46
public class PubApiExtractor {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    47
    // 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
    48
    // and to execute annotation processors.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    49
    Context context;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    50
    CompilationTask task;
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    51
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    52
    /**
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    53
     * 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
    54
     * as well as annotation processors.
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
    public PubApiExtractor(Options options) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    57
        JavacTool compiler = com.sun.tools.javac.api.JavacTool.create();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    58
        SmartFileManager fileManager = new SmartFileManager(compiler.getStandardFileManager(null, null, null));
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    59
        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
    60
        String[] args = options.prepJavacArgs();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    61
        task = compiler.getTask(new PrintWriter(System.err),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    62
                                fileManager,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    63
                                null,
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    64
                                Arrays.asList(args),
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    65
                                null,
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
                                context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    68
        // 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
    69
        // 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
    70
        // that happens to be on the sourcepath.
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    71
        JavaCompiler.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    72
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    73
//        context.put(JavaFileManager.class, fileManager);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    74
    }
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
    public PubApi getPubApi(String fullyQualifiedClassName) {
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    77
        ClassFinder cr = ClassFinder.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    78
        Names ns = Names.instance(context);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    79
        Name n = ns.fromString(fullyQualifiedClassName);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    80
        ClassSymbol cs = cr.loadClass(n);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    81
        PubapiVisitor v = new PubapiVisitor();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    82
        v.visit(cs);
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    83
        return v.getCollectedPubApi();
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    84
    }
8d8e98052d5d 8054717: SJavac should track changes in the public apis of classpath classes!
alundblad
parents:
diff changeset
    85
}