langtools/test/tools/javac/modules/AnnotationProcessing.java
author alanb
Thu, 17 Mar 2016 19:04:28 +0000
changeset 36526 3b41f1c69604
child 36778 e04318f39f92
permissions -rw-r--r--
8142968: Module System implementation Summary: Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Reviewed-by: jjg, jlahoda, vromero, mcimadamore, bpatel, ksrini, darcy, anazarov, dfuchs Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, jan.lahoda@oracle.com, vicente.romero@oracle.com, andreas.lundblad@oracle.com, andrey.x.nazarov@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, kumar.x.srinivasan@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Verify that annotation processing works.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @library /tools/lib
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @modules
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 *      jdk.compiler/com.sun.tools.javac.api
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *      jdk.compiler/com.sun.tools.javac.main
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *      jdk.jdeps/com.sun.tools.javap
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @build ToolBox ModuleTestBase
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 * @run main AnnotationProcessing
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Objects;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import javax.annotation.processing.AbstractProcessor;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.annotation.processing.RoundEnvironment;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.annotation.processing.SupportedAnnotationTypes;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.annotation.processing.SupportedOptions;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import javax.lang.model.element.Element;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import javax.lang.model.element.ModuleElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import javax.lang.model.element.ModuleElement.ProvidesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import javax.lang.model.element.ModuleElement.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import javax.lang.model.element.PackageElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import javax.lang.model.element.TypeElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import javax.lang.model.type.TypeKind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import javax.lang.model.util.ElementFilter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import javax.lang.model.util.ElementScanner9;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
public class AnnotationProcessing extends ModuleTestBase {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    public static void main(String... args) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        new AnnotationProcessing().runTests();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    void testAPSingleModule(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                          "package impl; public class Impl { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        String log = tb.new JavacTask()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                .options("-modulesourcepath", moduleSrc.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                         "-AexpectedEnclosedElements=m1=>impl")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                .writeAll()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                .getOutput(ToolBox.OutputKind.DIRECT);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    void testAPMultiModule(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Path m2 = moduleSrc.resolve("m2");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                          "package impl1; public class Impl1 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        tb.writeJavaFiles(m2,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                          "module m2 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                          "package impl2; public class Impl2 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        String log = tb.new JavacTask()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                .options("-modulesourcepath", moduleSrc.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                         "-AexpectedEnclosedElements=m1=>impl1,m2=>impl2")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                .writeAll()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                .getOutput(ToolBox.OutputKind.DIRECT);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    @SupportedOptions("expectedEnclosedElements")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    public static final class AP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        private Map<String, List<String>> module2ExpectedEnclosedElements;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            if (module2ExpectedEnclosedElements == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                module2ExpectedEnclosedElements = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                String expectedEnclosedElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                        processingEnv.getOptions().get("expectedEnclosedElements");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                for (String moduleDef : expectedEnclosedElements.split(",")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                    String[] module2Packages = moduleDef.split("=>");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    module2ExpectedEnclosedElements.put(module2Packages[0],
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                                                        Arrays.asList(module2Packages[1].split(":")));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            //verify ModuleType and ModuleSymbol behavior:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            for (Element root : roundEnv.getRootElements()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                ModuleElement module = processingEnv.getElementUtils().getModuleOf(root);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                assertEquals(TypeKind.MODULE, module.asType().getKind());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                boolean[] seenModule = new boolean[1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                module.accept(new ElementScanner9<Void, Void>() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                    public Void visitModule(ModuleElement e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                        seenModule[0] = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                    public Void scan(Element e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                        throw new AssertionError("Shouldn't get here.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                }, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                assertEquals(true, seenModule[0]);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                List<String> actualElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                        module.getEnclosedElements()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                              .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
                              .map(s -> (PackageElement) s)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                              .map(p -> p.getQualifiedName().toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                              .collect(Collectors.toList());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                assertEquals(module2ExpectedEnclosedElements.remove(module.getQualifiedName().toString()),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                             actualElements);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
            if (roundEnv.processingOver()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                assertEquals(true, module2ExpectedEnclosedElements.isEmpty());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    void testVerifyUsesProvides(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                          "module m1 { exports api; uses api.Api; provides api.Api with impl.Impl; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
                          "package api; public class Api { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   207
                          "package impl; public class Impl extends api.Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        String log = tb.new JavacTask()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                .options("-doe", "-processor", VerifyUsesProvidesAP.class.getName())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   212
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                .writeAll()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                .getOutput(ToolBox.OutputKind.DIRECT);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    public static final class VerifyUsesProvidesAP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   223
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   230
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   232
            assertNonNull("modle is null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
            List<? extends UsesDirective> uses = ElementFilter.usesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            assertEquals(1, uses.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   236
            assertEquals("api.Api", uses.iterator().next().getService().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            List<? extends ProvidesDirective> provides = ElementFilter.providesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            assertEquals(1, provides.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            assertEquals("api.Api", provides.iterator().next().getService().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            assertEquals("impl.Impl", provides.iterator().next().getImplementation().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
    @Test
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
    void testPackageNoModule(Path base) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        tb.writeJavaFiles(src,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                          "package api; public class Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        String log = tb.new JavacTask()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                .options("-processor", VerifyPackageNoModule.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                         "-source", "8",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                         "-Xlint:-options")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                .files(findJavaFiles(src))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                .writeAll()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                .getOutput(ToolBox.OutputKind.DIRECT);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   278
    public static final class VerifyPackageNoModule extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   279
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            assertNull("modle is not null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
    private static void assertNonNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        if (val == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
    private static void assertNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
        if (val != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    private static void assertEquals(Object expected, Object actual) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        if (!Objects.equals(expected, actual)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
            throw new AssertionError("expected: " + expected + "; actual=" + actual);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
}