Main.java revision 228:03bcd66bd8e7
11590Srgrimes/* 21590Srgrimes * Copyright 2006-2009 Sun Microsystems, Inc. All Rights Reserved. 31590Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41590Srgrimes * 51590Srgrimes * This code is free software; you can redistribute it and/or modify it 61590Srgrimes * under the terms of the GNU General Public License version 2 only, as 71590Srgrimes * published by the Free Software Foundation. 81590Srgrimes * 91590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131590Srgrimes * accompanied this code). 141590Srgrimes * 151590Srgrimes * You should have received a copy of the GNU General Public License version 161590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181590Srgrimes * 191590Srgrimes * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 201590Srgrimes * CA 95054 USA or visit www.sun.com if you need additional information or 211590Srgrimes * have any questions. 221590Srgrimes */ 231590Srgrimes 241590Srgrimes/** 251590Srgrimes * @test 261590Srgrimes * @bug 6374357 6308351 6707027 271590Srgrimes * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException 281590Srgrimes * @author Peter von der Ah\u00e9 291590Srgrimes * @run main/othervm -Xmx256m Main 301590Srgrimes */ 311590Srgrimes 321590Srgrimesimport java.io.File; 331590Srgrimesimport java.util.*; 341590Srgrimesimport javax.lang.model.SourceVersion; 351590Srgrimesimport javax.lang.model.element.Element; 361590Srgrimesimport javax.lang.model.element.ElementKind; 371590Srgrimesimport javax.lang.model.element.PackageElement; 3827270Scharnierimport javax.lang.model.element.TypeElement; 391590Srgrimesimport javax.lang.model.util.Elements; 401590Srgrimesimport javax.tools.*; 411590Srgrimesimport com.sun.source.util.JavacTask; 421590Srgrimes 431590Srgrimesimport static javax.tools.StandardLocation.CLASS_PATH; 4427270Scharnierimport static javax.tools.StandardLocation.PLATFORM_CLASS_PATH; 451590Srgrimesimport static javax.tools.JavaFileObject.Kind.CLASS; 4627270Scharnier 471590Srgrimes 481590Srgrimespublic class Main { 4987751Scharnier 5087751Scharnier public static PackageElement getPackage(TypeElement type) { 5187751Scharnier Element owner = type; 5294978Stjr while (owner.getKind() != ElementKind.PACKAGE) 5327270Scharnier owner = owner.getEnclosingElement(); 5494978Stjr return (PackageElement)owner; 5595033Sache } 561590Srgrimes 5727270Scharnier static int progress = 0; 5894978Stjr static JavaCompiler tool; 5927270Scharnier static JavacTask javac; 601590Srgrimes static Elements elements; 611590Srgrimes 621590Srgrimes public static void main(String[] args) throws Exception { 6392920Simp 6494978Stjr JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 6592920Simp StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 6627270Scharnier fm.setLocation(CLASS_PATH, Collections.<File>emptyList()); 6794978Stjr JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); 6894978Stjr Elements elements = javac.getElements(); 6994978Stjr 7027270Scharnier final Set<String> packages = new LinkedHashSet<String>(); 71102944Sdwmalone 721590Srgrimes int nestedClasses = 0; 73102944Sdwmalone int classes = 0; 7497266Stjr 751590Srgrimes for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) { 761590Srgrimes String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file); 7795033Sache if (type.endsWith("package-info")) 7895033Sache continue; 791590Srgrimes try { 8094978Stjr TypeElement elem = elements.getTypeElement(type); 811590Srgrimes if (elem == null && type.indexOf('$') > 0) { 8294978Stjr nestedClasses++; 8394978Stjr type = null; 8494978Stjr continue; 8594978Stjr } 8694978Stjr classes++; 8794978Stjr packages.add(getPackage(elem).getQualifiedName().toString()); 881590Srgrimes elements.getTypeElement(type).getKind(); // force completion 891590Srgrimes type = null; 9027270Scharnier } finally { 911590Srgrimes if (type != null) 921590Srgrimes System.err.println("Looking at " + type); 931590Srgrimes } 941590Srgrimes } 951590Srgrimes javac = null; 961590Srgrimes elements = null; 971590Srgrimes 981590Srgrimes javac = (JavacTask)tool.getTask(null, null, null, null, null, null); 991590Srgrimes elements = javac.getElements(); 1001590Srgrimes 1011590Srgrimes for (String name : packages) { 1021590Srgrimes PackageElement pe = elements.getPackageElement(name); 1031590Srgrimes for (Element e : pe.getEnclosedElements()) { 10427270Scharnier e.getSimpleName().getClass(); 1051590Srgrimes } 1061590Srgrimes } 1071590Srgrimes /* 1081590Srgrimes * A few sanity checks based on current values: 1091590Srgrimes * 1101590Srgrimes * packages: 775, classes: 12429 + 5917 11197266Stjr * 1121590Srgrimes * As the platform evolves the numbers are likely to grow 1131590Srgrimes * monotonically but in case somebody gets a clever idea for 1141590Srgrimes * limiting the number of packages exposed, this number might 1151590Srgrimes * drop. So we test low values. 11697266Stjr */ 11797266Stjr System.out.format("packages: %s, classes: %s + %s%n", 1181590Srgrimes packages.size(), classes, nestedClasses); 1191590Srgrimes if (classes < 9000) 12097266Stjr throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)"); 1211590Srgrimes if (packages.size() < 530) 1221590Srgrimes throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)"); 12327270Scharnier if (nestedClasses < 3000) 124102944Sdwmalone throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)"); 12527270Scharnier } 12694978Stjr} 12727270Scharnier