0N/A/*
553N/A * Copyright (c) 2006, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
207N/A * @bug 6374357 6308351 6707027
0N/A * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException
0N/A * @author Peter von der Ah\u00e9
0N/A * @run main/othervm -Xmx256m Main
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.util.*;
0N/Aimport javax.lang.model.SourceVersion;
0N/Aimport javax.lang.model.element.Element;
0N/Aimport javax.lang.model.element.ElementKind;
0N/Aimport javax.lang.model.element.PackageElement;
0N/Aimport javax.lang.model.element.TypeElement;
0N/Aimport javax.lang.model.util.Elements;
0N/Aimport javax.tools.*;
0N/Aimport com.sun.source.util.JavacTask;
0N/A
0N/Aimport static javax.tools.StandardLocation.CLASS_PATH;
0N/Aimport static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
0N/Aimport static javax.tools.JavaFileObject.Kind.CLASS;
0N/A
0N/A
0N/Apublic class Main {
0N/A
0N/A public static PackageElement getPackage(TypeElement type) {
0N/A Element owner = type;
0N/A while (owner.getKind() != ElementKind.PACKAGE)
0N/A owner = owner.getEnclosingElement();
0N/A return (PackageElement)owner;
0N/A }
0N/A
0N/A static int progress = 0;
0N/A static JavaCompiler tool;
0N/A static JavacTask javac;
0N/A static Elements elements;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
0N/A StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
0N/A fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
0N/A JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
0N/A Elements elements = javac.getElements();
0N/A
0N/A final Set<String> packages = new LinkedHashSet<String>();
0N/A
0N/A int nestedClasses = 0;
0N/A int classes = 0;
0N/A
0N/A for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
0N/A String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
0N/A if (type.endsWith("package-info"))
0N/A continue;
0N/A try {
0N/A TypeElement elem = elements.getTypeElement(type);
0N/A if (elem == null && type.indexOf('$') > 0) {
0N/A nestedClasses++;
0N/A type = null;
0N/A continue;
0N/A }
0N/A classes++;
0N/A packages.add(getPackage(elem).getQualifiedName().toString());
0N/A elements.getTypeElement(type).getKind(); // force completion
0N/A type = null;
0N/A } finally {
0N/A if (type != null)
0N/A System.err.println("Looking at " + type);
0N/A }
0N/A }
0N/A javac = null;
0N/A elements = null;
0N/A
377N/A javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
0N/A elements = javac.getElements();
0N/A
0N/A for (String name : packages) {
0N/A PackageElement pe = elements.getPackageElement(name);
0N/A for (Element e : pe.getEnclosedElements()) {
0N/A e.getSimpleName().getClass();
0N/A }
0N/A }
0N/A /*
0N/A * A few sanity checks based on current values:
0N/A *
0N/A * packages: 775, classes: 12429 + 5917
0N/A *
0N/A * As the platform evolves the numbers are likely to grow
0N/A * monotonically but in case somebody gets a clever idea for
0N/A * limiting the number of packages exposed, this number might
0N/A * drop. So we test low values.
0N/A */
0N/A System.out.format("packages: %s, classes: %s + %s%n",
0N/A packages.size(), classes, nestedClasses);
0N/A if (classes < 9000)
0N/A throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
207N/A if (packages.size() < 530)
0N/A throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
0N/A if (nestedClasses < 3000)
0N/A throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
0N/A }
0N/A}