8175193: jlink and requires static

Reviewed-by: alanb, forax
This commit is contained in:
Mandy Chung 2017-02-24 09:26:59 -08:00
parent 191aad0e62
commit 32e70396f4
7 changed files with 213 additions and 8 deletions

View File

@ -30,6 +30,8 @@ import jdk.tools.jlink.plugin.ResourcePoolModule;
import jdk.tools.jlink.plugin.ResourcePoolModuleView;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Requires.Modifier;
import java.nio.ByteBuffer;
import java.util.Deque;
import java.util.HashMap;
@ -67,14 +69,15 @@ public final class ModuleSorter {
private ModuleSorter addModule(ResourcePoolModule module) {
addNode(module);
readModuleDescriptor(module).requires().stream()
.forEach(req -> {
String dm = req.name();
ResourcePoolModule dep = moduleView.findModule(dm)
.orElseThrow(() -> new PluginException(dm + " not found"));
readModuleDescriptor(module).requires().forEach(req -> {
ResourcePoolModule dep = moduleView.findModule(req.name()).orElse(null);
if (dep != null) {
addNode(dep);
edges.get(module.name()).add(dep);
});
} else if (!req.modifiers().contains(Modifier.STATIC)) {
throw new PluginException(req.name() + " not found");
}
});
return this;
}
@ -113,7 +116,7 @@ public final class ModuleSorter {
return;
}
visited.add(node);
edges.get(node.name()).stream()
edges.get(node.name())
.forEach(x -> visit(x, visited, done));
done.add(node);
result.addLast(node);

View File

@ -64,7 +64,7 @@ public class UserModuleTest {
private static final String MAIN_MID = "m1/p1.Main";
// the names of the modules in this test
private static String[] modules = new String[] {"m1", "m2", "m3", "m4"};
private static String[] modules = new String[] {"m1", "m2", "m3", "m4", "m5"};
private static boolean hasJmods() {
@ -160,6 +160,50 @@ public class UserModuleTest {
.getExitValue() == 0);
}
@Test
public void testRequiresStatic() throws Throwable {
if (!hasJmods()) return;
Path dir = Paths.get("requiresStatic");
createImage(dir, "m5");
Path java = dir.resolve("bin").resolve("java");
assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue() == 0);
// run with m3 present
assertTrue(executeProcess(java.toString(),
"--module-path", MODS_DIR.toString(),
"--add-modules", "m3",
"-m", "m5/p5.Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue() == 0);
}
@Test
public void testRequiresStatic2() throws Throwable {
if (!hasJmods()) return;
Path dir = Paths.get("requiresStatic2");
createImage(dir, "m3", "m5");
Path java = dir.resolve("bin").resolve("java");
assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue() == 0);
// boot layer with m3 and m5
assertTrue(executeProcess(java.toString(),
"--add-modules", "m3",
"-m", "m5/p5.Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue() == 0);
}
private void createJmods(String... modules) throws IOException {
// use the same target platform as in java.base
ModuleDescriptor md = Layer.boot().findModule("java.base").get()

View File

@ -23,4 +23,5 @@
module m3 {
requires m4;
exports p3;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017, 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.
*/
package p3;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Foo {
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2017, 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.
*/
package p3;
public class Lib {
public static String concat(String x, String y) {
return x + y;
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2017, 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.
*/
module m5 {
requires static m3;
exports p5;
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (c) 2017, 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.
*/
package p5;
import java.lang.reflect.Layer;
import p3.Foo;
import p3.Lib;
/**
* This test verifies jlink support of requires static.
*/
public class Main {
public static void main(String... args) {
boolean libPresent = Layer.boot().findModule("m3").isPresent();
if (LibHelper.libClassFound != libPresent) {
throw new RuntimeException("Expected module m3 not in the boot layer");
}
if (libPresent) {
// p3.Lib must be present
LibHelper.concat("x", "y");
}
}
static class LibHelper {
@Foo
static final boolean libClassFound;
static {
boolean found = false;
try {
Class<?> c = Class.forName("p3.Lib");
found = true;
} catch (ClassNotFoundException e) {
}
libClassFound = found;
}
public static String concat(String x, String y) {
return Lib.concat(x, y);
}
}
}