langtools/test/tools/javac/modules/IncubatingTest.java
changeset 43270 de9a02e20567
child 44573 245bb4e6f983
equal deleted inserted replaced
43269:12f989542165 43270:de9a02e20567
       
     1 /*
       
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8171177
       
    27  * @summary Verify that ModuleResolution attribute flags are honored.
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  *          jdk.jdeps/com.sun.tools.classfile
       
    32  *          jdk.jdeps/com.sun.tools.javap
       
    33  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.JavapTask ModuleTestBase
       
    34  * @run main IncubatingTest
       
    35  */
       
    36 
       
    37 import java.io.IOException;
       
    38 import java.io.OutputStream;
       
    39 import java.net.URI;
       
    40 import java.nio.file.FileSystem;
       
    41 import java.nio.file.FileSystems;
       
    42 import java.nio.file.Files;
       
    43 import java.nio.file.Path;
       
    44 import java.util.ArrayList;
       
    45 import java.util.Arrays;
       
    46 import java.util.HashMap;
       
    47 import java.util.List;
       
    48 import java.util.Map;
       
    49 
       
    50 import com.sun.tools.classfile.Attribute;
       
    51 import com.sun.tools.classfile.Attributes;
       
    52 import com.sun.tools.classfile.ClassFile;
       
    53 import com.sun.tools.classfile.ClassWriter;
       
    54 import com.sun.tools.classfile.ConstantPool;
       
    55 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
       
    56 import com.sun.tools.classfile.ConstantPool.CPInfo;
       
    57 import com.sun.tools.classfile.ModuleResolution_attribute;
       
    58 import toolbox.JavacTask;
       
    59 import toolbox.Task;
       
    60 import toolbox.Task.Expect;
       
    61 
       
    62 public class IncubatingTest extends ModuleTestBase {
       
    63 
       
    64     public static void main(String... args) throws Exception {
       
    65         new IncubatingTest().runTests();
       
    66     }
       
    67 
       
    68     @Test
       
    69     public void testDoNotResolve(Path base) throws Exception {
       
    70         Path src = base.resolve("src");
       
    71         tb.writeJavaFiles(src,
       
    72                           "module jdk.i { exports api; }",
       
    73                           "package api; public class Api { }");
       
    74         Path classes = base.resolve("classes");
       
    75         Files.deleteIfExists(classes);
       
    76         Path iClasses = classes.resolve("jdk.i");
       
    77         tb.createDirectories(iClasses);
       
    78 
       
    79         new JavacTask(tb)
       
    80                 .outdir(iClasses)
       
    81                 .files(findJavaFiles(src))
       
    82                 .run()
       
    83                 .writeAll();
       
    84 
       
    85         copyJavaBase(classes);
       
    86 
       
    87         Path jdkIModuleInfo = iClasses.resolve("module-info.class");
       
    88         addModuleResolutionAttribute(jdkIModuleInfo, ModuleResolution_attribute.DO_NOT_RESOLVE_BY_DEFAULT);
       
    89 
       
    90         Path testSrc = base.resolve("test-src");
       
    91         tb.writeJavaFiles(testSrc,
       
    92                           "class T { api.Api api; }");
       
    93         Path testClasses = base.resolve("test-classes");
       
    94         tb.createDirectories(testClasses);
       
    95 
       
    96         List<String> log;
       
    97         List<String> expected;
       
    98 
       
    99         log = new JavacTask(tb)
       
   100                 .options("--system", "none",
       
   101                          "--upgrade-module-path", classes.toString(),
       
   102                          "-XDrawDiagnostics")
       
   103                 .outdir(testClasses)
       
   104                 .files(findJavaFiles(testSrc))
       
   105                 .run(Expect.FAIL)
       
   106                 .writeAll()
       
   107                 .getOutputLines(Task.OutputKind.DIRECT);
       
   108 
       
   109         expected = Arrays.asList(
       
   110                 "T.java:1:11: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.from.unnamed: api, jdk.i)",
       
   111                 "1 error"
       
   112         );
       
   113 
       
   114         if (!expected.equals(log)) {
       
   115             throw new AssertionError("Unexpected output: " + log);
       
   116         }
       
   117 
       
   118         log = new JavacTask(tb)
       
   119                 .options("--system", "none",
       
   120                          "--upgrade-module-path", classes.toString(),
       
   121                          "--add-modules", "ALL-SYSTEM",
       
   122                          "-XDrawDiagnostics")
       
   123                 .outdir(testClasses)
       
   124                 .files(findJavaFiles(testSrc))
       
   125                 .run(Expect.FAIL)
       
   126                 .writeAll()
       
   127                 .getOutputLines(Task.OutputKind.DIRECT);
       
   128 
       
   129         expected = Arrays.asList(
       
   130                 "T.java:1:11: compiler.err.package.not.visible: api, (compiler.misc.not.def.access.does.not.read.from.unnamed: api, jdk.i)",
       
   131                 "1 error"
       
   132         );
       
   133 
       
   134         if (!expected.equals(log)) {
       
   135             throw new AssertionError("Unexpected output: " + log);
       
   136         }
       
   137 
       
   138         new JavacTask(tb)
       
   139                 .options("--system", "none",
       
   140                          "--upgrade-module-path", classes.toString(),
       
   141                          "--add-modules", "jdk.i")
       
   142                 .outdir(testClasses)
       
   143                 .files(findJavaFiles(testSrc))
       
   144                 .run()
       
   145                 .writeAll();
       
   146 
       
   147         Path testModuleSrc = base.resolve("test-module-src");
       
   148         tb.writeJavaFiles(testModuleSrc,
       
   149                           "module test { requires jdk.i; }", //explicit requires of an incubating module
       
   150                           "class T { api.Api api; }");
       
   151         Path testModuleClasses = base.resolve("test-module-classes");
       
   152         tb.createDirectories(testModuleClasses);
       
   153 
       
   154         new JavacTask(tb)
       
   155                 .options("--system", "none",
       
   156                          "--upgrade-module-path", classes.toString())
       
   157                 .outdir(testModuleClasses)
       
   158                 .files(findJavaFiles(testModuleSrc))
       
   159                 .run()
       
   160                 .writeAll();
       
   161     }
       
   162 
       
   163     @Test
       
   164     public void testIncubating(Path base) throws Exception {
       
   165         Path src = base.resolve("src");
       
   166         tb.writeJavaFiles(src,
       
   167                           "module jdk.i { exports api; }",
       
   168                           "package api; public class Api { }");
       
   169         Path classes = base.resolve("classes");
       
   170         Files.deleteIfExists(classes);
       
   171         Path iClasses = classes.resolve("jdk.i");
       
   172         tb.createDirectories(iClasses);
       
   173 
       
   174         new JavacTask(tb)
       
   175                 .outdir(iClasses)
       
   176                 .files(findJavaFiles(src))
       
   177                 .run()
       
   178                 .writeAll();
       
   179 
       
   180         Path jdkIModuleInfo = iClasses.resolve("module-info.class");
       
   181         addModuleResolutionAttribute(jdkIModuleInfo, ModuleResolution_attribute.WARN_INCUBATING);
       
   182 
       
   183         Path testSrc = base.resolve("test-src");
       
   184         tb.writeJavaFiles(testSrc,
       
   185                           "class T { api.Api api; }");
       
   186         Path testClasses = base.resolve("test-classes");
       
   187         tb.createDirectories(testClasses);
       
   188 
       
   189         List<String> log;
       
   190         List<String> expected;
       
   191 
       
   192         log = new JavacTask(tb)
       
   193                 .options("--module-path", classes.toString(),
       
   194                          "--add-modules", "jdk.i",
       
   195                          "-XDrawDiagnostics",
       
   196                          "-Werror")
       
   197                 .outdir(testClasses)
       
   198                 .files(findJavaFiles(testSrc))
       
   199                 .run(Expect.FAIL)
       
   200                 .writeAll()
       
   201                 .getOutputLines(Task.OutputKind.DIRECT);
       
   202 
       
   203         expected = Arrays.asList(
       
   204                 "- compiler.warn.incubating.modules: jdk.i",
       
   205                 "- compiler.err.warnings.and.werror",
       
   206                 "1 error",
       
   207                 "1 warning"
       
   208         );
       
   209 
       
   210         if (!expected.equals(log)) {
       
   211             throw new AssertionError("Unexpected output: " + log);
       
   212         }
       
   213 
       
   214         Path testModuleSrc = base.resolve("test-module-src");
       
   215         tb.writeJavaFiles(testModuleSrc,
       
   216                           "module test { requires jdk.i; }", //explicit requires of an incubating module
       
   217                           "class T { api.Api api; }");
       
   218         Path testModuleClasses = base.resolve("test-module-classes");
       
   219         tb.createDirectories(testModuleClasses);
       
   220 
       
   221         log = new JavacTask(tb)
       
   222                 .options("--module-path", classes.toString(),
       
   223                          "-XDrawDiagnostics",
       
   224                          "-Werror")
       
   225                 .outdir(testModuleClasses)
       
   226                 .files(findJavaFiles(testModuleSrc))
       
   227                 .run(Expect.FAIL)
       
   228                 .writeAll()
       
   229                 .getOutputLines(Task.OutputKind.DIRECT);
       
   230 
       
   231         expected = Arrays.asList(
       
   232                 "- compiler.warn.incubating.modules: jdk.i",
       
   233                 "- compiler.err.warnings.and.werror",
       
   234                 "1 error",
       
   235                 "1 warning"
       
   236         );
       
   237 
       
   238         if (!expected.equals(log)) {
       
   239             throw new AssertionError("Unexpected output: " + log);
       
   240         }
       
   241     }
       
   242 
       
   243     private void copyJavaBase(Path targetDir) throws IOException {
       
   244         FileSystem jrt = FileSystems.getFileSystem(URI.create("jrt:/"));
       
   245         Path javaBase = jrt.getPath("modules", "java.base");
       
   246 
       
   247         if (!Files.exists(javaBase)) {
       
   248             throw new AssertionError("No java.base?");
       
   249         }
       
   250 
       
   251         Path javaBaseClasses = targetDir.resolve("java.base");
       
   252 
       
   253         for (Path clazz : tb.findFiles("class", javaBase)) {
       
   254             Path target = javaBaseClasses.resolve(javaBase.relativize(clazz).toString());
       
   255             Files.createDirectories(target.getParent());
       
   256             Files.copy(clazz, target);
       
   257         }
       
   258     }
       
   259 
       
   260     private void addModuleResolutionAttribute(Path classfile, int resolution_flags) throws Exception {
       
   261         ClassFile cf = ClassFile.read(classfile);
       
   262         Attributes attrs = cf.attributes;
       
   263         List<CPInfo> cpData = new ArrayList<>();
       
   264         cpData.add(null);
       
   265         for (CPInfo info : cf.constant_pool.entries()) {
       
   266             cpData.add(info);
       
   267             if (info.size() == 2)
       
   268                 cpData.add(null);
       
   269         }
       
   270         cpData.add(new CONSTANT_Utf8_info(Attribute.ModuleResolution));
       
   271         ConstantPool newCP = new ConstantPool(cpData.toArray(new CPInfo[0]));
       
   272         ModuleResolution_attribute res = new ModuleResolution_attribute(newCP, resolution_flags);
       
   273         Map<String, Attribute> newAttributeMap = new HashMap<>(attrs.map);
       
   274         newAttributeMap.put(Attribute.ModuleResolution, res);
       
   275         Attributes newAttrs = new Attributes(newAttributeMap);
       
   276         ClassFile newCF = new ClassFile(cf.magic,
       
   277                                         cf.minor_version,
       
   278                                         cf.major_version,
       
   279                                         newCP,
       
   280                                         cf.access_flags,
       
   281                                         cf.this_class,
       
   282                                         cf.super_class,
       
   283                                         cf.interfaces,
       
   284                                         cf.fields,
       
   285                                         cf.methods,
       
   286                                         newAttrs);
       
   287         try (OutputStream out = Files.newOutputStream(classfile)) {
       
   288             new ClassWriter().write(newCF, out);
       
   289         }
       
   290     }
       
   291 }