1 /*
   2  * Copyright (c) 2006, 2016, 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  * @modules jdk.compiler/com.sun.tools.javac.model
  30  * @run main/othervm -Xmx256m Main
  31  */
  32 
  33 import java.io.File;
  34 import java.util.*;
  35 import java.util.Map.Entry;
  36 
  37 import javax.lang.model.element.Element;
  38 import javax.lang.model.element.ElementKind;
  39 import javax.lang.model.element.ModuleElement;
  40 import javax.lang.model.element.PackageElement;
  41 import javax.lang.model.element.TypeElement;
  42 import javax.lang.model.util.Elements;
  43 import javax.tools.*;
  44 
  45 import com.sun.source.util.JavacTask;
  46 import com.sun.tools.javac.model.JavacElements;
  47 
  48 import static javax.tools.StandardLocation.CLASS_PATH;
  49 import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
  50 import static javax.tools.JavaFileObject.Kind.CLASS;
  51 
  52 
  53 public 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("--add-modules", "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 }