Main.java revision 2721:f7ce2cfa4cdb
1/* 2 * Copyright (c) 2006, 2014, 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 32import java.io.File; 33import java.util.*; 34import javax.lang.model.element.Element; 35import javax.lang.model.element.ElementKind; 36import javax.lang.model.element.PackageElement; 37import javax.lang.model.element.TypeElement; 38import javax.lang.model.util.Elements; 39import javax.tools.*; 40import com.sun.source.util.JavacTask; 41 42import static javax.tools.StandardLocation.CLASS_PATH; 43import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH; 44import static javax.tools.JavaFileObject.Kind.CLASS; 45 46 47public 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 JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 63 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 64 fm.setLocation(CLASS_PATH, Collections.<File>emptyList()); 65 JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); 66 Elements elements = javac.getElements(); 67 68 final Set<String> packages = new LinkedHashSet<String>(); 69 70 int nestedClasses = 0; 71 int classes = 0; 72 73 for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) { 74 String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file); 75 if (type.endsWith("package-info")) 76 continue; 77 try { 78 TypeElement elem = elements.getTypeElement(type); 79 if (elem == null && type.indexOf('$') > 0) { 80 nestedClasses++; 81 type = null; 82 continue; 83 } 84 classes++; 85 packages.add(getPackage(elem).getQualifiedName().toString()); 86 elements.getTypeElement(type).getKind(); // force completion 87 type = null; 88 } finally { 89 if (type != null) 90 System.err.println("Looking at " + type); 91 } 92 } 93 javac = null; 94 elements = null; 95 96 javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); 97 elements = javac.getElements(); 98 99 for (String name : packages) { 100 PackageElement pe = elements.getPackageElement(name); 101 for (Element e : pe.getEnclosedElements()) { 102 e.getSimpleName().getClass(); 103 } 104 } 105 /* 106 * A few sanity checks based on current values: 107 * 108 * packages: 775, classes: 12429 + 5917 109 * 110 * As the platform evolves the numbers are likely to grow 111 * monotonically but in case somebody gets a clever idea for 112 * limiting the number of packages exposed, this number might 113 * drop. So we test low values. 114 */ 115 System.out.format("packages: %s, classes: %s + %s%n", 116 packages.size(), classes, nestedClasses); 117 if (classes < 9000) 118 throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)"); 119 if (packages.size() < 530) 120 throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)"); 121 if (nestedClasses < 3000) 122 throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)"); 123 } 124 } 125} 126