src/share/classes/com/sun/tools/javac/sym/CreateSymbols.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2006, 2013, 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.javac.sym;
27
28 import com.sun.tools.javac.api.JavacTaskImpl;
29 import com.sun.tools.javac.code.Kinds;
30 import com.sun.tools.javac.code.Scope;
31 import com.sun.tools.javac.code.Symbol.*;
32 import com.sun.tools.javac.code.Symbol;
33 import com.sun.tools.javac.code.Attribute;
34 import com.sun.tools.javac.code.Symtab;
35 import com.sun.tools.javac.code.Type;
36 import com.sun.tools.javac.code.Types;
37 import com.sun.tools.javac.jvm.ClassWriter;
38 import com.sun.tools.javac.jvm.Pool;
39 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
40 import com.sun.tools.javac.util.List;
41 import com.sun.tools.javac.util.Names;
42 import com.sun.tools.javac.util.Pair;
43
44 import java.io.File;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.EnumSet;
48 import java.util.Enumeration;
49 import java.util.HashSet;
50 import java.util.Map;
51 import java.util.ResourceBundle;
52 import java.util.Set;
53
54 import javax.annotation.processing.AbstractProcessor;
55 import javax.annotation.processing.RoundEnvironment;
56 import javax.annotation.processing.SupportedAnnotationTypes;
57 import javax.annotation.processing.SupportedOptions;
58 import javax.lang.model.SourceVersion;
59 import javax.lang.model.element.ElementKind;
60 import javax.lang.model.element.TypeElement;
61 import javax.tools.Diagnostic;
62 import javax.tools.JavaCompiler;
63 import javax.tools.JavaFileManager.Location;
64 import javax.tools.JavaFileObject;
65 import static javax.tools.JavaFileObject.Kind.CLASS;
66 import javax.tools.StandardJavaFileManager;
67 import javax.tools.StandardLocation;
68 import javax.tools.ToolProvider;
69
70 /**
71 * Used to generate a "symbol file" representing rt.jar that only
72 * includes supported or legacy proprietary API. Valid annotation
73 * processor options:
74 *
75 * <dl>
76 * <dt>com.sun.tools.javac.sym.Jar</dt>
77 * <dd>Specifies the location of rt.jar.</dd>
78 * <dt>com.sun.tools.javac.sym.Dest</dt>
79 * <dd>Specifies the destination directory.</dd>
80 * </dl>
81 *
82 * <p><b>This is NOT part of any supported API.
83 * If you write code that depends on this, you do so at your own
84 * risk. This code and its internal interfaces are subject to change
85 * or deletion without notice.</b></p>
86 *
87 * @author Peter von der Ah\u00e9
88 */
89 @SupportedOptions({
90 "com.sun.tools.javac.sym.Jar",
91 "com.sun.tools.javac.sym.Dest",
92 "com.sun.tools.javac.sym.Profiles"})
93 @SupportedAnnotationTypes("*")
94 public class CreateSymbols extends AbstractProcessor {
95
96 static Set<String> getLegacyPackages() {
97 ResourceBundle legacyBundle
98 = ResourceBundle.getBundle("com.sun.tools.javac.resources.legacy");
99 Set<String> keys = new HashSet<String>();
100 for (Enumeration<String> e = legacyBundle.getKeys(); e.hasMoreElements(); )
101 keys.add(e.nextElement());
102 return keys;
103 }
104
105 public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
106 try {
107 if (renv.processingOver())
108 createSymbols();
109 } catch (IOException e) {
110 CharSequence msg = e.getLocalizedMessage();
111 if (msg == null)
112 msg = e.toString();
113 processingEnv.getMessager()
114 .printMessage(Diagnostic.Kind.ERROR, msg);
115 } catch (Throwable t) {
116 t.printStackTrace();
117 Throwable cause = t.getCause();
118 if (cause == null)
119 cause = t;
120 CharSequence msg = cause.getLocalizedMessage();
121 if (msg == null)
122 msg = cause.toString();
123 processingEnv.getMessager()
124 .printMessage(Diagnostic.Kind.ERROR, msg);
125 }
126 return true;
127 }
128
129 void createSymbols() throws IOException {
130 Set<String> legacy = getLegacyPackages();
131 Set<String> legacyProprietary = getLegacyPackages();
132 Set<String> documented = new HashSet<String>();
133 Set<PackageSymbol> packages =
134 ((JavacProcessingEnvironment)processingEnv).getSpecifiedPackages();
135 Map<String,String> pOptions = processingEnv.getOptions();
136 String jarName = pOptions.get("com.sun.tools.javac.sym.Jar");
137 if (jarName == null)
138 throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Jar=LOCATION_OF_JAR");
139 String destName = pOptions.get("com.sun.tools.javac.sym.Dest");
140 if (destName == null)
141 throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Dest=LOCATION_OF_JAR");
142 String profileSpec=pOptions.get("com.sun.tools.javac.sym.Profiles");
143 if (profileSpec == null)
144 throw new RuntimeException("Must use -Acom.sun.tools.javac.sym.Profiles=PROFILES_SPEC");
145 Profiles profiles = Profiles.read(new File(profileSpec));
146
147 for (PackageSymbol psym : packages) {
148 String name = psym.getQualifiedName().toString();
149 legacyProprietary.remove(name);
150 documented.add(name);
151 }
152
153 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
154 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
155 Location jarLocation = StandardLocation.locationFor(jarName);
156 File jarFile = new File(jarName);
157 fm.setLocation(jarLocation, List.of(jarFile));
158 fm.setLocation(StandardLocation.CLASS_PATH, List.<File>nil());
159 fm.setLocation(StandardLocation.SOURCE_PATH, List.<File>nil());
160 {
161 ArrayList<File> bootClassPath = new ArrayList<File>();
162 bootClassPath.add(jarFile);
163 for (File path : fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH)) {
164 if (!new File(path.getName()).equals(new File("rt.jar")))
165 bootClassPath.add(path);
166 }
167 System.err.println("Using boot class path = " + bootClassPath);
168 fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, bootClassPath);
169 }
170 // System.out.println(fm.getLocation(StandardLocation.PLATFORM_CLASS_PATH));
171 File destDir = new File(destName);
172 if (!destDir.exists())
173 if (!destDir.mkdirs())
174 throw new RuntimeException("Could not create " + destDir);
175 fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(destDir));
176 Set<String> hiddenPackages = new HashSet<String>();
177 Set<String> crisp = new HashSet<String>();
178 List<String> options = List.of("-XDdev");
179 // options = options.prepend("-doe");
180 // options = options.prepend("-verbose");
181 JavacTaskImpl task = (JavacTaskImpl)
182 tool.getTask(null, fm, null, options, null, null);
183 com.sun.tools.javac.main.JavaCompiler compiler =
184 com.sun.tools.javac.main.JavaCompiler.instance(task.getContext());
185 ClassWriter writer = ClassWriter.instance(task.getContext());
186 Symtab syms = Symtab.instance(task.getContext());
187 Names names = Names.instance(task.getContext());
188 Attribute.Compound proprietaryAnno =
189 new Attribute.Compound(syms.proprietaryType,
190 List.<Pair<Symbol.MethodSymbol,Attribute>>nil());
191 Attribute.Compound[] profileAnnos = new Attribute.Compound[profiles.getProfileCount() + 1];
192 Symbol.MethodSymbol profileValue = (MethodSymbol) syms.profileType.tsym.members().lookup(names.value).sym;
193 for (int i = 1; i < profileAnnos.length; i++) {
194 profileAnnos[i] = new Attribute.Compound(syms.profileType,
195 List.<Pair<Symbol.MethodSymbol, Attribute>>of(
196 new Pair<Symbol.MethodSymbol, Attribute>(profileValue, new Attribute.Constant(syms.intType, i))));
197 }
198
199 Type.moreInfo = true;
200 Types types = Types.instance(task.getContext());
201 Pool pool = new Pool(types);
202 for (JavaFileObject file : fm.list(jarLocation, "", EnumSet.of(CLASS), true)) {
203 String className = fm.inferBinaryName(jarLocation, file);
204 int index = className.lastIndexOf('.');
205 String pckName = index == -1 ? "" : className.substring(0, index);
206 boolean addLegacyAnnotation = false;
207 if (documented.contains(pckName)) {
208 if (!legacy.contains(pckName))
209 crisp.add(pckName);
210 // System.out.println("Documented: " + className);
211 } else if (legacyProprietary.contains(pckName)) {
212 addLegacyAnnotation = true;
213 // System.out.println("Legacy proprietary: " + className);
214 } else {
215 // System.out.println("Hidden " + className);
216 hiddenPackages.add(pckName);
217 continue;
218 }
219 TypeSymbol sym = (TypeSymbol)compiler.resolveIdent(className);
220 if (sym.kind != Kinds.TYP) {
221 if (className.indexOf('$') < 0) {
222 System.err.println("Ignoring (other) " + className + " : " + sym);
223 System.err.println(" " + sym.getClass().getSimpleName() + " " + sym.type);
224 }
225 continue;
226 }
227 sym.complete();
228 if (sym.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
229 System.err.println("Ignoring (bad) " + sym.getQualifiedName());
230 continue;
231 }
232 ClassSymbol cs = (ClassSymbol) sym;
233 if (addLegacyAnnotation) {
234 cs.prependAttributes(List.of(proprietaryAnno));
235 }
236 int p = profiles.getProfile(cs.fullname.toString().replace(".", "/"));
237 if (0 < p && p < profileAnnos.length)
238 cs.prependAttributes(List.of(profileAnnos[p]));
239 writeClass(pool, cs, writer);
240 }
241
242 if (false) {
243 for (String pckName : crisp)
244 System.out.println("Crisp: " + pckName);
245 for (String pckName : hiddenPackages)
246 System.out.println("Hidden: " + pckName);
247 for (String pckName : legacyProprietary)
248 System.out.println("Legacy proprietary: " + pckName);
249 for (String pckName : documented)
250 System.out.println("Documented: " + pckName);
251 }
252 }
253
254 void writeClass(final Pool pool, final ClassSymbol cs, final ClassWriter writer)
255 throws IOException
256 {
257 try {
258 pool.reset();
259 cs.pool = pool;
260 writer.writeClass(cs);
261 for (Scope.Entry e = cs.members().elems; e != null; e = e.sibling) {
262 if (e.sym.kind == Kinds.TYP) {
263 ClassSymbol nestedClass = (ClassSymbol)e.sym;
264 nestedClass.complete();
265 writeClass(pool, nestedClass, writer);
266 }
267 }
268 } catch (ClassWriter.StringOverflow ex) {
269 throw new RuntimeException(ex);
270 } catch (ClassWriter.PoolOverflow ex) {
271 throw new RuntimeException(ex);
272 }
273 }
274
275 public SourceVersion getSupportedSourceVersion() {
276 return SourceVersion.latest();
277 }
278
279 // used for debugging
280 public static void main(String... args) {
281 String rt_jar = args[0];
282 String dest = args[1];
283 args = new String[] {
284 "-Xbootclasspath:" + rt_jar,
285 "-XDprocess.packages",
286 "-proc:only",
287 "-processor",
288 "com.sun.tools.javac.sym.CreateSymbols",
289 "-Acom.sun.tools.javac.sym.Jar=" + rt_jar,
290 "-Acom.sun.tools.javac.sym.Dest=" + dest,
291 // <editor-fold defaultstate="collapsed">
292 "java.applet",
293 "java.awt",
294 "java.awt.color",
295 "java.awt.datatransfer",
296 "java.awt.dnd",
297 "java.awt.event",
298 "java.awt.font",
299 "java.awt.geom",
300 "java.awt.im",
301 "java.awt.im.spi",
302 "java.awt.image",
303 "java.awt.image.renderable",
304 "java.awt.print",
305 "java.beans",
306 "java.beans.beancontext",
307 "java.io",
308 "java.lang",
309 "java.lang.annotation",
310 "java.lang.instrument",
311 "java.lang.management",
312 "java.lang.ref",
313 "java.lang.reflect",
314 "java.math",
315 "java.net",
316 "java.nio",
317 "java.nio.channels",
318 "java.nio.channels.spi",
319 "java.nio.charset",
320 "java.nio.charset.spi",
321 "java.rmi",
322 "java.rmi.activation",
323 "java.rmi.dgc",
324 "java.rmi.registry",
325 "java.rmi.server",
326 "java.security",
327 "java.security.acl",
328 "java.security.cert",
329 "java.security.interfaces",
330 "java.security.spec",
331 "java.sql",
332 "java.text",
333 "java.text.spi",
334 "java.util",
335 "java.util.concurrent",
336 "java.util.concurrent.atomic",
337 "java.util.concurrent.locks",
338 "java.util.jar",
339 "java.util.logging",
340 "java.util.prefs",
341 "java.util.regex",
342 "java.util.spi",
343 "java.util.zip",
344 "javax.accessibility",
345 "javax.activation",
346 "javax.activity",
347 "javax.annotation",
348 "javax.annotation.processing",
349 "javax.crypto",
350 "javax.crypto.interfaces",
351 "javax.crypto.spec",
352 "javax.imageio",
353 "javax.imageio.event",
354 "javax.imageio.metadata",
355 "javax.imageio.plugins.jpeg",
356 "javax.imageio.plugins.bmp",
357 "javax.imageio.spi",
358 "javax.imageio.stream",
359 "javax.jws",
360 "javax.jws.soap",
361 "javax.lang.model",
362 "javax.lang.model.element",
363 "javax.lang.model.type",
364 "javax.lang.model.util",
365 "javax.management",
366 "javax.management.loading",
367 "javax.management.monitor",
368 "javax.management.relation",
369 "javax.management.openmbean",
370 "javax.management.timer",
371 "javax.management.modelmbean",
372 "javax.management.remote",
373 "javax.management.remote.rmi",
374 "javax.naming",
375 "javax.naming.directory",
376 "javax.naming.event",
377 "javax.naming.ldap",
378 "javax.naming.spi",
379 "javax.net",
380 "javax.net.ssl",
381 "javax.print",
382 "javax.print.attribute",
383 "javax.print.attribute.standard",
384 "javax.print.event",
385 "javax.rmi",
386 "javax.rmi.CORBA",
387 "javax.rmi.ssl",
388 "javax.script",
389 "javax.security.auth",
390 "javax.security.auth.callback",
391 "javax.security.auth.kerberos",
392 "javax.security.auth.login",
393 "javax.security.auth.spi",
394 "javax.security.auth.x500",
395 "javax.security.cert",
396 "javax.security.sasl",
397 "javax.sound.sampled",
398 "javax.sound.sampled.spi",
399 "javax.sound.midi",
400 "javax.sound.midi.spi",
401 "javax.sql",
402 "javax.sql.rowset",
403 "javax.sql.rowset.serial",
404 "javax.sql.rowset.spi",
405 "javax.swing",
406 "javax.swing.border",
407 "javax.swing.colorchooser",
408 "javax.swing.filechooser",
409 "javax.swing.event",
410 "javax.swing.table",
411 "javax.swing.text",
412 "javax.swing.text.html",
413 "javax.swing.text.html.parser",
414 "javax.swing.text.rtf",
415 "javax.swing.tree",
416 "javax.swing.undo",
417 "javax.swing.plaf",
418 "javax.swing.plaf.basic",
419 "javax.swing.plaf.metal",
420 "javax.swing.plaf.multi",
421 "javax.swing.plaf.synth",
422 "javax.tools",
423 "javax.transaction",
424 "javax.transaction.xa",
425 "javax.xml.parsers",
426 "javax.xml.bind",
427 "javax.xml.bind.annotation",
428 "javax.xml.bind.annotation.adapters",
429 "javax.xml.bind.attachment",
430 "javax.xml.bind.helpers",
431 "javax.xml.bind.util",
432 "javax.xml.soap",
433 "javax.xml.ws",
434 "javax.xml.ws.handler",
435 "javax.xml.ws.handler.soap",
436 "javax.xml.ws.http",
437 "javax.xml.ws.soap",
438 "javax.xml.ws.spi",
439 "javax.xml.transform",
440 "javax.xml.transform.sax",
441 "javax.xml.transform.dom",
442 "javax.xml.transform.stax",
443 "javax.xml.transform.stream",
444 "javax.xml",
445 "javax.xml.crypto",
446 "javax.xml.crypto.dom",
447 "javax.xml.crypto.dsig",
448 "javax.xml.crypto.dsig.dom",
449 "javax.xml.crypto.dsig.keyinfo",
450 "javax.xml.crypto.dsig.spec",
451 "javax.xml.datatype",
452 "javax.xml.validation",
453 "javax.xml.namespace",
454 "javax.xml.xpath",
455 "javax.xml.stream",
456 "javax.xml.stream.events",
457 "javax.xml.stream.util",
458 "org.ietf.jgss",
459 "org.omg.CORBA",
460 "org.omg.CORBA.DynAnyPackage",
461 "org.omg.CORBA.ORBPackage",
462 "org.omg.CORBA.TypeCodePackage",
463 "org.omg.stub.java.rmi",
464 "org.omg.CORBA.portable",
465 "org.omg.CORBA_2_3",
466 "org.omg.CORBA_2_3.portable",
467 "org.omg.CosNaming",
468 "org.omg.CosNaming.NamingContextExtPackage",
469 "org.omg.CosNaming.NamingContextPackage",
470 "org.omg.SendingContext",
471 "org.omg.PortableServer",
472 "org.omg.PortableServer.CurrentPackage",
473 "org.omg.PortableServer.POAPackage",
474 "org.omg.PortableServer.POAManagerPackage",
475 "org.omg.PortableServer.ServantLocatorPackage",
476 "org.omg.PortableServer.portable",
477 "org.omg.PortableInterceptor",
478 "org.omg.PortableInterceptor.ORBInitInfoPackage",
479 "org.omg.Messaging",
480 "org.omg.IOP",
481 "org.omg.IOP.CodecFactoryPackage",
482 "org.omg.IOP.CodecPackage",
483 "org.omg.Dynamic",
484 "org.omg.DynamicAny",
485 "org.omg.DynamicAny.DynAnyPackage",
486 "org.omg.DynamicAny.DynAnyFactoryPackage",
487 "org.w3c.dom",
488 "org.w3c.dom.events",
489 "org.w3c.dom.bootstrap",
490 "org.w3c.dom.ls",
491 "org.xml.sax",
492 "org.xml.sax.ext",
493 "org.xml.sax.helpers",
494 "com.sun.java.browser.dom",
495 "org.w3c.dom",
496 "org.w3c.dom.bootstrap",
497 "org.w3c.dom.ls",
498 "org.w3c.dom.ranges",
499 "org.w3c.dom.traversal",
500 "org.w3c.dom.html",
501 "org.w3c.dom.stylesheets",
502 "org.w3c.dom.css",
503 "org.w3c.dom.events",
504 "org.w3c.dom.views",
505 "com.sun.management",
506 "com.sun.security.auth",
507 "com.sun.security.auth.callback",
508 "com.sun.security.auth.login",
509 "com.sun.security.auth.module",
510 "com.sun.security.jgss",
511 "com.sun.net.httpserver",
512 "com.sun.net.httpserver.spi",
513 "javax.smartcardio"
514 // </editor-fold>
515 };
516 com.sun.tools.javac.Main.compile(args);
517 }
518
519 }

mercurial