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
|
/*
* Copyright (c) 2017, 2018, 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
* @bug 8193125 8196623
* @summary test modifiers with java.base
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.jvm
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.platform
* jdk.jdeps/com.sun.tools.classfile
* @build toolbox.ToolBox toolbox.JavacTask
* @run main JavaBaseTest
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.StreamSupport;
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 com.sun.tools.javac.jvm.Target;
import com.sun.tools.javac.platform.JDKPlatformProvider;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class JavaBaseTest {
public static void main(String... args) throws Exception {
JavaBaseTest t = new JavaBaseTest();
t.run();
}
final List<List<String>> modifiers = List.of(
List.of("static"),
List.of("transitive"),
List.of("static", "transitive")
);
enum Mode { SOURCE, CLASS };
ToolBox tb = new ToolBox();
int testCount = 0;
int errorCount = 0;
void run() throws Exception {
Set<String> targets = new LinkedHashSet<>();
StreamSupport.stream(new JDKPlatformProvider().getSupportedPlatformNames()
.spliterator(),
false)
.filter(p -> Integer.parseInt(p) >= 9)
.forEach(targets::add);
//run without --release:
targets.add("current");
for (List<String> mods : modifiers) {
for (String target : targets) {
for (Mode mode : Mode.values()) {
test(mods, target, mode);
}
}
}
System.err.println(testCount + " tests");
if (errorCount > 0) {
throw new Exception(errorCount + " errors found");
}
}
void test(List<String> mods, String target, Mode mode) {
System.err.println("Test " + mods + " " + target + " " + mode);
testCount++;
try {
Path base = Paths.get(String.join("-", mods))
.resolve(target).resolve(mode.name().toLowerCase());
Files.createDirectories(base);
switch (mode) {
case SOURCE:
testSource(base, mods, target);
break;
case CLASS:
testClass(base, mods, target);
break;
}
} catch (Exception e) {
error("Exception: " + e);
e.printStackTrace();
}
System.err.println();
}
void testSource(Path base, List<String> mods, String target) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { requires " + String.join(" ", mods) + " java.base; }");
Path modules = Files.createDirectories(base.resolve("modules"));
boolean expectOK = target.equals("9");
JavacTask jct = new JavacTask(tb)
.outdir(modules);
if (target.equals("current"))
jct.options("-XDrawDiagnostics");
else
jct.options("-XDrawDiagnostics", "--release", target);
String log = jct.files(tb.findJavaFiles(src))
.run(expectOK ? Task.Expect.SUCCESS : Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!expectOK) {
boolean foundErrorMessage = false;
for (String mod : mods) {
String key = mod.equals("static")
? "compiler.err.mod.not.allowed.here"
: "compiler.err.modifier.not.allowed.here";
String message = "module-info.java:1:12: " + key + ": " + mod;
if (log.contains(message)) {
foundErrorMessage = true;
}
}
if (!foundErrorMessage) {
throw new Exception("expected error message not found");
}
}
}
void testClass(Path base, List<String> mods, String target) throws Exception {
createClass(base, mods, target);
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module mx { requires m; }");
Path modules = Files.createDirectories(base.resolve("modules"));
boolean expectOK = target.equals("9");
String log = new JavacTask(tb)
.outdir(modules)
.options("-XDrawDiagnostics",
"--module-path", base.resolve("test-modules").toString())
.files(tb.findJavaFiles(src))
.run(expectOK ? Task.Expect.SUCCESS : Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!expectOK) {
boolean foundErrorMessage = false;
for (String mod : mods) {
String flag = mod.equals("static")
? "ACC_STATIC_PHASE (0x0040)"
: "ACC_TRANSITIVE (0x0020)";
String message = "- compiler.err.cant.access: m.module-info,"
+ " (compiler.misc.bad.class.file.header: module-info.class,"
+ " (compiler.misc.bad.requires.flag: " + flag + ")";
if (log.contains(message)) {
foundErrorMessage = true;
}
}
if (!foundErrorMessage) {
throw new Exception("expected error message not found");
}
}
}
void createClass(Path base, List<String> mods, String target) throws Exception {
Path src1 = base.resolve("interim-src");
tb.writeJavaFiles(src1,
"module m { requires java.base; }");
Path modules1 = Files.createDirectories(base.resolve("interim-modules"));
JavacTask jct = new JavacTask(tb)
.outdir(modules1);
if (!target.equals("current")) {
jct.options("--release", target);
}
jct.files(tb.findJavaFiles(src1))
.run(Task.Expect.SUCCESS);
ClassFile cf1 = ClassFile.read(modules1.resolve("module-info.class"));
Map<String,Attribute> attrMap = new LinkedHashMap<>(cf1.attributes.map);
Module_attribute modAttr1 = (Module_attribute) attrMap.get("Module");
Module_attribute.RequiresEntry[] requires =
new Module_attribute.RequiresEntry[modAttr1.requires_count];
for (int i = 0; i < modAttr1.requires_count; i++) {
Module_attribute.RequiresEntry e1 = modAttr1.requires[i];
int flags = e1.requires_flags;
Module_attribute.RequiresEntry e2;
if (e1.getRequires(cf1.constant_pool).equals("java.base")) {
for (String mod : mods) {
switch (mod) {
case "static":
flags |= Module_attribute.ACC_STATIC_PHASE;
break;
case "transitive":
flags |= Module_attribute.ACC_TRANSITIVE;
break;
}
}
e2 = new Module_attribute.RequiresEntry(
e1.requires_index,
flags,
e1.requires_version_index);
} else {
e2 = e1;
}
requires[i] = e2;
}
Module_attribute modAttr2 = new Module_attribute(
modAttr1.attribute_name_index,
modAttr1.module_name,
modAttr1.module_flags,
modAttr1.module_version_index,
requires,
modAttr1.exports,
modAttr1.opens,
modAttr1.uses_index,
modAttr1.provides);
attrMap.put("Module", modAttr2);
Attributes attributes = new Attributes(attrMap);
ClassFile cf2 = new ClassFile(
cf1.magic, cf1.minor_version, cf1.major_version,
cf1.constant_pool, cf1.access_flags,
cf1.this_class, cf1.super_class, cf1.interfaces,
cf1.fields, cf1.methods, attributes);
Path modInfo = base.resolve("test-modules").resolve("module-info.class");
Files.createDirectories(modInfo.getParent());
new ClassWriter().write(cf2, modInfo.toFile());
}
private void error(String message) {
System.err.println("Error: " + message);
errorCount++;
}
}
|