test/langtools/tools/javac/processing/model/testgetallmembers/Main.java
author pliden
Wed, 20 Nov 2019 10:37:46 +0100
changeset 59151 8babf00875bc
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234382: Test tools/javac/processing/model/testgetallmembers/Main.java using too small heap Reviewed-by: eosterlund, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 41638
diff changeset
     2
 * Copyright (c) 2006, 2017, Oracle and/or its affiliates. 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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3662
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3662
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3662
diff changeset
    21
 * questions.
10
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
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.model
59151
8babf00875bc 8234382: Test tools/javac/processing/model/testgetallmembers/Main.java using too small heap
pliden
parents: 47216
diff changeset
    30
 * @run main/othervm -Xmx512m Main
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.File;
41638
7b8819e22551 8168343: 3 javac tests fail when run on an exploded image
jjg
parents: 40308
diff changeset
    34
import java.nio.file.Path;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.util.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    36
import java.util.Map.Entry;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    37
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.element.ElementKind;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    40
import javax.lang.model.element.ModuleElement;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import javax.lang.model.element.PackageElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import javax.lang.model.util.Elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import javax.tools.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    45
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.source.util.JavacTask;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    47
import com.sun.tools.javac.model.JavacElements;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import static javax.tools.StandardLocation.CLASS_PATH;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import static javax.tools.JavaFileObject.Kind.CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
public class Main {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    public static PackageElement getPackage(TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        Element owner = type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        while (owner.getKind() != ElementKind.PACKAGE)
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            owner = owner.getEnclosingElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        return (PackageElement)owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    static int progress = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    static JavaCompiler tool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    static JavacTask javac;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    static Elements elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37848
diff changeset
    68
    static List<String> addmods_ALL_SYSTEM = Arrays.asList("--add-modules", "ALL-SYSTEM");
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
    69
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    public static void main(String[] args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    72
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    73
            fm.setLocation(CLASS_PATH, Collections.<File>emptyList());
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
    74
            JavacTask javac = (JavacTask)tool.getTask(null, fm, null, addmods_ALL_SYSTEM, null, null);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    75
            Elements elements = javac.getElements();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    77
            final Map<String, Set<String>> packages = new LinkedHashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    79
            int nestedClasses = 0;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    80
            int classes = 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    82
            for (JavaFileObject file : fm.list(PLATFORM_CLASS_PATH, "", EnumSet.of(CLASS), true)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    83
                String type = fm.inferBinaryName(PLATFORM_CLASS_PATH, file);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    84
                if (type.endsWith("package-info"))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                    continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    86
                if (type.endsWith("module-info"))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    87
                    continue;
41638
7b8819e22551 8168343: 3 javac tests fail when run on an exploded image
jjg
parents: 40308
diff changeset
    88
                Path path = fm.asPath(file);
7b8819e22551 8168343: 3 javac tests fail when run on an exploded image
jjg
parents: 40308
diff changeset
    89
                int moduleIndex = path.getNameCount() - type.split("\\Q.\\E").length - 1;
7b8819e22551 8168343: 3 javac tests fail when run on an exploded image
jjg
parents: 40308
diff changeset
    90
                String moduleName = path.getName(moduleIndex).toString();
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 41638
diff changeset
    91
                if (moduleName.startsWith("jdk.incubator.")) //incubator modules not in module graph by default
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 41638
diff changeset
    92
                    continue;
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    93
                try {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    94
                    ModuleElement me = elements.getModuleElement(moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    95
                    me.getClass();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
    96
                    TypeElement elem = ((JavacElements) elements).getTypeElement(me, type);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    97
                    if (elem == null && type.indexOf('$') > 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    98
                        nestedClasses++;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
    99
                        type = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   100
                        continue;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   101
                    }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   102
                    classes++;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   103
                    String pack = getPackage(elem).getQualifiedName().toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   104
                    packages.computeIfAbsent(me.getQualifiedName().toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   105
                                             m -> new LinkedHashSet<>()).add(pack);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   106
                    elem.getKind(); // force completion
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   107
                    type = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   108
                } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   109
                    if (type != null)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   110
                        System.err.println("Looking at " + type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   112
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   113
            javac = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   114
            elements = null;
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   115
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   116
            javac = (JavacTask)tool.getTask(null, fm, null, addmods_ALL_SYSTEM, null, null);
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   117
            elements = javac.getElements();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   118
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   119
            for (Entry<String, Set<String>> module2Packages : packages.entrySet()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   120
                ModuleElement me = elements.getModuleElement(module2Packages.getKey());
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   121
                me.getClass();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   122
                for (String name : module2Packages.getValue()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   123
                    PackageElement pe = ((JavacElements) elements).getPackageElement(me, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   124
                    for (Element e : pe.getEnclosedElements()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   125
                        e.getSimpleName().getClass();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   126
                    }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   127
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   129
            /*
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   130
             * A few sanity checks based on current values:
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   131
             *
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   132
             * packages: 775, classes: 12429 + 5917
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   133
             *
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   134
             * As the platform evolves the numbers are likely to grow
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   135
             * monotonically but in case somebody gets a clever idea for
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   136
             * limiting the number of packages exposed, this number might
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   137
             * drop.  So we test low values.
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   138
             */
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   139
            System.out.format("packages: %s, classes: %s + %s%n",
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   140
                              packages.size(), classes, nestedClasses);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   141
            if (classes < 9000)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   142
                throw new AssertionError("Too few classes in PLATFORM_CLASS_PATH ;-)");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30730
diff changeset
   143
            if (packages.values().stream().flatMap(packs -> packs.stream()).count() < 530)
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   144
                throw new AssertionError("Too few packages in PLATFORM_CLASS_PATH ;-)");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   145
            if (nestedClasses < 3000)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 19509
diff changeset
   146
                throw new AssertionError("Too few nested classes in PLATFORM_CLASS_PATH ;-)");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
}