1 /*
   2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug     6374357 6308351
  27  * @summary PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException
  28  * @author  Peter von der Ah\u00e9
  29  * @run main/othervm -Xmx256m Main
  30  */
  31 
  32 import java.io.File;
  33 import java.util.*;
  34 import javax.lang.model.SourceVersion;
  35 import javax.lang.model.element.Element;
  36 import javax.lang.model.element.ElementKind;
  37 import javax.lang.model.element.PackageElement;
  38 import javax.lang.model.element.TypeElement;
  39 import javax.lang.model.util.Elements;
  40 import javax.tools.*;
  41 import com.sun.source.util.JavacTask;
  42 
  43 import static javax.tools.StandardLocation.CLASS_PATH;
  44 import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
  45 import static javax.tools.JavaFileObject.Kind.CLASS;
  46 
  47 
  48 public class Main {
  49 
  50     public static PackageElement getPackage(TypeElement type) {
  51         Element owner = type;
  52         while (owner.getKind() != ElementKind.PACKAGE)
  53             owner = owner.getEnclosingElement();
  54         return (PackageElement)owner;
  55     }
  56 
  57     static int progress = 0;
  58     static JavaCompiler tool;
  59     static JavacTask javac;
  60     static Elements elements;
  61 
  62     public static void main(String[] args) throws Exception {
  63 
  64         JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
  65         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
  66         fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
  67         JavacTask javac = (JavacTask)tool.getTask(null, fm, null, null, null, null);
  68         Elements elements = javac.getElements();
  69 
  70         final Set<String> packages = new LinkedHashSet<String>();
  71 
  72         int nestedClasses = 0;
  73         int classes = 0;
  74 
  75         for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
  76             String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
  77             if (type.endsWith("package-info"))
  78                 continue;
  79             try {
  80                 TypeElement elem = elements.getTypeElement(type);
  81                 if (elem == null && type.indexOf('$') > 0) {
  82                     nestedClasses++;
  83                     type = null;
  84                     continue;
  85                 }
  86                 classes++;
  87                 packages.add(getPackage(elem).getQualifiedName().toString());
  88                 elements.getTypeElement(type).getKind(); // force completion
  89                 type = null;
  90             } finally {
  91                 if (type != null)
  92                     System.err.println("Looking at " + type);
  93             }
  94         }
  95         javac = null;
  96         elements = null;
  97 
  98         javac = (JavacTask)tool.getTask(null, null, null, null, null, null);
  99         elements = javac.getElements();
 100 
 101         for (String name : packages) {
 102             PackageElement pe = elements.getPackageElement(name);
 103             for (Element e : pe.getEnclosedElements()) {
 104                 e.getSimpleName().getClass();
 105             }
 106         }
 107         /*
 108          * A few sanity checks based on current values:
 109          *
 110          * packages: 775, classes: 12429 + 5917
 111          *
 112          * As the platform evolves the numbers are likely to grow
 113          * monotonically but in case somebody gets a clever idea for
 114          * limiting the number of packages exposed, this number might
 115          * drop.  So we test low values.
 116          */
 117         System.out.format("packages: %s, classes: %s + %s%n",
 118                           packages.size(), classes, nestedClasses);
 119         if (classes < 9000)
 120             throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
 121         if (packages.size() < 545)
 122             throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
 123         if (nestedClasses < 3000)
 124             throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
 125     }
 126 }