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