Main.java revision 3376:4c740bddc648
1169689Skan/*
2169689Skan * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
3169689Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169689Skan *
5169689Skan * This code is free software; you can redistribute it and/or modify it
6169689Skan * under the terms of the GNU General Public License version 2 only, as
7169689Skan * published by the Free Software Foundation.
8169689Skan *
9169689Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10169689Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11169689Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12169689Skan * version 2 for more details (a copy is included in the LICENSE file that
13169689Skan * accompanied this code).
14169689Skan *
15169689Skan * You should have received a copy of the GNU General Public License version
16169689Skan * 2 along with this work; if not, write to the Free Software Foundation,
17169689Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169689Skan *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169689Skan * or visit www.oracle.com if you need additional information or have any
21169689Skan * questions.
22169689Skan */
23169689Skan
24169689Skan/**
25169689Skan * @test
26169689Skan * @bug     6374357 6308351 6707027
27169689Skan * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException
28169689Skan * @author  Peter von der Ah\u00e9
29169689Skan * @modules jdk.compiler/com.sun.tools.javac.model
30169689Skan * @run main/othervm -Xmx256m Main
31169689Skan */
32169689Skan
33169689Skanimport java.io.File;
34169689Skanimport java.util.*;
35169689Skanimport java.util.Map.Entry;
36169689Skan
37169689Skanimport javax.lang.model.element.Element;
38169689Skanimport javax.lang.model.element.ElementKind;
39169689Skanimport javax.lang.model.element.ModuleElement;
40169689Skanimport javax.lang.model.element.PackageElement;
41169689Skanimport javax.lang.model.element.TypeElement;
42169689Skanimport javax.lang.model.util.Elements;
43169689Skanimport javax.tools.*;
44169689Skan
45169689Skanimport com.sun.source.util.JavacTask;
46import com.sun.tools.javac.model.JavacElements;
47
48import static javax.tools.StandardLocation.CLASS_PATH;
49import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
50import static javax.tools.JavaFileObject.Kind.CLASS;
51
52
53public class Main {
54
55    public static PackageElement getPackage(TypeElement type) {
56        Element owner = type;
57        while (owner.getKind() != ElementKind.PACKAGE)
58            owner = owner.getEnclosingElement();
59        return (PackageElement)owner;
60    }
61
62    static int progress = 0;
63    static JavaCompiler tool;
64    static JavacTask javac;
65    static Elements elements;
66
67    static List<String> addmods_ALL_SYSTEM = Arrays.asList("-addmods", "ALL-SYSTEM");
68
69    public static void main(String[] args) throws Exception {
70        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
71        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
72            fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
73            JavacTask javac = (JavacTask)tool.getTask(null, fm, null, addmods_ALL_SYSTEM, null, null);
74            Elements elements = javac.getElements();
75
76            final Map<String, Set<String>> packages = new LinkedHashMap<>();
77
78            int nestedClasses = 0;
79            int classes = 0;
80
81            for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
82                String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
83                if (type.endsWith("package-info"))
84                    continue;
85                if (type.endsWith("module-info"))
86                    continue;
87                String moduleName = fm.asPath(file).getName(1).toString();
88                try {
89                    ModuleElement me = elements.getModuleElement(moduleName);
90                    me.getClass();
91                    TypeElement elem = ((JavacElements) elements).getTypeElement(me, type);
92                    if (elem == null && type.indexOf('$') > 0) {
93                        nestedClasses++;
94                        type = null;
95                        continue;
96                    }
97                    classes++;
98                    String pack = getPackage(elem).getQualifiedName().toString();
99                    packages.computeIfAbsent(me.getQualifiedName().toString(),
100                                             m -> new LinkedHashSet<>()).add(pack);
101                    elem.getKind(); // force completion
102                    type = null;
103                } finally {
104                    if (type != null)
105                        System.err.println("Looking at " + type);
106                }
107            }
108            javac = null;
109            elements = null;
110
111            javac = (JavacTask)tool.getTask(null, fm, null, addmods_ALL_SYSTEM, null, null);
112            elements = javac.getElements();
113
114            for (Entry<String, Set<String>> module2Packages : packages.entrySet()) {
115                ModuleElement me = elements.getModuleElement(module2Packages.getKey());
116                me.getClass();
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