langtools/test/tools/javac/modules/AnnotationProcessing.java
author jlahoda
Tue, 13 Dec 2016 10:49:28 +0100
changeset 42815 050370edaade
parent 42407 f3702cff2933
child 42823 58864b03c7b9
permissions -rw-r--r--
8072988: Update javax.annotation.processing for modules Summary: Support for generating files in multi-module mode. Reviewed-by: darcy, jjg Contributed-by: joe.darcy@oracle.com, jan.lahoda@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
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
    26
 * @bug 8133884 8162711
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @summary Verify that annotation processing works.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library /tools/lib
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *      jdk.compiler/com.sun.tools.javac.api
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *      jdk.compiler/com.sun.tools.javac.main
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    32
 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
36526
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
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    36
import java.io.File;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    37
import java.io.IOException;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    38
import java.io.OutputStream;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    39
import java.io.Reader;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    40
import java.io.Writer;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.nio.file.Path;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    43
import java.nio.file.Paths;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.HashMap;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    46
import java.util.HashSet;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.Objects;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.Set;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    51
import java.util.concurrent.Callable;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import javax.annotation.processing.AbstractProcessor;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    55
import javax.annotation.processing.Filer;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    56
import javax.annotation.processing.FilerException;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
    57
import javax.annotation.processing.Messager;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    58
import javax.annotation.processing.ProcessingEnvironment;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import javax.annotation.processing.RoundEnvironment;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import javax.annotation.processing.SupportedAnnotationTypes;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import javax.annotation.processing.SupportedOptions;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import javax.lang.model.element.Element;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    64
import javax.lang.model.element.ElementKind;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
import javax.lang.model.element.ModuleElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
import javax.lang.model.element.ModuleElement.ProvidesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import javax.lang.model.element.ModuleElement.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import javax.lang.model.element.PackageElement;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
import javax.lang.model.element.TypeElement;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
    70
import javax.lang.model.element.VariableElement;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
import javax.lang.model.type.TypeKind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
import javax.lang.model.util.ElementFilter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
import javax.lang.model.util.ElementScanner9;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
    74
import javax.tools.Diagnostic.Kind;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    75
import javax.tools.FileObject;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    76
import javax.tools.JavaCompiler;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    77
import javax.tools.JavaCompiler.CompilationTask;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    78
import javax.tools.JavaFileManager;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    79
import javax.tools.JavaFileManager.Location;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    80
import javax.tools.JavaFileObject;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    81
import javax.tools.StandardJavaFileManager;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    82
import javax.tools.StandardLocation;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    83
import javax.tools.ToolProvider;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    85
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    86
import toolbox.Task;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
    87
import toolbox.Task.Mode;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
    88
import toolbox.Task.OutputKind;
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    89
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
public class AnnotationProcessing extends ModuleTestBase {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    public static void main(String... args) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        new AnnotationProcessing().runTests();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
    97
    public void testAPSingleModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                          "package impl; public class Impl { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   109
        String log = new JavacTask(tb)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37758
diff changeset
   110
                .options("--module-source-path", moduleSrc.toString(),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                         "-AexpectedEnclosedElements=m1=>impl")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   117
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
   124
    public void testAPMultiModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        Path m2 = moduleSrc.resolve("m2");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                          "module m1 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                          "package impl1; public class Impl1 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        tb.writeJavaFiles(m2,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                          "module m2 { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                          "package impl2; public class Impl2 { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   141
        String log = new JavacTask(tb)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 37758
diff changeset
   142
                .options("--module-source-path", moduleSrc.toString(),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                         "-processor", AP.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                         "-AexpectedEnclosedElements=m1=>impl1,m2=>impl2")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   149
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
            throw new AssertionError("Unexpected output: " + log);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    @SupportedOptions("expectedEnclosedElements")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    public static final class AP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
        private Map<String, List<String>> module2ExpectedEnclosedElements;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   160
        private Set<String> seenModules = new HashSet<>();
36526
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 boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            if (module2ExpectedEnclosedElements == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
                module2ExpectedEnclosedElements = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
                String expectedEnclosedElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                        processingEnv.getOptions().get("expectedEnclosedElements");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                for (String moduleDef : expectedEnclosedElements.split(",")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                    String[] module2Packages = moduleDef.split("=>");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
                    module2ExpectedEnclosedElements.put(module2Packages[0],
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
                                                        Arrays.asList(module2Packages[1].split(":")));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
            //verify ModuleType and ModuleSymbol behavior:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
            for (Element root : roundEnv.getRootElements()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                ModuleElement module = processingEnv.getElementUtils().getModuleOf(root);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                assertEquals(TypeKind.MODULE, module.asType().getKind());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                boolean[] seenModule = new boolean[1];
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                module.accept(new ElementScanner9<Void, Void>() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                    public Void visitModule(ModuleElement e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
                        seenModule[0] = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
                        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    public Void scan(Element e, Void p) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                        throw new AssertionError("Shouldn't get here.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                }, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                assertEquals(true, seenModule[0]);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                List<String> actualElements =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                        module.getEnclosedElements()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                              .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                              .map(s -> (PackageElement) s)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                              .map(p -> p.getQualifiedName().toString())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                              .collect(Collectors.toList());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   207
                String moduleName = module.getQualifiedName().toString();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   208
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   209
                assertEquals(module2ExpectedEnclosedElements.get(moduleName),
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
                             actualElements);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   211
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   212
                seenModules.add(moduleName);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            if (roundEnv.processingOver()) {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   216
                assertEquals(module2ExpectedEnclosedElements.keySet(), seenModules);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
   230
    public void testVerifyUsesProvides(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        Path moduleSrc = base.resolve("module-src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        Path m1 = moduleSrc.resolve("m1");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        tb.writeJavaFiles(m1,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                          "module m1 { exports api; uses api.Api; provides api.Api with impl.Impl; }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                          "package api; public class Api { }",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                          "package impl; public class Impl extends api.Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   243
        String log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                .options("-doe", "-processor", VerifyUsesProvidesAP.class.getName())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                .files(findJavaFiles(moduleSrc))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   249
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            throw new AssertionError("Unexpected output: " + log);
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
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
    public static final class VerifyUsesProvidesAP extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
            assertNonNull("modle is null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
            List<? extends UsesDirective> uses = ElementFilter.usesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            assertEquals(1, uses.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
            assertEquals("api.Api", uses.iterator().next().getService().getQualifiedName().toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            List<? extends ProvidesDirective> provides = ElementFilter.providesIn(modle.getDirectives());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            assertEquals(1, provides.size());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            assertEquals("api.Api", provides.iterator().next().getService().getQualifiedName().toString());
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40311
diff changeset
   275
            assertEquals("impl.Impl", provides.iterator().next().getImplementations().get(0).getQualifiedName().toString());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            return false;
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
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    @Test
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 36778
diff changeset
   288
    public void testPackageNoModule(Path base) throws Exception {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
        Path src = base.resolve("src");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        Path classes = base.resolve("classes");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        Files.createDirectories(classes);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        tb.writeJavaFiles(src,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                          "package api; public class Api { }");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   297
        String log = new JavacTask(tb)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                .options("-processor", VerifyPackageNoModule.class.getName(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                         "-source", "8",
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                         "-Xlint:-options")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                .outdir(classes)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                .files(findJavaFiles(src))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                .run()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                .writeAll()
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   305
                .getOutput(Task.OutputKind.DIRECT);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
        if (!log.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            throw new AssertionError("Unexpected output: " + log);
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
    @SupportedAnnotationTypes("*")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    public static final class VerifyPackageNoModule extends AbstractProcessor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            TypeElement api = processingEnv.getElementUtils().getTypeElement("api.Api");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            assertNonNull("Cannot find api.Api", api);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            ModuleElement modle = (ModuleElement) processingEnv.getElementUtils().getPackageOf(api).getEnclosingElement();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            assertNull("modle is not null", modle);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        public SourceVersion getSupportedSourceVersion() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
            return SourceVersion.latest();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   334
    @Test
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   335
    public void testQualifiedClassForProcessing(Path base) throws Exception {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   336
        Path moduleSrc = base.resolve("module-src");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   337
        Path m1 = moduleSrc.resolve("m1");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   338
        Path m2 = moduleSrc.resolve("m2");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   339
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   340
        Path classes = base.resolve("classes");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   341
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   342
        Files.createDirectories(classes);
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   343
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   344
        tb.writeJavaFiles(m1,
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   345
                          "module m1 { }",
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   346
                          "package impl; public class Impl { int m1; }");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   347
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   348
        tb.writeJavaFiles(m2,
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   349
                          "module m2 { }",
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   350
                          "package impl; public class Impl { int m2; }");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   351
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   352
        new JavacTask(tb)
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   353
            .options("--module-source-path", moduleSrc.toString())
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   354
            .outdir(classes)
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   355
            .files(findJavaFiles(moduleSrc))
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   356
            .run()
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   357
            .writeAll()
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   358
            .getOutput(Task.OutputKind.DIRECT);
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   359
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   360
        List<String> expected = Arrays.asList("Note: field: m1");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   361
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   362
        for (Mode mode : new Mode[] {Mode.API, Mode.CMDLINE}) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   363
            List<String> log = new JavacTask(tb, mode)
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   364
                    .options("-processor", QualifiedClassForProcessing.class.getName(),
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   365
                             "--module-path", classes.toString())
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   366
                    .classes("m1/impl.Impl")
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   367
                    .outdir(classes)
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   368
                    .run()
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   369
                    .writeAll()
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   370
                    .getOutputLines(Task.OutputKind.DIRECT);
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   371
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   372
            if (!expected.equals(log))
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   373
                throw new AssertionError("Unexpected output: " + log);
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   374
        }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   375
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   376
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   377
    @SupportedAnnotationTypes("*")
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   378
    public static final class QualifiedClassForProcessing extends AbstractProcessor {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   379
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   380
        @Override
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   381
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   382
            if (processingEnv.getElementUtils().getModuleElement("m1") == null) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   383
                throw new AssertionError("No m1 module found.");
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   384
            }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   385
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   386
            Messager messager = processingEnv.getMessager();
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   387
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   388
            for (TypeElement clazz : ElementFilter.typesIn(roundEnv.getRootElements())) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   389
                for (VariableElement field : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   390
                    messager.printMessage(Kind.NOTE, "field: " + field.getSimpleName());
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   391
                }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   392
            }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   393
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   394
            return false;
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   395
        }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   396
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   397
        @Override
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   398
        public SourceVersion getSupportedSourceVersion() {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   399
            return SourceVersion.latest();
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   400
        }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   401
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   402
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   403
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   404
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   405
    public void testModuleInRootElements(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   406
        Path moduleSrc = base.resolve("module-src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   407
        Path m1 = moduleSrc.resolve("m1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   408
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   409
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   410
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   411
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   412
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   413
        tb.writeJavaFiles(m1,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   414
                          "module m1 { exports api; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   415
                          "package api; public class Api { }");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   416
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   417
        List<String> log = new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   418
                .options("-processor", ModuleInRootElementsAP.class.getName())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   419
                .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   420
                .files(findJavaFiles(moduleSrc))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   421
                .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   422
                .writeAll()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   423
                .getOutputLines(Task.OutputKind.STDERR);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   424
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   425
        assertEquals(Arrays.asList("module: m1"), log);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   426
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   427
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   428
    @SupportedAnnotationTypes("*")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   429
    public static final class ModuleInRootElementsAP extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   430
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   431
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   432
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   433
            roundEnv.getRootElements()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   434
                    .stream()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   435
                    .filter(el -> el.getKind() == ElementKind.MODULE)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   436
                    .forEach(mod -> System.err.println("module: " + mod.getSimpleName()));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   437
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   438
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   439
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   440
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   441
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   442
        public SourceVersion getSupportedSourceVersion() {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   443
            return SourceVersion.latest();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   444
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   445
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   446
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   447
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   448
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   449
    public void testAnnotationsInModuleInfo(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   450
        Path moduleSrc = base.resolve("module-src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   451
        Path m1 = moduleSrc.resolve("m1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   452
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   453
        tb.writeJavaFiles(m1,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   454
                          "@Deprecated module m1 { }");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   455
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   456
        Path m2 = moduleSrc.resolve("m2");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   457
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   458
        tb.writeJavaFiles(m2,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   459
                          "@SuppressWarnings(\"\") module m2 { }");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   460
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   461
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   462
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   463
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   464
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   465
        List<String> log = new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   466
                .options("-processor", AnnotationsInModuleInfoPrint.class.getName())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   467
                .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   468
                .files(findJavaFiles(m1))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   469
                .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   470
                .writeAll()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   471
                .getOutputLines(Task.OutputKind.DIRECT);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   472
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   473
        List<String> expectedLog = Arrays.asList("Note: AP Invoked",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   474
                                                 "Note: AP Invoked");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   475
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   476
        assertEquals(expectedLog, log);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   477
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   478
        new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   479
            .options("-processor", AnnotationsInModuleInfoFail.class.getName())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   480
            .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   481
            .files(findJavaFiles(m2))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   482
            .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   483
            .writeAll();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   484
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   485
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   486
    @SupportedAnnotationTypes("java.lang.Deprecated")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   487
    public static final class AnnotationsInModuleInfoPrint extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   488
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   489
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   490
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   491
            processingEnv.getMessager().printMessage(Kind.NOTE, "AP Invoked");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   492
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   493
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   494
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   495
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   496
        public SourceVersion getSupportedSourceVersion() {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   497
            return SourceVersion.latest();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   498
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   499
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   500
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   501
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   502
    @SupportedAnnotationTypes("java.lang.Deprecated")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   503
    public static final class AnnotationsInModuleInfoFail extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   504
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   505
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   506
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   507
            throw new AssertionError();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   508
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   509
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   510
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   511
        public SourceVersion getSupportedSourceVersion() {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   512
            return SourceVersion.latest();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   513
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   514
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   515
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   516
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   517
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   518
    public void testGenerateInMultiModeAPI(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   519
        Path moduleSrc = base.resolve("module-src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   520
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   521
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   522
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   523
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   524
        Path m1 = moduleSrc.resolve("m1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   525
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   526
        tb.writeJavaFiles(m1,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   527
                          "module m1 { exports api1; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   528
                          "package api1; public class Api { GenApi ga; impl.Impl i; }");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   529
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   530
        writeFile("1", m1, "api1", "api");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   531
        writeFile("1", m1, "impl", "impl");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   532
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   533
        Path m2 = moduleSrc.resolve("m2");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   534
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   535
        tb.writeJavaFiles(m2,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   536
                          "module m2 { requires m1; exports api2; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   537
                          "package api2; public class Api { api1.GenApi ga1; GenApi qa2; impl.Impl i;}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   538
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   539
        writeFile("2", m2, "api2", "api");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   540
        writeFile("2", m2, "impl", "impl");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   541
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   542
        for (FileType fileType : FileType.values()) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   543
            if (Files.isDirectory(classes)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   544
                tb.cleanDirectory(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   545
            } else {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   546
                Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   547
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   548
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   549
            new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   550
              .options("-processor", MultiModeAPITestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   551
                       "--module-source-path", moduleSrc.toString(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   552
                       "-Afiletype=" + fileType.name())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   553
              .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   554
              .files(findJavaFiles(moduleSrc))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   555
              .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   556
              .writeAll();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   557
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   558
            assertFileExists(classes, "m1", "api1", "GenApi.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   559
            assertFileExists(classes, "m1", "impl", "Impl.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   560
            assertFileExists(classes, "m1", "api1", "gen1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   561
            assertFileExists(classes, "m2", "api2", "GenApi.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   562
            assertFileExists(classes, "m2", "impl", "Impl.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   563
            assertFileExists(classes, "m2", "api2", "gen1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   564
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   565
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   566
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   567
    enum FileType {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   568
        SOURCE,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   569
        CLASS;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   570
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   571
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   572
    public static abstract class GeneratingAP extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   573
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   574
        void createSource(CreateFileObject file, String name, String content) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   575
            try (Writer out = file.create().openWriter()) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   576
                out.write(content);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   577
            } catch (IOException ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   578
                throw new IllegalStateException(ex);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   579
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   580
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   581
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   582
        void createClass(CreateFileObject file, String name, String content) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   583
            String fileNameStub = name.replace(".", File.separator);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   584
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   585
            try (OutputStream out = file.create().openOutputStream()) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   586
                Path scratch = Files.createDirectories(Paths.get(""));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   587
                Path scratchSrc = scratch.resolve(fileNameStub + ".java").toAbsolutePath();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   588
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   589
                Files.createDirectories(scratchSrc.getParent());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   590
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   591
                try (Writer w = Files.newBufferedWriter(scratchSrc)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   592
                    w.write(content);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   593
                }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   594
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   595
                Path scratchClasses = scratch.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   596
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   597
                Files.createDirectories(scratchClasses);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   598
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   599
                JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   600
                try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   601
                    List<String> options = Arrays.asList("-d", scratchClasses.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   602
                    Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(scratchSrc);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   603
                    CompilationTask task = comp.getTask(null, fm, null, options, null, files);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   604
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   605
                    if (!task.call()) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   606
                        throw new AssertionError("compilation failed");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   607
                    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   608
                }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   609
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   610
                Path classfile = scratchClasses.resolve(fileNameStub + ".class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   611
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   612
                Files.copy(classfile, out);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   613
            } catch (IOException ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   614
                throw new IllegalStateException(ex);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   615
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   616
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   617
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   618
        void doReadResource(CreateFileObject file, String expectedContent) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   619
            try {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   620
                StringBuilder actualContent = new StringBuilder();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   621
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   622
                try (Reader r = file.create().openReader(true)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   623
                    int read;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   624
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   625
                    while ((read = r.read()) != (-1)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   626
                        actualContent.append((char) read);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   627
                    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   628
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   629
                }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   630
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   631
                assertEquals(expectedContent, actualContent.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   632
            } catch (IOException ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   633
                throw new IllegalStateException(ex);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   634
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   635
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   636
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   637
        public interface CreateFileObject {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   638
            public FileObject create() throws IOException;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   639
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   640
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   641
        void expectFilerException(Callable<Object> c) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   642
            try {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   643
                c.call();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   644
                throw new AssertionError("Expected exception not thrown");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   645
            } catch (FilerException ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   646
                //expected
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   647
            } catch (Exception ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   648
                throw new IllegalStateException(ex);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   649
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   650
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   651
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   652
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   653
        public SourceVersion getSupportedSourceVersion() {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   654
            return SourceVersion.latest();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   655
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   656
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   657
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   658
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   659
    @SupportedAnnotationTypes("*")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   660
    @SupportedOptions({"filetype", "modulename"})
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   661
    public static final class MultiModeAPITestAP extends GeneratingAP {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   662
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   663
        int round;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   664
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   665
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   666
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   667
            if (round++ != 0)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   668
                return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   669
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   670
            createClass("m1", "api1.GenApi", "package api1; public class GenApi {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   671
            createClass("m1", "impl.Impl", "package impl; public class Impl {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   672
            createClass("m2", "api2.GenApi", "package api2; public class GenApi {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   673
            createClass("m2", "impl.Impl", "package impl; public class Impl {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   674
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   675
            createResource("m1", "api1", "gen1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   676
            createResource("m2", "api2", "gen1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   677
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   678
            readResource("m1", "api1", "api", "1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   679
            readResource("m1", "impl", "impl", "1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   680
            readResource("m2", "api2", "api", "2");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   681
            readResource("m2", "impl", "impl", "2");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   682
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   683
            Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   684
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   685
            expectFilerException(() -> filer.createSourceFile("fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   686
            expectFilerException(() -> filer.createClassFile("fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   687
            expectFilerException(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "fail", "fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   688
            expectFilerException(() -> filer.getResource(StandardLocation.MODULE_SOURCE_PATH, "fail", "fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   689
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   690
            //must not generate to unnamed package:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   691
            expectFilerException(() -> filer.createSourceFile("m1/Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   692
            expectFilerException(() -> filer.createClassFile("m1/Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   693
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   694
            //cannot generate resources to modules that are not root modules:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   695
            expectFilerException(() -> filer.createSourceFile("java.base/fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   696
            expectFilerException(() -> filer.createClassFile("java.base/fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   697
            expectFilerException(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "java.base/fail", "Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   698
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   699
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   700
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   701
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   702
        void createClass(String expectedModule, String name, String content) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   703
            Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   704
            FileType filetype = FileType.valueOf(processingEnv.getOptions().getOrDefault("filetype", ""));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   705
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   706
            switch (filetype) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   707
                case SOURCE:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   708
                    createSource(() -> filer.createSourceFile(expectedModule + "/" + name), name, content);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   709
                    break;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   710
                case CLASS:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   711
                    createClass(() -> filer.createClassFile(expectedModule + "/" + name), name, content);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   712
                    break;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   713
                default:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   714
                    throw new AssertionError("Unexpected filetype: " + filetype);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   715
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   716
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   717
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   718
        void createResource(String expectedModule, String pkg, String relName) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   719
            try {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   720
                Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   721
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   722
                filer.createResource(StandardLocation.CLASS_OUTPUT, expectedModule + "/" + pkg, relName)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   723
                     .openOutputStream()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   724
                     .close();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   725
            } catch (IOException ex) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   726
                throw new IllegalStateException(ex);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   727
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   728
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   729
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   730
        void readResource(String expectedModule, String pkg, String relName, String expectedContent) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   731
            Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   732
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   733
            doReadResource(() -> filer.getResource(StandardLocation.MODULE_SOURCE_PATH, expectedModule + "/" + pkg, relName),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   734
                           expectedContent);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   735
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   736
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   737
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   738
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   739
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   740
    public void testGenerateInSingleNameModeAPI(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   741
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   742
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   743
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   744
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   745
        Path m1 = base.resolve("module-src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   746
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   747
        tb.writeJavaFiles(m1,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   748
                          "module m1 { }");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   749
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   750
        writeFile("3", m1, "impl", "resource");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   751
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   752
        new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   753
          .options("-processor", SingleNameModeAPITestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   754
                   "-sourcepath", m1.toString())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   755
          .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   756
          .files(findJavaFiles(m1))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   757
          .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   758
          .writeAll();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   759
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   760
        assertFileExists(classes, "impl", "Impl1.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   761
        assertFileExists(classes, "impl", "Impl2.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   762
        assertFileExists(classes, "impl", "Impl3");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   763
        assertFileExists(classes, "impl", "Impl4.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   764
        assertFileExists(classes, "impl", "Impl5.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   765
        assertFileExists(classes, "impl", "Impl6");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   766
        assertFileExists(classes, "impl", "Impl7.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   767
        assertFileExists(classes, "impl", "Impl8.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   768
        assertFileExists(classes, "impl", "Impl9");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   769
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   770
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   771
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   772
    @SupportedAnnotationTypes("*")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   773
    public static final class SingleNameModeAPITestAP extends GeneratingAP {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   774
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   775
        int round;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   776
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   777
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   778
        public synchronized void init(ProcessingEnvironment processingEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   779
            super.init(processingEnv);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   780
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   781
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   782
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   783
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   784
            if (round++ != 0)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   785
                return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   786
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   787
            Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   788
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   789
            createSource(() -> filer.createSourceFile("impl.Impl1"), "impl.Impl1", "package impl; class Impl1 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   790
            createClass(() -> filer.createClassFile("impl.Impl2"), "impl.Impl2", "package impl; class Impl2 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   791
            createSource(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "impl", "Impl3"), "impl.Impl3", "");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   792
            doReadResource(() -> filer.getResource(StandardLocation.SOURCE_PATH, "impl", "resource"), "3");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   793
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   794
            createSource(() -> filer.createSourceFile("m1/impl.Impl4"), "impl.Impl4", "package impl; class Impl4 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   795
            createClass(() -> filer.createClassFile("m1/impl.Impl5"), "impl.Impl5", "package impl; class Impl5 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   796
            createSource(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "m1/impl", "Impl6"), "impl.Impl6", "");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   797
            doReadResource(() -> filer.getResource(StandardLocation.SOURCE_PATH, "m1/impl", "resource"), "3");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   798
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   799
            TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   800
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   801
            //"broken" originating element:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   802
            createSource(() -> filer.createSourceFile("impl.Impl7", jlObject), "impl.Impl7", "package impl; class Impl7 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   803
            createClass(() -> filer.createClassFile("impl.Impl8", jlObject), "impl.Impl8", "package impl; class Impl8 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   804
            createSource(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "impl", "Impl9", jlObject), "impl.Impl9", "");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   805
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   806
            //must not generate to unnamed package:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   807
            expectFilerException(() -> filer.createSourceFile("Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   808
            expectFilerException(() -> filer.createClassFile("Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   809
            expectFilerException(() -> filer.createSourceFile("m1/Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   810
            expectFilerException(() -> filer.createClassFile("m1/Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   811
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   812
            //cannot generate resources to modules that are not root modules:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   813
            expectFilerException(() -> filer.createSourceFile("java.base/fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   814
            expectFilerException(() -> filer.createClassFile("java.base/fail.Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   815
            expectFilerException(() -> filer.createResource(StandardLocation.CLASS_OUTPUT, "java.base/fail", "Fail"));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   816
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   817
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   818
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   819
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   820
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   821
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   822
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   823
    public void testGenerateInUnnamedModeAPI(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   824
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   825
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   826
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   827
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   828
        Path src = base.resolve("src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   829
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   830
        tb.writeJavaFiles(src,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   831
                          "class T {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   832
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   833
        new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   834
          .options("-processor", UnnamedModeAPITestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   835
                   "-sourcepath", src.toString())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   836
          .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   837
          .files(findJavaFiles(src))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   838
          .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   839
          .writeAll();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   840
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   841
        assertFileExists(classes, "Impl1.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   842
        assertFileExists(classes, "Impl2.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   843
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   844
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   845
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   846
    public void testGenerateInNoModeAPI(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   847
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   848
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   849
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   850
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   851
        Path src = base.resolve("src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   852
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   853
        tb.writeJavaFiles(src,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   854
                          "class T {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   855
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   856
        new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   857
          .options("-processor", UnnamedModeAPITestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   858
                   "-source", "8", "-target", "8",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   859
                   "-sourcepath", src.toString())
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   860
          .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   861
          .files(findJavaFiles(src))
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   862
          .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   863
          .writeAll();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   864
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   865
        assertFileExists(classes, "Impl1.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   866
        assertFileExists(classes, "Impl2.class");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   867
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   868
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   869
    @SupportedAnnotationTypes("*")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   870
    public static final class UnnamedModeAPITestAP extends GeneratingAP {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   871
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   872
        int round;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   873
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   874
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   875
        public synchronized void init(ProcessingEnvironment processingEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   876
            super.init(processingEnv);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   877
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   878
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   879
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   880
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   881
            if (round++ != 0)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   882
                return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   883
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   884
            Filer filer = processingEnv.getFiler();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   885
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   886
            //must not generate to unnamed package:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   887
            createSource(() -> filer.createSourceFile("Impl1"), "Impl1", "class Impl1 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   888
            createClass(() -> filer.createClassFile("Impl2"), "Impl2", "class Impl2 {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   889
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   890
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   891
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   892
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   893
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   894
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   895
    @Test
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   896
    public void testDisambiguateAnnotations(Path base) throws Exception {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   897
        Path classes = base.resolve("classes");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   898
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   899
        Files.createDirectories(classes);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   900
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   901
        Path src = base.resolve("src");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   902
        Path m1 = src.resolve("m1");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   903
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   904
        tb.writeJavaFiles(m1,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   905
                          "module m1 { exports api; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   906
                          "package api; public @interface A {}",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   907
                          "package api; public @interface B {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   908
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   909
        Path m2 = src.resolve("m2");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   910
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   911
        tb.writeJavaFiles(m2,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   912
                          "module m2 { exports api; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   913
                          "package api; public @interface A {}",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   914
                          "package api; public @interface B {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   915
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   916
        Path m3 = src.resolve("m3");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   917
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   918
        tb.writeJavaFiles(m3,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   919
                          "module m3 { requires m1; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   920
                          "package impl; import api.*; @A @B public class T {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   921
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   922
        Path m4 = src.resolve("m4");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   923
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   924
        tb.writeJavaFiles(m4,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   925
                          "module m4 { requires m2; }",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   926
                          "package impl; import api.*; @A @B public class T {}");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   927
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   928
        List<String> log;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   929
        List<String> expected;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   930
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   931
        log = new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   932
            .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   933
                     "--module-source-path", src.toString(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   934
                     "-m", "m1,m2")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   935
            .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   936
            .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   937
            .writeAll()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   938
            .getOutputLines(OutputKind.STDERR);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   939
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   940
        expected = Arrays.asList("");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   941
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   942
        if (!expected.equals(log)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   943
            throw new AssertionError("Output does not match; output: " + log);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   944
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   945
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   946
        log = new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   947
            .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   948
                     "--module-source-path", src.toString(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   949
                     "-m", "m3")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   950
            .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   951
            .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   952
            .writeAll()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   953
            .getOutputLines(OutputKind.STDERR);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   954
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   955
        expected = Arrays.asList("SelectAnnotationBTestAP",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   956
                                 "SelectAnnotationBTestAP");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   957
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   958
        if (!expected.equals(log)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   959
            throw new AssertionError("Output does not match; output: " + log);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   960
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   961
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   962
        log = new JavacTask(tb)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   963
            .options("-processor", SelectAnnotationATestAP.class.getName() + "," + SelectAnnotationBTestAP.class.getName(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   964
                     "--module-source-path", src.toString(),
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   965
                     "-m", "m4")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   966
            .outdir(classes)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   967
            .run()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   968
            .writeAll()
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   969
            .getOutputLines(OutputKind.STDERR);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   970
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   971
        expected = Arrays.asList("SelectAnnotationATestAP",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   972
                                 "SelectAnnotationBTestAP",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   973
                                 "SelectAnnotationATestAP",
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   974
                                 "SelectAnnotationBTestAP");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   975
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   976
        if (!expected.equals(log)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   977
            throw new AssertionError("Output does not match; output: " + log);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   978
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   979
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   980
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   981
    @SupportedAnnotationTypes("m2/api.A")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   982
    public static final class SelectAnnotationATestAP extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   983
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   984
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   985
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   986
            System.err.println("SelectAnnotationATestAP");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   987
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   988
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   989
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   990
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   991
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   992
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   993
    @SupportedAnnotationTypes("api.B")
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   994
    public static final class SelectAnnotationBTestAP extends AbstractProcessor {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   995
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   996
        @Override
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   997
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   998
            System.err.println("SelectAnnotationBTestAP");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
   999
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1000
            return false;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1001
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1002
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1003
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1004
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1005
    private static void writeFile(String content, Path base, String... pathElements) throws IOException {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1006
        Path file = resolveFile(base, pathElements);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1007
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1008
        Files.createDirectories(file.getParent());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1009
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1010
        try (Writer out = Files.newBufferedWriter(file)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1011
            out.append(content);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1012
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1013
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1014
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1015
    private static void assertNonNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1016
        if (val == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1017
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1018
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1019
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1020
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1021
    private static void assertNull(String msg, Object val) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1022
        if (val != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
            throw new AssertionError(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1027
    private static void assertEquals(Object expected, Object actual) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
        if (!Objects.equals(expected, actual)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
            throw new AssertionError("expected: " + expected + "; actual=" + actual);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1033
    private static void assertFileExists(Path base, String... pathElements) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1034
        Path file = resolveFile(base, pathElements);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1035
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1036
        if (!Files.exists(file)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1037
            throw new AssertionError("Expected file: " + file + " exist, but it does not.");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1038
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1039
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1040
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1041
    static Path resolveFile(Path base, String... pathElements) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1042
        Path file = base;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1043
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1044
        for (String el : pathElements) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1045
            file = file.resolve(el);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1046
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1047
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1048
        return file;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1049
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42407
diff changeset
  1050
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
}