1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary tests for multi-module mode compilation
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.classfile
* jdk.jdeps/com.sun.tools.javap
* @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
* @run main OpenModulesTest
*/
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.Attributes;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.ClassWriter;
import com.sun.tools.classfile.Module_attribute;
import toolbox.JavacTask;
import toolbox.JavapTask;
import toolbox.Task;
import toolbox.Task.Expect;
import toolbox.Task.OutputKind;
public class OpenModulesTest extends ModuleTestBase {
public static void main(String... args) throws Exception {
new OpenModulesTest().runTests();
}
@Test
public void testStrongModule(Path base) throws Exception {
Path m1 = base.resolve("m1x");
tb.writeJavaFiles(m1,
"module m1x { exports api1; opens api2; }",
"package api1; public class Api1 {}",
"package api2; public class Api2 {}",
"package impl; public class Impl {}");
Path classes = base.resolve("classes");
Path m1Classes = classes.resolve("m1x");
tb.createDirectories(m1Classes);
String log = new JavacTask(tb)
.outdir(m1Classes)
.files(findJavaFiles(m1))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.isEmpty())
throw new Exception("expected output not found: " + log);
String decompiled = new JavapTask(tb)
.options("--system", "none", "-bootclasspath", "")
.classes(m1Classes.resolve("module-info.class").toString())
.run()
.writeAll()
.getOutput(OutputKind.DIRECT)
.replace(System.getProperty("line.separator"), "\n")
.replaceAll("@[^;]*;", ";");
String expected = "module m1x {\n" +
" requires java.base;\n" +
" exports api1;\n" +
" opens api2;\n" +
"}";
if (!decompiled.contains(expected)) {
throw new Exception("expected output not found: " + decompiled);
}
//compiling against a strong module read from binary:
Path m2 = base.resolve("m2x");
tb.writeJavaFiles(m2,
"module m2x { requires m1x; }",
"package test; public class Test { api1.Api1 a1; api2.Api2 a2; }");
Path m2Classes = classes.resolve("m2x");
tb.createDirectories(m2Classes);
List<String> log2 = new JavacTask(tb)
.options("--module-path", m1Classes.toString(),
"-XDrawDiagnostics")
.outdir(m2Classes)
.files(findJavaFiles(m2))
.run(Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected2 = Arrays.asList("Test.java:1:49: compiler.err.package.not.visible: api2, (compiler.misc.not.def.access.not.exported: api2, m1x)",
"1 error");
if (!Objects.equals(log2, expected2))
throw new Exception("expected output not found: " + log2);
}
@Test
public void testOpenModule(Path base) throws Exception {
Path m1 = base.resolve("m1x");
tb.writeJavaFiles(m1,
"open module m1x { exports api1; }",
"package api1; public class Api1 {}",
"package api2; public class Api2 {}",
"package impl; public class Impl {}");
Path classes = base.resolve("classes");
Path m1Classes = classes.resolve("m1x");
tb.createDirectories(m1Classes);
String log = new JavacTask(tb)
.outdir(m1Classes)
.files(findJavaFiles(m1))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.isEmpty())
throw new Exception("expected output not found: " + log);
String decompiled = new JavapTask(tb)
.options("--system", "none", "-bootclasspath", "")
.classes(m1Classes.resolve("module-info.class").toString())
.run()
.writeAll()
.getOutput(OutputKind.DIRECT)
.replace(System.getProperty("line.separator"), "\n")
.replaceAll("@[^;]*;", ";");
String expected = "open module m1x {\n" +
" requires java.base;\n" +
" exports api1;\n" +
"}";
if (!decompiled.contains(expected)) {
throw new Exception("expected output not found: " + decompiled);
}
//compiling against a ordinary module read from binary:
Path m2 = base.resolve("m2x");
tb.writeJavaFiles(m2,
"module m2x { requires m1x; }",
"package test; public class Test { api1.Api1 a1; api2.Api2 a2; }");
Path m2Classes = classes.resolve("m2x");
tb.createDirectories(m2Classes);
List<String> log2 = new JavacTask(tb)
.options("--module-path", m1Classes.toString(),
"-XDrawDiagnostics")
.outdir(m2Classes)
.files(findJavaFiles(m2))
.run(Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected2 = Arrays.asList("Test.java:1:49: compiler.err.package.not.visible: api2, (compiler.misc.not.def.access.not.exported: api2, m1x)",
"1 error");
if (!Objects.equals(log2, expected2))
throw new Exception("expected output not found: " + log2);
}
@Test
public void testOpenModuleNoOpens(Path base) throws Exception {
Path m1 = base.resolve("m1x");
tb.writeJavaFiles(m1,
"open module m1x { exports api1; opens api2; }",
"package api1; public class Api1 {}",
"package api2; public class Api2 {}",
"package impl; public class Impl {}");
Path classes = base.resolve("classes");
Path m1Classes = classes.resolve("m1x");
tb.createDirectories(m1Classes);
List<String> log = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(m1Classes)
.files(findJavaFiles(m1))
.run(Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("module-info.java:1:33: compiler.err.no.opens.unless.strong",
"1 error");
if (!expected.equals(log))
throw new Exception("expected output not found: " + log);
}
@Test
public void testNonZeroOpensInOpen(Path base) throws Exception {
Path m1 = base.resolve("m1x");
tb.writeJavaFiles(m1,
"module m1x { opens api; }",
"package api; public class Api {}");
Path classes = base.resolve("classes");
Path m1Classes = classes.resolve("m1x");
tb.createDirectories(m1Classes);
new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(m1Classes)
.files(findJavaFiles(m1))
.run(Expect.SUCCESS)
.writeAll();
Path miClass = m1Classes.resolve("module-info.class");
ClassFile cf = ClassFile.read(miClass);
Module_attribute module = (Module_attribute) cf.attributes.map.get(Attribute.Module);
Module_attribute newModule = new Module_attribute(module.attribute_name_index,
module.module_name,
module.module_flags | Module_attribute.ACC_OPEN,
module.module_version_index,
module.requires,
module.exports,
module.opens,
module.uses_index,
module.provides);
Map<String, Attribute> attrs = new HashMap<>(cf.attributes.map);
attrs.put(Attribute.Module, newModule);
Attributes newAttributes = new Attributes(attrs);
ClassFile newClassFile = new ClassFile(cf.magic,
cf.minor_version,
cf.major_version,
cf.constant_pool,
cf.access_flags,
cf.this_class,
cf.super_class,
cf.interfaces,
cf.fields,
cf.methods,
newAttributes);
try (OutputStream out = Files.newOutputStream(miClass)) {
new ClassWriter().write(newClassFile, out);
}
Path test = base.resolve("test");
tb.writeJavaFiles(test,
"package impl; public class Impl extends api.Api {}");
Path testClasses = base.resolve("test-classes");
tb.createDirectories(testClasses);
List<String> log = new JavacTask(tb)
.options("-XDrawDiagnostics",
"--module-path", classes.toString(),
"--add-modules", "m1x")
.outdir(testClasses)
.files(findJavaFiles(test))
.run(Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"- compiler.err.cant.access: m1x.module-info, "
+ "(compiler.misc.bad.class.file.header: module-info.class, "
+ "(compiler.misc.module.non.zero.opens: m1x))",
"1 error");
if (!expected.equals(log))
throw new Exception("expected output not found: " + log);
}
}
|