1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/sym/CreateSymbols.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,495 @@ 1.4 +/* 1.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.sym; 1.30 + 1.31 +import com.sun.tools.javac.api.JavacTaskImpl; 1.32 +import com.sun.tools.javac.code.Kinds; 1.33 +import com.sun.tools.javac.code.Scope; 1.34 +import com.sun.tools.javac.code.Symbol.*; 1.35 +import com.sun.tools.javac.code.Symbol; 1.36 +import com.sun.tools.javac.code.Attribute; 1.37 +import com.sun.tools.javac.code.Symtab; 1.38 +import com.sun.tools.javac.code.Type; 1.39 +import com.sun.tools.javac.jvm.ClassReader; 1.40 +import com.sun.tools.javac.jvm.ClassWriter; 1.41 +import com.sun.tools.javac.jvm.Pool; 1.42 +import com.sun.tools.javac.processing.JavacProcessingEnvironment; 1.43 +import com.sun.tools.javac.util.List; 1.44 +import com.sun.tools.javac.util.Pair; 1.45 +import com.sun.tools.javac.util.Name; 1.46 + 1.47 +import java.io.File; 1.48 +import java.io.IOException; 1.49 +import java.util.ArrayList; 1.50 +import java.util.EnumSet; 1.51 +import java.util.Enumeration; 1.52 +import java.util.HashSet; 1.53 +import java.util.Properties; 1.54 +import java.util.ResourceBundle; 1.55 +import java.util.Set; 1.56 + 1.57 +import javax.annotation.processing.AbstractProcessor; 1.58 +import javax.annotation.processing.RoundEnvironment; 1.59 +import javax.annotation.processing.SupportedAnnotationTypes; 1.60 +import javax.annotation.processing.SupportedOptions; 1.61 +import javax.lang.model.SourceVersion; 1.62 +import javax.lang.model.element.ElementKind; 1.63 +import javax.lang.model.element.TypeElement; 1.64 +import javax.tools.Diagnostic; 1.65 +import javax.tools.JavaCompiler; 1.66 +import javax.tools.JavaFileManager.Location; 1.67 +import javax.tools.JavaFileObject; 1.68 +import static javax.tools.JavaFileObject.Kind.CLASS; 1.69 +import javax.tools.StandardJavaFileManager; 1.70 +import javax.tools.StandardLocation; 1.71 +import javax.tools.ToolProvider; 1.72 + 1.73 +/** 1.74 + * Used to generate a "symbol file" representing rt.jar that only 1.75 + * includes supported or legacy proprietary API. Valid annotation 1.76 + * processor options: 1.77 + * 1.78 + * <dl> 1.79 + * <dt>com.sun.tools.javac.sym.Jar</dt> 1.80 + * <dd>Specifies the location of rt.jar.</dd> 1.81 + * <dt>com.sun.tools.javac.sym.Dest</dt> 1.82 + * <dd>Specifies the destination directory.</dd> 1.83 + * </dl> 1.84 + * 1.85 + * <p><b>This is NOT part of any API supported by Sun Microsystems. 1.86 + * If you write code that depends on this, you do so at your own 1.87 + * risk. This code and its internal interfaces are subject to change 1.88 + * or deletion without notice.</b></p> 1.89 + * 1.90 + * @author Peter von der Ah\u00e9 1.91 + */ 1.92 +@SupportedOptions({"com.sun.tools.javac.sym.Jar","com.sun.tools.javac.sym.Dest"}) 1.93 +@SupportedAnnotationTypes("*") 1.94 +public class CreateSymbols extends AbstractProcessor { 1.95 + 1.96 + static Set<String> getLegacyPackages() { 1.97 + ResourceBundle legacyBundle 1.98 + = ResourceBundle.getBundle("com.sun.tools.javac.resources.legacy"); 1.99 + Set<String> keys = new HashSet<String>(); 1.100 + for (Enumeration<String> e = legacyBundle.getKeys(); e.hasMoreElements(); ) 1.101 + keys.add(e.nextElement()); 1.102 + return keys; 1.103 + } 1.104 + 1.105 + public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) { 1.106 + try { 1.107 + if (renv.processingOver()) 1.108 + createSymbols(); 1.109 + } catch (IOException e) { 1.110 + processingEnv.getMessager() 1.111 + .printMessage(Diagnostic.Kind.ERROR, e.getLocalizedMessage()); 1.112 + } catch (Throwable t) { 1.113 + Throwable cause = t.getCause(); 1.114 + if (cause == null) 1.115 + cause = t; 1.116 + processingEnv.getMessager() 1.117 + .printMessage(Diagnostic.Kind.ERROR, cause.getLocalizedMessage()); 1.118 + } 1.119 + return true; 1.120 + } 1.121 + 1.122 + void createSymbols() throws IOException { 1.123 + Set<String> legacy = getLegacyPackages(); 1.124 + Set<String> legacyProprietary = getLegacyPackages(); 1.125 + Set<String> documented = new HashSet<String>(); 1.126 + Set<PackageSymbol> packages = 1.127 + ((JavacProcessingEnvironment)processingEnv).getSpecifiedPackages(); 1.128 + String jarName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Jar"); 1.129 + if (jarName == null) 1.130 + throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR"); 1.131 + String destName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Dest"); 1.132 + if (destName == null) 1.133 + throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR"); 1.134 + 1.135 + for (PackageSymbol psym : packages) { 1.136 + String name = psym.getQualifiedName().toString(); 1.137 + legacyProprietary.remove(name); 1.138 + documented.add(name); 1.139 + } 1.140 + 1.141 + JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 1.142 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.143 + Location jarLocation = StandardLocation.locationFor(jarName); 1.144 + File jarFile = new File(jarName); 1.145 + fm.setLocation(jarLocation, List.of(jarFile)); 1.146 + fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil()); 1.147 + fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil()); 1.148 + { 1.149 + ArrayList<File> bootClassPath = new ArrayList<File>(); 1.150 + bootClassPath.add(jarFile); 1.151 + for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) { 1.152 + if (!new File(path.getName()).equals(new File("rt.jar"))) 1.153 + bootClassPath.add(path); 1.154 + } 1.155 + System.err.println("Using boot class path = " + bootClassPath); 1.156 + fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath); 1.157 + } 1.158 + // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)); 1.159 + File destDir = new File(destName); 1.160 + if (!destDir.exists()) 1.161 + if (!destDir.mkdirs()) 1.162 + throw new RuntimeException("Could not create " + destDir); 1.163 + fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir)); 1.164 + Set<String> hiddenPackages = new HashSet<String>(); 1.165 + Set<String> crisp = new HashSet<String>(); 1.166 + List<String> options = List.of("-XDdev"); 1.167 + // options = options.prepend("-doe"); 1.168 + // options = options.prepend("-verbose"); 1.169 + JavacTaskImpl task = (JavacTaskImpl) 1.170 + tool.getTask(null, fm, null, options, null, null); 1.171 + com.sun.tools.javac.main.JavaCompiler compiler = 1.172 + com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()); 1.173 + ClassReader reader = ClassReader.instance(task.getContext()); 1.174 + ClassWriter writer = ClassWriter.instance(task.getContext()); 1.175 + Symtab syms = Symtab.instance(task.getContext()); 1.176 + Attribute.Compound proprietary = 1.177 + new Attribute.Compound(syms.proprietaryType, 1.178 + List.<Pair<Symbol.MethodSymbol,Attribute>>nil()); 1.179 + 1.180 + Type.moreInfo = true; 1.181 + Pool pool = new Pool(); 1.182 + for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) { 1.183 + String className = fm.inferBinaryName(jarLocation, file); 1.184 + int index = className.lastIndexOf('.'); 1.185 + String pckName = index == -1 ? "" : className.substring(0, index); 1.186 + boolean addLegacyAnnotation = false; 1.187 + if (documented.contains(pckName)) { 1.188 + if (!legacy.contains(pckName)) 1.189 + crisp.add(pckName); 1.190 + // System.out.println("Documented: " + className); 1.191 + } else if (legacyProprietary.contains(pckName)) { 1.192 + addLegacyAnnotation = true; 1.193 + // System.out.println("Legacy proprietary: " + className); 1.194 + } else { 1.195 + // System.out.println("Hidden " + className); 1.196 + hiddenPackages.add(pckName); 1.197 + continue; 1.198 + } 1.199 + TypeSymbol sym = (TypeSymbol)compiler.resolveIdent(className); 1.200 + if (sym.kind != Kinds.TYP) { 1.201 + if (className.indexOf('$') < 0) { 1.202 + System.err.println("Ignoring (other) " + className + " : " + sym); 1.203 + System.err.println(" " + sym.getClass().getSimpleName() + " " + sym.type); 1.204 + } 1.205 + continue; 1.206 + } 1.207 + sym.complete(); 1.208 + if (sym.getEnclosingElement().getKind() != ElementKind.PACKAGE) { 1.209 + System.err.println("Ignoring (bad) " + sym.getQualifiedName()); 1.210 + continue; 1.211 + } 1.212 + ClassSymbol cs = (ClassSymbol) sym; 1.213 + if (addLegacyAnnotation) { 1.214 + cs.attributes_field = (cs.attributes_field == null) 1.215 + ? List.of(proprietary) 1.216 + : cs.attributes_field.prepend(proprietary); 1.217 + } 1.218 + writeClass(pool, cs, writer); 1.219 + } 1.220 + 1.221 + if (false) { 1.222 + for (String pckName : crisp) 1.223 + System.out.println("Crisp: " + pckName); 1.224 + for (String pckName : hiddenPackages) 1.225 + System.out.println("Hidden: " + pckName); 1.226 + for (String pckName : legacyProprietary) 1.227 + System.out.println("Legacy proprietary: " + pckName); 1.228 + for (String pckName : documented) 1.229 + System.out.println("Documented: " + pckName); 1.230 + } 1.231 + } 1.232 + 1.233 + void writeClass(final Pool pool, final ClassSymbol cs, final ClassWriter writer) 1.234 + throws IOException 1.235 + { 1.236 + try { 1.237 + pool.reset(); 1.238 + cs.pool = pool; 1.239 + writer.writeClass(cs); 1.240 + for (Scope.Entry e = cs.members().elems; e != null; e = e.sibling) { 1.241 + if (e.sym.kind == Kinds.TYP) { 1.242 + ClassSymbol nestedClass = (ClassSymbol)e.sym; 1.243 + nestedClass.complete(); 1.244 + writeClass(pool, nestedClass, writer); 1.245 + } 1.246 + } 1.247 + } catch (ClassWriter.StringOverflow ex) { 1.248 + throw new RuntimeException(ex); 1.249 + } catch (ClassWriter.PoolOverflow ex) { 1.250 + throw new RuntimeException(ex); 1.251 + } 1.252 + } 1.253 + 1.254 + public SourceVersion getSupportedSourceVersion() { 1.255 + return SourceVersion.latest(); 1.256 + } 1.257 + 1.258 + // used for debugging 1.259 + public static void main(String... args) { 1.260 + String rt_jar = args[0]; 1.261 + String dest = args[1]; 1.262 + args = new String[] { 1.263 + "-Xbootclasspath:" + rt_jar, 1.264 + "-XDprocess.packages", 1.265 + "-proc:only", 1.266 + "-processor", 1.267 + "com.sun.tools.javac.sym.CreateSymbols", 1.268 + "-Acom.sun.tools.javac.sym.Jar=" + rt_jar, 1.269 + "-Acom.sun.tools.javac.sym.Dest=" + dest, 1.270 + // <editor-fold defaultstate="collapsed"> 1.271 + "java.applet", 1.272 + "java.awt", 1.273 + "java.awt.color", 1.274 + "java.awt.datatransfer", 1.275 + "java.awt.dnd", 1.276 + "java.awt.event", 1.277 + "java.awt.font", 1.278 + "java.awt.geom", 1.279 + "java.awt.im", 1.280 + "java.awt.im.spi", 1.281 + "java.awt.image", 1.282 + "java.awt.image.renderable", 1.283 + "java.awt.print", 1.284 + "java.beans", 1.285 + "java.beans.beancontext", 1.286 + "java.io", 1.287 + "java.lang", 1.288 + "java.lang.annotation", 1.289 + "java.lang.instrument", 1.290 + "java.lang.management", 1.291 + "java.lang.ref", 1.292 + "java.lang.reflect", 1.293 + "java.math", 1.294 + "java.net", 1.295 + "java.nio", 1.296 + "java.nio.channels", 1.297 + "java.nio.channels.spi", 1.298 + "java.nio.charset", 1.299 + "java.nio.charset.spi", 1.300 + "java.rmi", 1.301 + "java.rmi.activation", 1.302 + "java.rmi.dgc", 1.303 + "java.rmi.registry", 1.304 + "java.rmi.server", 1.305 + "java.security", 1.306 + "java.security.acl", 1.307 + "java.security.cert", 1.308 + "java.security.interfaces", 1.309 + "java.security.spec", 1.310 + "java.sql", 1.311 + "java.text", 1.312 + "java.text.spi", 1.313 + "java.util", 1.314 + "java.util.concurrent", 1.315 + "java.util.concurrent.atomic", 1.316 + "java.util.concurrent.locks", 1.317 + "java.util.jar", 1.318 + "java.util.logging", 1.319 + "java.util.prefs", 1.320 + "java.util.regex", 1.321 + "java.util.spi", 1.322 + "java.util.zip", 1.323 + "javax.accessibility", 1.324 + "javax.activation", 1.325 + "javax.activity", 1.326 + "javax.annotation", 1.327 + "javax.annotation.processing", 1.328 + "javax.crypto", 1.329 + "javax.crypto.interfaces", 1.330 + "javax.crypto.spec", 1.331 + "javax.imageio", 1.332 + "javax.imageio.event", 1.333 + "javax.imageio.metadata", 1.334 + "javax.imageio.plugins.jpeg", 1.335 + "javax.imageio.plugins.bmp", 1.336 + "javax.imageio.spi", 1.337 + "javax.imageio.stream", 1.338 + "javax.jws", 1.339 + "javax.jws.soap", 1.340 + "javax.lang.model", 1.341 + "javax.lang.model.element", 1.342 + "javax.lang.model.type", 1.343 + "javax.lang.model.util", 1.344 + "javax.management", 1.345 + "javax.management.loading", 1.346 + "javax.management.monitor", 1.347 + "javax.management.relation", 1.348 + "javax.management.openmbean", 1.349 + "javax.management.timer", 1.350 + "javax.management.modelmbean", 1.351 + "javax.management.remote", 1.352 + "javax.management.remote.rmi", 1.353 + "javax.naming", 1.354 + "javax.naming.directory", 1.355 + "javax.naming.event", 1.356 + "javax.naming.ldap", 1.357 + "javax.naming.spi", 1.358 + "javax.net", 1.359 + "javax.net.ssl", 1.360 + "javax.print", 1.361 + "javax.print.attribute", 1.362 + "javax.print.attribute.standard", 1.363 + "javax.print.event", 1.364 + "javax.rmi", 1.365 + "javax.rmi.CORBA", 1.366 + "javax.rmi.ssl", 1.367 + "javax.script", 1.368 + "javax.security.auth", 1.369 + "javax.security.auth.callback", 1.370 + "javax.security.auth.kerberos", 1.371 + "javax.security.auth.login", 1.372 + "javax.security.auth.spi", 1.373 + "javax.security.auth.x500", 1.374 + "javax.security.cert", 1.375 + "javax.security.sasl", 1.376 + "javax.sound.sampled", 1.377 + "javax.sound.sampled.spi", 1.378 + "javax.sound.midi", 1.379 + "javax.sound.midi.spi", 1.380 + "javax.sql", 1.381 + "javax.sql.rowset", 1.382 + "javax.sql.rowset.serial", 1.383 + "javax.sql.rowset.spi", 1.384 + "javax.swing", 1.385 + "javax.swing.border", 1.386 + "javax.swing.colorchooser", 1.387 + "javax.swing.filechooser", 1.388 + "javax.swing.event", 1.389 + "javax.swing.table", 1.390 + "javax.swing.text", 1.391 + "javax.swing.text.html", 1.392 + "javax.swing.text.html.parser", 1.393 + "javax.swing.text.rtf", 1.394 + "javax.swing.tree", 1.395 + "javax.swing.undo", 1.396 + "javax.swing.plaf", 1.397 + "javax.swing.plaf.basic", 1.398 + "javax.swing.plaf.metal", 1.399 + "javax.swing.plaf.multi", 1.400 + "javax.swing.plaf.synth", 1.401 + "javax.tools", 1.402 + "javax.transaction", 1.403 + "javax.transaction.xa", 1.404 + "javax.xml.parsers", 1.405 + "javax.xml.bind", 1.406 + "javax.xml.bind.annotation", 1.407 + "javax.xml.bind.annotation.adapters", 1.408 + "javax.xml.bind.attachment", 1.409 + "javax.xml.bind.helpers", 1.410 + "javax.xml.bind.util", 1.411 + "javax.xml.soap", 1.412 + "javax.xml.ws", 1.413 + "javax.xml.ws.handler", 1.414 + "javax.xml.ws.handler.soap", 1.415 + "javax.xml.ws.http", 1.416 + "javax.xml.ws.soap", 1.417 + "javax.xml.ws.spi", 1.418 + "javax.xml.transform", 1.419 + "javax.xml.transform.sax", 1.420 + "javax.xml.transform.dom", 1.421 + "javax.xml.transform.stax", 1.422 + "javax.xml.transform.stream", 1.423 + "javax.xml", 1.424 + "javax.xml.crypto", 1.425 + "javax.xml.crypto.dom", 1.426 + "javax.xml.crypto.dsig", 1.427 + "javax.xml.crypto.dsig.dom", 1.428 + "javax.xml.crypto.dsig.keyinfo", 1.429 + "javax.xml.crypto.dsig.spec", 1.430 + "javax.xml.datatype", 1.431 + "javax.xml.validation", 1.432 + "javax.xml.namespace", 1.433 + "javax.xml.xpath", 1.434 + "javax.xml.stream", 1.435 + "javax.xml.stream.events", 1.436 + "javax.xml.stream.util", 1.437 + "org.ietf.jgss", 1.438 + "org.omg.CORBA", 1.439 + "org.omg.CORBA.DynAnyPackage", 1.440 + "org.omg.CORBA.ORBPackage", 1.441 + "org.omg.CORBA.TypeCodePackage", 1.442 + "org.omg.stub.java.rmi", 1.443 + "org.omg.CORBA.portable", 1.444 + "org.omg.CORBA_2_3", 1.445 + "org.omg.CORBA_2_3.portable", 1.446 + "org.omg.CosNaming", 1.447 + "org.omg.CosNaming.NamingContextExtPackage", 1.448 + "org.omg.CosNaming.NamingContextPackage", 1.449 + "org.omg.SendingContext", 1.450 + "org.omg.PortableServer", 1.451 + "org.omg.PortableServer.CurrentPackage", 1.452 + "org.omg.PortableServer.POAPackage", 1.453 + "org.omg.PortableServer.POAManagerPackage", 1.454 + "org.omg.PortableServer.ServantLocatorPackage", 1.455 + "org.omg.PortableServer.portable", 1.456 + "org.omg.PortableInterceptor", 1.457 + "org.omg.PortableInterceptor.ORBInitInfoPackage", 1.458 + "org.omg.Messaging", 1.459 + "org.omg.IOP", 1.460 + "org.omg.IOP.CodecFactoryPackage", 1.461 + "org.omg.IOP.CodecPackage", 1.462 + "org.omg.Dynamic", 1.463 + "org.omg.DynamicAny", 1.464 + "org.omg.DynamicAny.DynAnyPackage", 1.465 + "org.omg.DynamicAny.DynAnyFactoryPackage", 1.466 + "org.w3c.dom", 1.467 + "org.w3c.dom.events", 1.468 + "org.w3c.dom.bootstrap", 1.469 + "org.w3c.dom.ls", 1.470 + "org.xml.sax", 1.471 + "org.xml.sax.ext", 1.472 + "org.xml.sax.helpers", 1.473 + "com.sun.java.browser.dom", 1.474 + "org.w3c.dom", 1.475 + "org.w3c.dom.bootstrap", 1.476 + "org.w3c.dom.ls", 1.477 + "org.w3c.dom.ranges", 1.478 + "org.w3c.dom.traversal", 1.479 + "org.w3c.dom.html", 1.480 + "org.w3c.dom.stylesheets", 1.481 + "org.w3c.dom.css", 1.482 + "org.w3c.dom.events", 1.483 + "org.w3c.dom.views", 1.484 + "com.sun.management", 1.485 + "com.sun.security.auth", 1.486 + "com.sun.security.auth.callback", 1.487 + "com.sun.security.auth.login", 1.488 + "com.sun.security.auth.module", 1.489 + "com.sun.security.jgss", 1.490 + "com.sun.net.httpserver", 1.491 + "com.sun.net.httpserver.spi", 1.492 + "javax.smartcardio" 1.493 + // </editor-fold> 1.494 + }; 1.495 + com.sun.tools.javac.Main.compile(args); 1.496 + } 1.497 + 1.498 +}