langtools/test/tools/javac/modules/AnnotationProcessing.java
author jjg
Wed, 10 Aug 2016 15:47:46 -0700
changeset 40308 274367a99f98
parent 37758 3ecf9b414e05
child 40311 bb76098875c8
permissions -rw-r--r--
8136930: Simplify use of module-system options by custom launchers 8160489: Multiple -Xpatch lines ignored by javac 8156998: javac should support new option -XinheritRuntimeEnvironment Reviewed-by: jlahoda, ksrini
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
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    31
 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 * @run main AnnotationProcessing
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Objects;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import javax.annotation.processing.AbstractProcessor;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import javax.annotation.processing.RoundEnvironment;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.annotation.processing.SupportedAnnotationTypes;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.annotation.processing.SupportedOptions;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.lang.model.element.Element;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import javax.lang.model.element.ModuleElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import javax.lang.model.element.ModuleElement.ProvidesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import javax.lang.model.element.ModuleElement.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import javax.lang.model.element.PackageElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import javax.lang.model.element.TypeElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import javax.lang.model.type.TypeKind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import javax.lang.model.util.ElementFilter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import javax.lang.model.util.ElementScanner9;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    60
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    61
import toolbox.Task;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    62
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    63
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
public class AnnotationProcessing extends ModuleTestBase {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    public static void main(String... args) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        new AnnotationProcessing().runTests();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
    71
    public void testAPSingleModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                          "package impl; public class Impl { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    83
        String log = new JavacTask(tb)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37758
diff changeset
    84
                .options("--module-source-path", moduleSrc.toString(),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                         "-AexpectedEnclosedElements=m1=>impl")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    91
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
    98
    public void testAPMultiModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        Path m2 = moduleSrc.resolve("m2");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                          "package impl1; public class Impl1 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        tb.writeJavaFiles(m2,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                          "module m2 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                          "package impl2; public class Impl2 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   115
        String log = new JavacTask(tb)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37758
diff changeset
   116
                .options("--module-source-path", moduleSrc.toString(),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                         "-AexpectedEnclosedElements=m1=>impl1,m2=>impl2")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   123
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    @SupportedOptions("expectedEnclosedElements")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    public static final class AP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        private Map<String, List<String>> module2ExpectedEnclosedElements;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            if (module2ExpectedEnclosedElements == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                module2ExpectedEnclosedElements = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                String expectedEnclosedElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                        processingEnv.getOptions().get("expectedEnclosedElements");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                for (String moduleDef : expectedEnclosedElements.split(",")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                    String[] module2Packages = moduleDef.split("=>");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                    module2ExpectedEnclosedElements.put(module2Packages[0],
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                                                        Arrays.asList(module2Packages[1].split(":")));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            //verify ModuleType and ModuleSymbol behavior:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            for (Element root : roundEnv.getRootElements()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                ModuleElement module = processingEnv.getElementUtils().getModuleOf(root);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                assertEquals(TypeKind.MODULE, module.asType().getKind());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                boolean[] seenModule = new boolean[1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                module.accept(new ElementScanner9<Void, Void>() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                    public Void visitModule(ModuleElement e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                        seenModule[0] = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
                    public Void scan(Element e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                        throw new AssertionError("Shouldn't get here.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                }, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                assertEquals(true, seenModule[0]);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
                List<String> actualElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                        module.getEnclosedElements()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                              .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                              .map(s -> (PackageElement) s)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                              .map(p -> p.getQualifiedName().toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                              .collect(Collectors.toList());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                assertEquals(module2ExpectedEnclosedElements.remove(module.getQualifiedName().toString()),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                             actualElements);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            if (roundEnv.processingOver()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                assertEquals(true, module2ExpectedEnclosedElements.isEmpty());
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
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
   199
    public void testVerifyUsesProvides(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
                          "module m1 { exports api; uses api.Api; provides api.Api with impl.Impl; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
                          "package api; public class Api { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                          "package impl; public class Impl extends api.Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   212
        String log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
                .options("-doe", "-processor", VerifyUsesProvidesAP.class.getName())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   218
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   223
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   224
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   225
    public static final class VerifyUsesProvidesAP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   230
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   232
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            assertNonNull("modle is null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   236
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
            List<? extends UsesDirective> uses = ElementFilter.usesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   238
            assertEquals(1, uses.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            assertEquals("api.Api", uses.iterator().next().getService().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            List<? extends ProvidesDirective> provides = ElementFilter.providesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            assertEquals(1, provides.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            assertEquals("api.Api", provides.iterator().next().getService().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            assertEquals("impl.Impl", provides.iterator().next().getImplementation().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
   257
    public void testPackageNoModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        tb.writeJavaFiles(src,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                          "package api; public class Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   266
        String log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                .options("-processor", VerifyPackageNoModule.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                         "-source", "8",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                         "-Xlint:-options")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                .files(findJavaFiles(src))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   274
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   278
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   279
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
    public static final class VerifyPackageNoModule extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            assertNull("modle is not null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
    private static void assertNonNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        if (val == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
    private static void assertNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
        if (val != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
    private static void assertEquals(Object expected, Object actual) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        if (!Objects.equals(expected, actual)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            throw new AssertionError("expected: " + expected + "; actual=" + actual);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   320
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
}