Merge pull request #12030 from lsieun/master
BAEL-1699: Java 9 illegal reflective access warning
This commit is contained in:
commit
60082cdba9
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
DIR=baeldung-agent
|
||||
|
||||
# compile
|
||||
mkdir -p out/${DIR}
|
||||
javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
|
||||
|
||||
# package
|
||||
mkdir -p mods
|
||||
jar --create --file=mods/${DIR}.jar --manifest=${DIR}/manifest.txt -C out/${DIR} .
|
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
DIR=baeldung-reflected
|
||||
|
||||
# compile
|
||||
mkdir -p out/${DIR}
|
||||
javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
|
||||
|
||||
# package
|
||||
mkdir -p mods
|
||||
jar --create --file=mods/${DIR}.jar -C out/${DIR} .
|
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
DIR=baeldung-intermedium
|
||||
|
||||
# compile
|
||||
mkdir -p out/${DIR}
|
||||
javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
|
||||
|
||||
# package
|
||||
mkdir -p mods
|
||||
jar --create --file=mods/${DIR}.jar -C out/${DIR} .
|
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
DIR=baeldung-reflecting-named
|
||||
|
||||
# compile
|
||||
mkdir -p out/${DIR}
|
||||
javac -d out/${DIR} --module-path mods $(find ${DIR} -type f -name "*.java")
|
||||
|
||||
# package
|
||||
mkdir -p mods
|
||||
jar --create --file=mods/${DIR}.jar -C out/${DIR} .
|
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
DIR=baeldung-reflecting-unnamed
|
||||
|
||||
# compile
|
||||
mkdir -p out/${DIR}
|
||||
javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
|
||||
|
||||
# package
|
||||
mkdir -p mods
|
||||
jar --create --file=mods/${DIR}.jar -C out/${DIR} .
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/bash
|
||||
java --module-path mods \
|
||||
--add-opens baeldung.reflected/com.baeldung.reflected.internal=baeldung.reflecting.named \
|
||||
--module baeldung.reflecting.named/com.baeldung.reflecting.named.Main
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/bash
|
||||
java --module-path mods \
|
||||
--add-opens baeldung.reflected/com.baeldung.reflected.internal=baeldung.intermedium \
|
||||
--module baeldung.reflecting.named/com.baeldung.reflecting.named.MainWithForwardOpen
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/bash
|
||||
java --module-path mods \
|
||||
-javaagent:mods/baeldung-agent.jar=com.baeldung.reflected.internal.InternalNonPublicClass,com.baeldung.reflecting.named.Main \
|
||||
--module baeldung.reflecting.named/com.baeldung.reflecting.named.Main
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
java -cp "mods/*" com.baeldung.reflecting.unnamed.Main
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
java -cp "mods/*" --add-opens java.base/java.lang=ALL-UNNAMED com.baeldung.reflecting.unnamed.Main
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/bash
|
||||
java -cp "mods/*" -javaagent:mods/baeldung-agent.jar com.baeldung.reflecting.unnamed.Main
|
|
@ -0,0 +1,4 @@
|
|||
#!/bin/bash
|
||||
|
||||
rm -rf out
|
||||
rm -rf mods
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.agent;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.util.*;
|
||||
|
||||
public class LoadTimeAgent {
|
||||
public static void premain(String agentArgs, Instrumentation inst) {
|
||||
System.out.println("agentArgs: " + agentArgs);
|
||||
|
||||
if (agentArgs != null) {
|
||||
addExportsAndOpensByClassName(inst, agentArgs);
|
||||
}
|
||||
else {
|
||||
addExportsAndOpensToUnnamedModule(inst);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addExportsAndOpensByClassName(Instrumentation inst, String agentArgs) {
|
||||
String[] array = agentArgs.split(",");
|
||||
try {
|
||||
String className1 = array[0];
|
||||
String className2 = array[1];
|
||||
Class<?> clazz1 = Class.forName(className1);
|
||||
Class<?> clazz2 = Class.forName(className2);
|
||||
|
||||
Module srcModule = clazz1.getModule();
|
||||
Module targetModule = clazz2.getModule();
|
||||
redefineModule(inst, srcModule, targetModule);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void addExportsAndOpensToUnnamedModule(Instrumentation inst) {
|
||||
Module unnamedModule = ClassLoader.getSystemClassLoader().getUnnamedModule();
|
||||
Set<Module> modules = ModuleLayer.boot().modules();
|
||||
|
||||
for (Module m : modules) {
|
||||
redefineModule(inst, m, unnamedModule);
|
||||
}
|
||||
}
|
||||
|
||||
private static void redefineModule(Instrumentation inst, Module src, Module target) {
|
||||
// prepare extra reads
|
||||
Set<Module> extraReads = Collections.singleton(target);
|
||||
|
||||
// prepare extra exports
|
||||
Set<String> packages = src.getPackages();
|
||||
Map<String, Set<Module>> extraExports = new HashMap<>();
|
||||
for (String pkg : packages) {
|
||||
extraExports.put(pkg, extraReads);
|
||||
}
|
||||
|
||||
// prepare extra opens
|
||||
Map<String, Set<Module>> extraOpens = new HashMap<>();
|
||||
for (String pkg : packages) {
|
||||
extraOpens.put(pkg, extraReads);
|
||||
}
|
||||
|
||||
// prepare extra uses
|
||||
Set<Class<?>> extraUses = Collections.emptySet();
|
||||
|
||||
// prepare extra provides
|
||||
Map<Class<?>, List<Class<?>>> extraProvides = Collections.emptyMap();
|
||||
|
||||
// redefine module
|
||||
inst.redefineModule(src, extraReads, extraExports, extraOpens, extraUses, extraProvides);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
Premain-Class: com.baeldung.agent.LoadTimeAgent
|
||||
Can-Redefine-Classes: true
|
||||
Can-Retransform-Classes: true
|
|
@ -0,0 +1,3 @@
|
|||
module baeldung.agent {
|
||||
requires java.instrument;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.intermedium;
|
||||
|
||||
public class ForwardOpen {
|
||||
public static void addOpens(Class<?> clazz1, Class<?> clazz2) {
|
||||
Module currentModule = ForwardOpen.class.getModule();
|
||||
Module srcModule = clazz1.getModule();
|
||||
Module targetModule = clazz2.getModule();
|
||||
|
||||
System.out.println("current module: " + currentModule.getName());
|
||||
System.out.println("source module: " + srcModule.getName());
|
||||
System.out.println("target module: " + targetModule.getName());
|
||||
|
||||
srcModule.addOpens(clazz1.getPackageName(), targetModule);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module baeldung.intermedium {
|
||||
exports com.baeldung.intermedium;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.exported;
|
||||
|
||||
class ExportedNonPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("ExportedNonPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("ExportedNonPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.exported;
|
||||
|
||||
public class ExportedPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("ExportedPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("ExportedPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.internal;
|
||||
|
||||
class InternalNonPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("InternalNonPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("InternalNonPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.internal;
|
||||
|
||||
public class InternalPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("InternalPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("InternalPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.opened;
|
||||
|
||||
class OpenedNonPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("OpenedNonPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("OpenedNonPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.reflected.opened;
|
||||
|
||||
public class OpenedPublicClass {
|
||||
public static void testPublicStaticMethod() {
|
||||
System.out.println("OpenedPublicClass.testPublicStaticMethod()");
|
||||
}
|
||||
|
||||
private static void testPrivateStaticMethod() {
|
||||
System.out.println("OpenedPublicClass.testPrivateStaticMethod()");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module baeldung.reflected {
|
||||
exports com.baeldung.reflected.exported;
|
||||
opens com.baeldung.reflected.opened;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.reflecting.named;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Class<?> clazz = Class.forName("com.baeldung.reflected.internal.InternalNonPublicClass");
|
||||
Method method = clazz.getDeclaredMethod("testPrivateStaticMethod");
|
||||
method.setAccessible(true);
|
||||
method.invoke(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.reflecting.named;
|
||||
|
||||
import com.baeldung.intermedium.ForwardOpen;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class MainWithForwardOpen {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Class<?> currentClass = Main.class;
|
||||
Class<?> clazz = Class.forName("com.baeldung.reflected.internal.InternalNonPublicClass");
|
||||
|
||||
ForwardOpen.addOpens(clazz, currentClass);
|
||||
|
||||
Method method = clazz.getDeclaredMethod("testPrivateStaticMethod");
|
||||
method.setAccessible(true);
|
||||
method.invoke(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module baeldung.reflecting.named {
|
||||
requires baeldung.intermedium;
|
||||
requires baeldung.reflected;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.reflecting.unnamed;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Class<?> clazz = StringBuilder.class;
|
||||
Field f = clazz.getDeclaredField("serialVersionUID");
|
||||
f.setAccessible(true);
|
||||
Object value = f.get(null);
|
||||
System.out.println(value);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue