Main.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2006, 2016, 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 * @modules jdk.compiler/com.sun.tools.javac.model 30 * @run main/othervm -Xmx256m Main 31 */ 32 33import java.io.File; 34import java.util.*; 35import java.util.Map.Entry; 36import java.util.stream.StreamSupport; 37 38import javax.lang.model.element.Element; 39import javax.lang.model.element.ElementKind; 40import javax.lang.model.element.ModuleElement; 41import javax.lang.model.element.PackageElement; 42import javax.lang.model.element.TypeElement; 43import javax.lang.model.util.Elements; 44import javax.tools.*; 45import javax.tools.JavaFileManager.Location; 46 47import com.sun.source.util.JavacTask; 48import com.sun.tools.javac.model.JavacElements; 49 50import static javax.tools.StandardLocation.CLASS_PATH; 51import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH; 52import static javax.tools.StandardLocation.SYSTEM_MODULES; 53import static javax.tools.JavaFileObject.Kind.CLASS; 54 55 56public class Main { 57 58 public static PackageElement getPackage(TypeElement type) { 59 Element owner = type; 60 while (owner.getKind() != ElementKind.PACKAGE) 61 owner = owner.getEnclosingElement(); 62 return (PackageElement)owner; 63 } 64 65 static int progress = 0; 66 static JavaCompiler tool; 67 static JavacTask javac; 68 static Elements elements; 69 70 public static void main(String[] args) throws Exception { 71 JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 72 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 73 fm.setLocation(CLASS_PATH, Collections.<File>emptyList()); 74 JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); 75 Elements elements = javac.getElements(); 76 77 final Map<String, Set<String>> packages = new LinkedHashMap<>(); 78 79 int nestedClasses = 0; 80 int classes = 0; 81 82 for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) { 83 String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file); 84 if (type.endsWith("package-info")) 85 continue; 86 if (type.endsWith("module-info")) 87 continue; 88 String moduleName = fm.asPath(file).getName(1).toString(); 89 try { 90 ModuleElement me = elements.getModuleElement(moduleName); 91 me.getClass(); 92 TypeElement elem = ((JavacElements) elements).getTypeElement(me, type); 93 if (elem == null && type.indexOf('$') > 0) { 94 nestedClasses++; 95 type = null; 96 continue; 97 } 98 classes++; 99 String pack = getPackage(elem).getQualifiedName().toString(); 100 packages.computeIfAbsent(me.getQualifiedName().toString(), 101 m -> new LinkedHashSet<>()).add(pack); 102 elem.getKind(); // force completion 103 type = null; 104 } finally { 105 if (type != null) 106 System.err.println("Looking at " + type); 107 } 108 } 109 javac = null; 110 elements = null; 111 112 javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); 113 elements = javac.getElements(); 114 115 for (Entry<String, Set<String>> module2Packages : packages.entrySet()) { 116 ModuleElement me = elements.getModuleElement(module2Packages.getKey()); 117 for (String name : module2Packages.getValue()) { 118 PackageElement pe = ((JavacElements) elements).getPackageElement(me, name); 119 for (Element e : pe.getEnclosedElements()) { 120 e.getSimpleName().getClass(); 121 } 122 } 123 } 124 /* 125 * A few sanity checks based on current values: 126 * 127 * packages: 775, classes: 12429 + 5917 128 * 129 * As the platform evolves the numbers are likely to grow 130 * monotonically but in case somebody gets a clever idea for 131 * limiting the number of packages exposed, this number might 132 * drop. So we test low values. 133 */ 134 System.out.format("packages: %s, classes: %s + %s%n", 135 packages.size(), classes, nestedClasses); 136 if (classes < 9000) 137 throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)"); 138 if (packages.values().stream().flatMap(packs -> packs.stream()).count() < 530) 139 throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)"); 140 if (nestedClasses < 3000) 141 throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)"); 142 } 143 } 144} 145