test/langtools/tools/javac/api/TestModuleUnnamedPackage.java
author jjg
Wed, 13 Nov 2019 10:33:00 -0800
changeset 59058 3f57daa2d6eb
child 59096 e6baa47fbacf
permissions -rw-r--r--
8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59058
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     1
/*
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     4
 *
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     8
 *
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    13
 * accompanied this code).
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    14
 *
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    18
 *
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    21
 * questions.
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    22
 */
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    23
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    24
/*
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    25
 * @test
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    26
 * @bug     8234025
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    27
 * @summary Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    28
 * @modules jdk.compiler
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    29
 * @library /tools/lib /tools/javac/lib
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    30
 * @build toolbox.ModuleBuilder toolbox.ToolBox
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    31
 * @run main TestModuleUnnamedPackage
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    32
 */
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    33
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    34
import java.io.IOException;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    35
import java.nio.file.Path;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    36
import java.util.List;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    37
import java.util.Set;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    38
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    39
import javax.annotation.processing.RoundEnvironment;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    40
import javax.lang.model.element.ModuleElement;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    41
import javax.lang.model.element.PackageElement;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    42
import javax.lang.model.element.TypeElement;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    43
import javax.lang.model.util.Elements;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    44
import javax.tools.JavaCompiler;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    45
import javax.tools.JavaFileObject;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    46
import javax.tools.StandardJavaFileManager;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    47
import javax.tools.StandardLocation;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    48
import javax.tools.ToolProvider;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    49
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    50
import com.sun.source.util.JavacTask;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    51
import toolbox.Assert;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    52
import toolbox.ToolBox;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    53
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    54
public class TestModuleUnnamedPackage extends JavacTestingAbstractProcessor {
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    55
    public static void main(String... args) throws IOException {
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    56
        TestModuleUnnamedPackage t = new TestModuleUnnamedPackage();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    57
            t.run();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    58
    }
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    59
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    60
    void run() throws IOException {
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    61
        ToolBox tb = new ToolBox();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    62
        Path src = Path.of("src");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    63
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    64
        tb.writeJavaFiles(src, "module m { exports p; }",
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    65
                "package p; public class C { }");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    66
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    67
        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    68
        StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    69
        fm.setLocationFromPaths(StandardLocation.SOURCE_PATH, List.of(src));
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    70
        List<String> options = List.of("-proc:only");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    71
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(tb.findJavaFiles(src));
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    72
        JavacTask t = (JavacTask) c.getTask(null, fm, null, null, null, files);
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    73
        t.setProcessors(List.of(this));
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    74
        t.call();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    75
    }
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    76
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    77
    public boolean process(Set<? extends TypeElement> elems, RoundEnvironment rEnv) {
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    78
        if (rEnv.processingOver()) {
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    79
            Elements elements = processingEnv.getElementUtils();
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    80
            ModuleElement m = elements.getModuleElement("m");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    81
            PackageElement unnamed = elements.getPackageElement(m, "");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    82
            System.out.println("module m: " + m);
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    83
            System.out.println("module m, unnamed package: " + unnamed);
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    84
            Assert.checkNonNull(unnamed, "unnamed package in module m");
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    85
        }
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    86
        return true;
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    87
    }
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    88
}
3f57daa2d6eb 8234025: Elements.getPackageElement(ModuleElement,CharSequence) returns null for unnamed package
jjg
parents:
diff changeset
    89