duke@1: /*
jfranck@1313: * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.sym;
duke@1:
duke@1: import com.sun.tools.javac.api.JavacTaskImpl;
duke@1: import com.sun.tools.javac.code.Kinds;
duke@1: import com.sun.tools.javac.code.Scope;
duke@1: import com.sun.tools.javac.code.Symbol.*;
duke@1: import com.sun.tools.javac.code.Symbol;
duke@1: import com.sun.tools.javac.code.Attribute;
duke@1: import com.sun.tools.javac.code.Symtab;
duke@1: import com.sun.tools.javac.code.Type;
vromero@1452: import com.sun.tools.javac.code.Types;
duke@1: import com.sun.tools.javac.jvm.ClassWriter;
duke@1: import com.sun.tools.javac.jvm.Pool;
duke@1: import com.sun.tools.javac.processing.JavacProcessingEnvironment;
duke@1: import com.sun.tools.javac.util.List;
jjg@1569: import com.sun.tools.javac.util.Names;
duke@1: import com.sun.tools.javac.util.Pair;
duke@1:
duke@1: import java.io.File;
duke@1: import java.io.IOException;
duke@1: import java.util.ArrayList;
duke@1: import java.util.EnumSet;
duke@1: import java.util.Enumeration;
duke@1: import java.util.HashSet;
jjg@1569: import java.util.Map;
duke@1: import java.util.ResourceBundle;
duke@1: import java.util.Set;
duke@1:
duke@1: import javax.annotation.processing.AbstractProcessor;
duke@1: import javax.annotation.processing.RoundEnvironment;
duke@1: import javax.annotation.processing.SupportedAnnotationTypes;
duke@1: import javax.annotation.processing.SupportedOptions;
duke@1: import javax.lang.model.SourceVersion;
duke@1: import javax.lang.model.element.ElementKind;
duke@1: import javax.lang.model.element.TypeElement;
duke@1: import javax.tools.Diagnostic;
duke@1: import javax.tools.JavaCompiler;
duke@1: import javax.tools.JavaFileManager.Location;
duke@1: import javax.tools.JavaFileObject;
duke@1: import static javax.tools.JavaFileObject.Kind.CLASS;
duke@1: import javax.tools.StandardJavaFileManager;
duke@1: import javax.tools.StandardLocation;
duke@1: import javax.tools.ToolProvider;
duke@1:
duke@1: /**
duke@1: * Used to generate a "symbol file" representing rt.jar that only
duke@1: * includes supported or legacy proprietary API. Valid annotation
duke@1: * processor options:
duke@1: *
duke@1: *
duke@1: * - com.sun.tools.javac.sym.Jar
duke@1: * - Specifies the location of rt.jar.
duke@1: * - com.sun.tools.javac.sym.Dest
duke@1: * - Specifies the destination directory.
duke@1: *
duke@1: *
jjg@581: * This is NOT part of any supported API.
duke@1: * If you write code that depends on this, you do so at your own
duke@1: * risk. This code and its internal interfaces are subject to change
duke@1: * or deletion without notice.
duke@1: *
duke@1: * @author Peter von der Ah\u00e9
duke@1: */
jjg@1569: @SupportedOptions({
jjg@1569: "com.sun.tools.javac.sym.Jar",
jjg@1569: "com.sun.tools.javac.sym.Dest",
jjg@1569: "com.sun.tools.javac.sym.Profiles"})
duke@1: @SupportedAnnotationTypes("*")
duke@1: public class CreateSymbols extends AbstractProcessor {
duke@1:
duke@1: static Set getLegacyPackages() {
duke@1: ResourceBundle legacyBundle
duke@1: = ResourceBundle.getBundle("com.sun.tools.javac.resources.legacy");
duke@1: Set keys = new HashSet();
duke@1: for (Enumeration e = legacyBundle.getKeys(); e.hasMoreElements(); )
duke@1: keys.add(e.nextElement());
duke@1: return keys;
duke@1: }
duke@1:
duke@1: public boolean process(Set extends TypeElement> tes, RoundEnvironment renv) {
duke@1: try {
duke@1: if (renv.processingOver())
duke@1: createSymbols();
duke@1: } catch (IOException e) {
duke@1: processingEnv.getMessager()
duke@1: .printMessage(Diagnostic.Kind.ERROR, e.getLocalizedMessage());
duke@1: } catch (Throwable t) {
jjg@1569: t.printStackTrace();
duke@1: Throwable cause = t.getCause();
duke@1: if (cause == null)
duke@1: cause = t;
duke@1: processingEnv.getMessager()
duke@1: .printMessage(Diagnostic.Kind.ERROR, cause.getLocalizedMessage());
duke@1: }
duke@1: return true;
duke@1: }
duke@1:
duke@1: void createSymbols() throws IOException {
duke@1: Set legacy = getLegacyPackages();
duke@1: Set legacyProprietary = getLegacyPackages();
duke@1: Set documented = new HashSet();
duke@1: Set packages =
duke@1: ((JavacProcessingEnvironment)processingEnv).getSpecifiedPackages();
jjg@1569: Map pOptions = processingEnv.getOptions();
jjg@1569: String jarName = pOptions.get("com.sun.tools.javac.sym.Jar");
duke@1: if (jarName == null)
duke@1: throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR");
jjg@1569: String destName = pOptions.get("com.sun.tools.javac.sym.Dest");
duke@1: if (destName == null)
duke@1: throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR");
jjg@1569: String profileSpec=pOptions.get("com.sun.tools.javac.sym.Profiles");
jjg@1569: if (profileSpec == null)
jjg@1569: throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Profiles=PROFILES_SPEC");
jjg@1569: Profiles profiles = Profiles.read(new File(profileSpec));
duke@1:
duke@1: for (PackageSymbol psym : packages) {
duke@1: String name = psym.getQualifiedName().toString();
duke@1: legacyProprietary.remove(name);
duke@1: documented.add(name);
duke@1: }
duke@1:
duke@1: JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
duke@1: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
duke@1: Location jarLocation = StandardLocation.locationFor(jarName);
duke@1: File jarFile = new File(jarName);
duke@1: fm.setLocation(jarLocation, List.of(jarFile));
duke@1: fm.setLocation(StandardLocation.CLASS_PATH, List.nil());
duke@1: fm.setLocation(StandardLocation.SOURCE_PATH, List.nil());
duke@1: {
duke@1: ArrayList bootClassPath = new ArrayList();
duke@1: bootClassPath.add(jarFile);
duke@1: for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
duke@1: if (!new File(path.getName()).equals(new File("rt.jar")))
duke@1: bootClassPath.add(path);
duke@1: }
duke@1: System.err.println("Using boot class path = " + bootClassPath);
duke@1: fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
duke@1: }
duke@1: // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
duke@1: File destDir = new File(destName);
duke@1: if (!destDir.exists())
duke@1: if (!destDir.mkdirs())
duke@1: throw new RuntimeException("Could not create " + destDir);
duke@1: fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
duke@1: Set hiddenPackages = new HashSet();
duke@1: Set crisp = new HashSet();
duke@1: List options = List.of("-XDdev");
duke@1: // options = options.prepend("-doe");
duke@1: // options = options.prepend("-verbose");
duke@1: JavacTaskImpl task = (JavacTaskImpl)
duke@1: tool.getTask(null, fm, null, options, null, null);
duke@1: com.sun.tools.javac.main.JavaCompiler compiler =
duke@1: com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
duke@1: ClassWriter writer = ClassWriter.instance(task.getContext());
duke@1: Symtab syms = Symtab.instance(task.getContext());
jjg@1569: Names names = Names.instance(task.getContext());
jjg@1569: Attribute.Compound proprietaryAnno =
duke@1: new Attribute.Compound(syms.proprietaryType,
duke@1: List.>nil());
jjg@1569: Attribute.Compound[] profileAnnos = new Attribute.Compound[profiles.getProfileCount() + 1];
jjg@1569: Symbol.MethodSymbol profileValue = (MethodSymbol) syms.profileType.tsym.members().lookup(names.value).sym;
jjg@1569: for (int i = 1; i < profileAnnos.length; i++) {
jjg@1569: profileAnnos[i] = new Attribute.Compound(syms.profileType,
jjg@1569: List.>of(
jjg@1569: new Pair(profileValue, new Attribute.Constant(syms.intType, i))));
jjg@1569: }
duke@1:
duke@1: Type.moreInfo = true;
vromero@1452: Types types = Types.instance(task.getContext());
vromero@1452: Pool pool = new Pool(types);
duke@1: for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
duke@1: String className = fm.inferBinaryName(jarLocation, file);
duke@1: int index = className.lastIndexOf('.');
duke@1: String pckName = index == -1 ? "" : className.substring(0, index);
duke@1: boolean addLegacyAnnotation = false;
duke@1: if (documented.contains(pckName)) {
duke@1: if (!legacy.contains(pckName))
duke@1: crisp.add(pckName);
duke@1: // System.out.println("Documented: " + className);
duke@1: } else if (legacyProprietary.contains(pckName)) {
duke@1: addLegacyAnnotation = true;
duke@1: // System.out.println("Legacy proprietary: " + className);
duke@1: } else {
duke@1: // System.out.println("Hidden " + className);
duke@1: hiddenPackages.add(pckName);
duke@1: continue;
duke@1: }
duke@1: TypeSymbol sym = (TypeSymbol)compiler.resolveIdent(className);
duke@1: if (sym.kind != Kinds.TYP) {
duke@1: if (className.indexOf('$') < 0) {
duke@1: System.err.println("Ignoring (other) " + className + " : " + sym);
duke@1: System.err.println(" " + sym.getClass().getSimpleName() + " " + sym.type);
duke@1: }
duke@1: continue;
duke@1: }
duke@1: sym.complete();
duke@1: if (sym.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
duke@1: System.err.println("Ignoring (bad) " + sym.getQualifiedName());
duke@1: continue;
duke@1: }
duke@1: ClassSymbol cs = (ClassSymbol) sym;
duke@1: if (addLegacyAnnotation) {
jjg@1569: cs.annotations.prepend(List.of(proprietaryAnno));
duke@1: }
jjg@1569: int p = profiles.getProfile(cs.fullname.toString().replace(".", "/"));
jjg@1569: if (0 < p && p < profileAnnos.length)
jjg@1569: cs.annotations.prepend(List.of(profileAnnos[p]));
duke@1: writeClass(pool, cs, writer);
duke@1: }
duke@1:
duke@1: if (false) {
duke@1: for (String pckName : crisp)
duke@1: System.out.println("Crisp: " + pckName);
duke@1: for (String pckName : hiddenPackages)
duke@1: System.out.println("Hidden: " + pckName);
duke@1: for (String pckName : legacyProprietary)
duke@1: System.out.println("Legacy proprietary: " + pckName);
duke@1: for (String pckName : documented)
duke@1: System.out.println("Documented: " + pckName);
duke@1: }
duke@1: }
duke@1:
duke@1: void writeClass(final Pool pool, final ClassSymbol cs, final ClassWriter writer)
duke@1: throws IOException
duke@1: {
duke@1: try {
duke@1: pool.reset();
duke@1: cs.pool = pool;
duke@1: writer.writeClass(cs);
duke@1: for (Scope.Entry e = cs.members().elems; e != null; e = e.sibling) {
duke@1: if (e.sym.kind == Kinds.TYP) {
duke@1: ClassSymbol nestedClass = (ClassSymbol)e.sym;
duke@1: nestedClass.complete();
duke@1: writeClass(pool, nestedClass, writer);
duke@1: }
duke@1: }
duke@1: } catch (ClassWriter.StringOverflow ex) {
duke@1: throw new RuntimeException(ex);
duke@1: } catch (ClassWriter.PoolOverflow ex) {
duke@1: throw new RuntimeException(ex);
duke@1: }
duke@1: }
duke@1:
duke@1: public SourceVersion getSupportedSourceVersion() {
duke@1: return SourceVersion.latest();
duke@1: }
duke@1:
duke@1: // used for debugging
duke@1: public static void main(String... args) {
duke@1: String rt_jar = args[0];
duke@1: String dest = args[1];
duke@1: args = new String[] {
duke@1: "-Xbootclasspath:" + rt_jar,
duke@1: "-XDprocess.packages",
duke@1: "-proc:only",
duke@1: "-processor",
duke@1: "com.sun.tools.javac.sym.CreateSymbols",
duke@1: "-Acom.sun.tools.javac.sym.Jar=" + rt_jar,
duke@1: "-Acom.sun.tools.javac.sym.Dest=" + dest,
duke@1: //
duke@1: "java.applet",
duke@1: "java.awt",
duke@1: "java.awt.color",
duke@1: "java.awt.datatransfer",
duke@1: "java.awt.dnd",
duke@1: "java.awt.event",
duke@1: "java.awt.font",
duke@1: "java.awt.geom",
duke@1: "java.awt.im",
duke@1: "java.awt.im.spi",
duke@1: "java.awt.image",
duke@1: "java.awt.image.renderable",
duke@1: "java.awt.print",
duke@1: "java.beans",
duke@1: "java.beans.beancontext",
duke@1: "java.io",
duke@1: "java.lang",
duke@1: "java.lang.annotation",
duke@1: "java.lang.instrument",
duke@1: "java.lang.management",
duke@1: "java.lang.ref",
duke@1: "java.lang.reflect",
duke@1: "java.math",
duke@1: "java.net",
duke@1: "java.nio",
duke@1: "java.nio.channels",
duke@1: "java.nio.channels.spi",
duke@1: "java.nio.charset",
duke@1: "java.nio.charset.spi",
duke@1: "java.rmi",
duke@1: "java.rmi.activation",
duke@1: "java.rmi.dgc",
duke@1: "java.rmi.registry",
duke@1: "java.rmi.server",
duke@1: "java.security",
duke@1: "java.security.acl",
duke@1: "java.security.cert",
duke@1: "java.security.interfaces",
duke@1: "java.security.spec",
duke@1: "java.sql",
duke@1: "java.text",
duke@1: "java.text.spi",
duke@1: "java.util",
duke@1: "java.util.concurrent",
duke@1: "java.util.concurrent.atomic",
duke@1: "java.util.concurrent.locks",
duke@1: "java.util.jar",
duke@1: "java.util.logging",
duke@1: "java.util.prefs",
duke@1: "java.util.regex",
duke@1: "java.util.spi",
duke@1: "java.util.zip",
duke@1: "javax.accessibility",
duke@1: "javax.activation",
duke@1: "javax.activity",
duke@1: "javax.annotation",
duke@1: "javax.annotation.processing",
duke@1: "javax.crypto",
duke@1: "javax.crypto.interfaces",
duke@1: "javax.crypto.spec",
duke@1: "javax.imageio",
duke@1: "javax.imageio.event",
duke@1: "javax.imageio.metadata",
duke@1: "javax.imageio.plugins.jpeg",
duke@1: "javax.imageio.plugins.bmp",
duke@1: "javax.imageio.spi",
duke@1: "javax.imageio.stream",
duke@1: "javax.jws",
duke@1: "javax.jws.soap",
duke@1: "javax.lang.model",
duke@1: "javax.lang.model.element",
duke@1: "javax.lang.model.type",
duke@1: "javax.lang.model.util",
duke@1: "javax.management",
duke@1: "javax.management.loading",
duke@1: "javax.management.monitor",
duke@1: "javax.management.relation",
duke@1: "javax.management.openmbean",
duke@1: "javax.management.timer",
duke@1: "javax.management.modelmbean",
duke@1: "javax.management.remote",
duke@1: "javax.management.remote.rmi",
duke@1: "javax.naming",
duke@1: "javax.naming.directory",
duke@1: "javax.naming.event",
duke@1: "javax.naming.ldap",
duke@1: "javax.naming.spi",
duke@1: "javax.net",
duke@1: "javax.net.ssl",
duke@1: "javax.print",
duke@1: "javax.print.attribute",
duke@1: "javax.print.attribute.standard",
duke@1: "javax.print.event",
duke@1: "javax.rmi",
duke@1: "javax.rmi.CORBA",
duke@1: "javax.rmi.ssl",
duke@1: "javax.script",
duke@1: "javax.security.auth",
duke@1: "javax.security.auth.callback",
duke@1: "javax.security.auth.kerberos",
duke@1: "javax.security.auth.login",
duke@1: "javax.security.auth.spi",
duke@1: "javax.security.auth.x500",
duke@1: "javax.security.cert",
duke@1: "javax.security.sasl",
duke@1: "javax.sound.sampled",
duke@1: "javax.sound.sampled.spi",
duke@1: "javax.sound.midi",
duke@1: "javax.sound.midi.spi",
duke@1: "javax.sql",
duke@1: "javax.sql.rowset",
duke@1: "javax.sql.rowset.serial",
duke@1: "javax.sql.rowset.spi",
duke@1: "javax.swing",
duke@1: "javax.swing.border",
duke@1: "javax.swing.colorchooser",
duke@1: "javax.swing.filechooser",
duke@1: "javax.swing.event",
duke@1: "javax.swing.table",
duke@1: "javax.swing.text",
duke@1: "javax.swing.text.html",
duke@1: "javax.swing.text.html.parser",
duke@1: "javax.swing.text.rtf",
duke@1: "javax.swing.tree",
duke@1: "javax.swing.undo",
duke@1: "javax.swing.plaf",
duke@1: "javax.swing.plaf.basic",
duke@1: "javax.swing.plaf.metal",
duke@1: "javax.swing.plaf.multi",
duke@1: "javax.swing.plaf.synth",
duke@1: "javax.tools",
duke@1: "javax.transaction",
duke@1: "javax.transaction.xa",
duke@1: "javax.xml.parsers",
duke@1: "javax.xml.bind",
duke@1: "javax.xml.bind.annotation",
duke@1: "javax.xml.bind.annotation.adapters",
duke@1: "javax.xml.bind.attachment",
duke@1: "javax.xml.bind.helpers",
duke@1: "javax.xml.bind.util",
duke@1: "javax.xml.soap",
duke@1: "javax.xml.ws",
duke@1: "javax.xml.ws.handler",
duke@1: "javax.xml.ws.handler.soap",
duke@1: "javax.xml.ws.http",
duke@1: "javax.xml.ws.soap",
duke@1: "javax.xml.ws.spi",
duke@1: "javax.xml.transform",
duke@1: "javax.xml.transform.sax",
duke@1: "javax.xml.transform.dom",
duke@1: "javax.xml.transform.stax",
duke@1: "javax.xml.transform.stream",
duke@1: "javax.xml",
duke@1: "javax.xml.crypto",
duke@1: "javax.xml.crypto.dom",
duke@1: "javax.xml.crypto.dsig",
duke@1: "javax.xml.crypto.dsig.dom",
duke@1: "javax.xml.crypto.dsig.keyinfo",
duke@1: "javax.xml.crypto.dsig.spec",
duke@1: "javax.xml.datatype",
duke@1: "javax.xml.validation",
duke@1: "javax.xml.namespace",
duke@1: "javax.xml.xpath",
duke@1: "javax.xml.stream",
duke@1: "javax.xml.stream.events",
duke@1: "javax.xml.stream.util",
duke@1: "org.ietf.jgss",
duke@1: "org.omg.CORBA",
duke@1: "org.omg.CORBA.DynAnyPackage",
duke@1: "org.omg.CORBA.ORBPackage",
duke@1: "org.omg.CORBA.TypeCodePackage",
duke@1: "org.omg.stub.java.rmi",
duke@1: "org.omg.CORBA.portable",
duke@1: "org.omg.CORBA_2_3",
duke@1: "org.omg.CORBA_2_3.portable",
duke@1: "org.omg.CosNaming",
duke@1: "org.omg.CosNaming.NamingContextExtPackage",
duke@1: "org.omg.CosNaming.NamingContextPackage",
duke@1: "org.omg.SendingContext",
duke@1: "org.omg.PortableServer",
duke@1: "org.omg.PortableServer.CurrentPackage",
duke@1: "org.omg.PortableServer.POAPackage",
duke@1: "org.omg.PortableServer.POAManagerPackage",
duke@1: "org.omg.PortableServer.ServantLocatorPackage",
duke@1: "org.omg.PortableServer.portable",
duke@1: "org.omg.PortableInterceptor",
duke@1: "org.omg.PortableInterceptor.ORBInitInfoPackage",
duke@1: "org.omg.Messaging",
duke@1: "org.omg.IOP",
duke@1: "org.omg.IOP.CodecFactoryPackage",
duke@1: "org.omg.IOP.CodecPackage",
duke@1: "org.omg.Dynamic",
duke@1: "org.omg.DynamicAny",
duke@1: "org.omg.DynamicAny.DynAnyPackage",
duke@1: "org.omg.DynamicAny.DynAnyFactoryPackage",
duke@1: "org.w3c.dom",
duke@1: "org.w3c.dom.events",
duke@1: "org.w3c.dom.bootstrap",
duke@1: "org.w3c.dom.ls",
duke@1: "org.xml.sax",
duke@1: "org.xml.sax.ext",
duke@1: "org.xml.sax.helpers",
duke@1: "com.sun.java.browser.dom",
duke@1: "org.w3c.dom",
duke@1: "org.w3c.dom.bootstrap",
duke@1: "org.w3c.dom.ls",
duke@1: "org.w3c.dom.ranges",
duke@1: "org.w3c.dom.traversal",
duke@1: "org.w3c.dom.html",
duke@1: "org.w3c.dom.stylesheets",
duke@1: "org.w3c.dom.css",
duke@1: "org.w3c.dom.events",
duke@1: "org.w3c.dom.views",
duke@1: "com.sun.management",
duke@1: "com.sun.security.auth",
duke@1: "com.sun.security.auth.callback",
duke@1: "com.sun.security.auth.login",
duke@1: "com.sun.security.auth.module",
duke@1: "com.sun.security.jgss",
duke@1: "com.sun.net.httpserver",
duke@1: "com.sun.net.httpserver.spi",
duke@1: "javax.smartcardio"
duke@1: //
duke@1: };
duke@1: com.sun.tools.javac.Main.compile(args);
duke@1: }
duke@1:
duke@1: }