Sat, 01 Dec 2007 00:00:00 +0000
Initial load
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
duke@1 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 21 | * have any questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /** |
duke@1 | 25 | * @test |
duke@1 | 26 | * @bug 6374357 6308351 |
duke@1 | 27 | * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException |
duke@1 | 28 | * @author Peter von der Ah\u00e9 |
duke@1 | 29 | * @run main/othervm -Xmx256m Main |
duke@1 | 30 | */ |
duke@1 | 31 | |
duke@1 | 32 | import java.io.File; |
duke@1 | 33 | import java.util.*; |
duke@1 | 34 | import javax.lang.model.SourceVersion; |
duke@1 | 35 | import javax.lang.model.element.Element; |
duke@1 | 36 | import javax.lang.model.element.ElementKind; |
duke@1 | 37 | import javax.lang.model.element.PackageElement; |
duke@1 | 38 | import javax.lang.model.element.TypeElement; |
duke@1 | 39 | import javax.lang.model.util.Elements; |
duke@1 | 40 | import javax.tools.*; |
duke@1 | 41 | import com.sun.source.util.JavacTask; |
duke@1 | 42 | |
duke@1 | 43 | import static javax.tools.StandardLocation.CLASS_PATH; |
duke@1 | 44 | import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH; |
duke@1 | 45 | import static javax.tools.JavaFileObject.Kind.CLASS; |
duke@1 | 46 | |
duke@1 | 47 | |
duke@1 | 48 | public class Main { |
duke@1 | 49 | |
duke@1 | 50 | public static PackageElement getPackage(TypeElement type) { |
duke@1 | 51 | Element owner = type; |
duke@1 | 52 | while (owner.getKind() != ElementKind.PACKAGE) |
duke@1 | 53 | owner = owner.getEnclosingElement(); |
duke@1 | 54 | return (PackageElement)owner; |
duke@1 | 55 | } |
duke@1 | 56 | |
duke@1 | 57 | static int progress = 0; |
duke@1 | 58 | static JavaCompiler tool; |
duke@1 | 59 | static JavacTask javac; |
duke@1 | 60 | static Elements elements; |
duke@1 | 61 | |
duke@1 | 62 | public static void main(String[] args) throws Exception { |
duke@1 | 63 | |
duke@1 | 64 | JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
duke@1 | 65 | StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
duke@1 | 66 | fm.setLocation(CLASS_PATH, Collections.<File>emptyList()); |
duke@1 | 67 | JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null); |
duke@1 | 68 | Elements elements = javac.getElements(); |
duke@1 | 69 | |
duke@1 | 70 | final Set<String> packages = new LinkedHashSet<String>(); |
duke@1 | 71 | |
duke@1 | 72 | int nestedClasses = 0; |
duke@1 | 73 | int classes = 0; |
duke@1 | 74 | |
duke@1 | 75 | for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) { |
duke@1 | 76 | String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file); |
duke@1 | 77 | if (type.endsWith("package-info")) |
duke@1 | 78 | continue; |
duke@1 | 79 | try { |
duke@1 | 80 | TypeElement elem = elements.getTypeElement(type); |
duke@1 | 81 | if (elem == null && type.indexOf('$') > 0) { |
duke@1 | 82 | nestedClasses++; |
duke@1 | 83 | type = null; |
duke@1 | 84 | continue; |
duke@1 | 85 | } |
duke@1 | 86 | classes++; |
duke@1 | 87 | packages.add(getPackage(elem).getQualifiedName().toString()); |
duke@1 | 88 | elements.getTypeElement(type).getKind(); // force completion |
duke@1 | 89 | type = null; |
duke@1 | 90 | } finally { |
duke@1 | 91 | if (type != null) |
duke@1 | 92 | System.err.println("Looking at " + type); |
duke@1 | 93 | } |
duke@1 | 94 | } |
duke@1 | 95 | javac = null; |
duke@1 | 96 | elements = null; |
duke@1 | 97 | |
duke@1 | 98 | javac = (JavacTask)tool.getTask(null, null, null, null, null, null); |
duke@1 | 99 | elements = javac.getElements(); |
duke@1 | 100 | |
duke@1 | 101 | for (String name : packages) { |
duke@1 | 102 | PackageElement pe = elements.getPackageElement(name); |
duke@1 | 103 | for (Element e : pe.getEnclosedElements()) { |
duke@1 | 104 | e.getSimpleName().getClass(); |
duke@1 | 105 | } |
duke@1 | 106 | } |
duke@1 | 107 | /* |
duke@1 | 108 | * A few sanity checks based on current values: |
duke@1 | 109 | * |
duke@1 | 110 | * packages: 775, classes: 12429 + 5917 |
duke@1 | 111 | * |
duke@1 | 112 | * As the platform evolves the numbers are likely to grow |
duke@1 | 113 | * monotonically but in case somebody gets a clever idea for |
duke@1 | 114 | * limiting the number of packages exposed, this number might |
duke@1 | 115 | * drop. So we test low values. |
duke@1 | 116 | */ |
duke@1 | 117 | System.out.format("packages: %s, classes: %s + %s%n", |
duke@1 | 118 | packages.size(), classes, nestedClasses); |
duke@1 | 119 | if (classes < 9000) |
duke@1 | 120 | throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)"); |
duke@1 | 121 | if (packages.size() < 545) |
duke@1 | 122 | throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)"); |
duke@1 | 123 | if (nestedClasses < 3000) |
duke@1 | 124 | throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)"); |
duke@1 | 125 | } |
duke@1 | 126 | } |