test/tools/javac/processing/model/testgetallmembers/Main.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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @bug 6374357 6308351 6707027
27 * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException
28 * @author Peter von der Ah\u00e9
29 * @run main/othervm -Xmx256m Main
30 */
31
32 import java.io.File;
33 import java.util.*;
34 import javax.lang.model.element.Element;
35 import javax.lang.model.element.ElementKind;
36 import javax.lang.model.element.PackageElement;
37 import javax.lang.model.element.TypeElement;
38 import javax.lang.model.util.Elements;
39 import javax.tools.*;
40 import com.sun.source.util.JavacTask;
41
42 import static javax.tools.StandardLocation.CLASS_PATH;
43 import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
44 import static javax.tools.JavaFileObject.Kind.CLASS;
45
46
47 public class Main {
48
49 public static PackageElement getPackage(TypeElement type) {
50 Element owner = type;
51 while (owner.getKind() != ElementKind.PACKAGE)
52 owner = owner.getEnclosingElement();
53 return (PackageElement)owner;
54 }
55
56 static int progress = 0;
57 static JavaCompiler tool;
58 static JavacTask javac;
59 static Elements elements;
60
61 public static void main(String[] args) throws Exception {
62 if (haveAltRt()) {
63 System.out.println("Warning: alt-rt.jar detected, test skipped");
64 return;
65 }
66 JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
67 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
68 fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
69 JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
70 Elements elements = javac.getElements();
71
72 final Set<String> packages = new LinkedHashSet<String>();
73
74 int nestedClasses = 0;
75 int classes = 0;
76
77 for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
78 String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
79 if (type.endsWith("package-info"))
80 continue;
81 try {
82 TypeElement elem = elements.getTypeElement(type);
83 if (elem == null && type.indexOf('$') > 0) {
84 nestedClasses++;
85 type = null;
86 continue;
87 }
88 classes++;
89 packages.add(getPackage(elem).getQualifiedName().toString());
90 elements.getTypeElement(type).getKind(); // force completion
91 type = null;
92 } finally {
93 if (type != null)
94 System.err.println("Looking at " + type);
95 }
96 }
97 javac = null;
98 elements = null;
99
100 javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
101 elements = javac.getElements();
102
103 for (String name : packages) {
104 PackageElement pe = elements.getPackageElement(name);
105 for (Element e : pe.getEnclosedElements()) {
106 e.getSimpleName().getClass();
107 }
108 }
109 /*
110 * A few sanity checks based on current values:
111 *
112 * packages: 775, classes: 12429 + 5917
113 *
114 * As the platform evolves the numbers are likely to grow
115 * monotonically but in case somebody gets a clever idea for
116 * limiting the number of packages exposed, this number might
117 * drop. So we test low values.
118 */
119 System.out.format("packages: %s, classes: %s + %s%n",
120 packages.size(), classes, nestedClasses);
121 if (classes < 9000)
122 throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
123 if (packages.size() < 530)
124 throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
125 if (nestedClasses < 3000)
126 throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
127 }
128 /*
129 * If -XX:+AggressiveOpts has been used to test, the option currently
130 * instructs the VM to prepend alt-rt.jar onto the bootclasspath. This
131 * overrides the default TreeMap implemation in rt.jar causing symbol
132 * resolution problems (caused by inconsistent inner class), although
133 * alt-rt.jar is being eliminated, we have this sanity check to detect this
134 * case and skip the test.
135 */
136 static boolean haveAltRt() {
137 String bootClassPath = System.getProperty("sun.boot.class.path");
138 for (String cp : bootClassPath.split(File.pathSeparator)) {
139 if (cp.endsWith("alt-rt.jar")) {
140 System.err.println("Warning: detected alt-rt.jar in "
141 + "sun.boot.class.path");
142 return true;
143 }
144 }
145 return false;
146 }
147 }

mercurial