BAEL-1699 - Java 9 illegal reflective access warning

This commit is contained in:
515882294 2022-04-07 22:06:01 +08:00
parent 9892d9ba83
commit 4b11cc78b0
28 changed files with 287 additions and 0 deletions

View File

@ -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} .

View File

@ -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} .

View File

@ -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} .

View File

@ -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} .

View File

@ -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} .

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
#!/bin/bash
java -cp "mods/*" com.baeldung.reflecting.unnamed.Main

View File

@ -0,0 +1,2 @@
#!/bin/bash
java -cp "mods/*" --add-opens java.base/java.lang=ALL-UNNAMED com.baeldung.reflecting.unnamed.Main

View File

@ -0,0 +1,2 @@
#!/bin/bash
java -cp "mods/*" -javaagent:mods/baeldung-agent.jar com.baeldung.reflecting.unnamed.Main

View File

@ -0,0 +1,4 @@
#!/bin/bash
rm -rf out
rm -rf mods

View File

@ -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 uses
Map<Class<?>, List<Class<?>>> extraProvides = Collections.emptyMap();
// redefine module
inst.redefineModule(src, extraReads, extraExports, extraOpens, extraUses, extraProvides);
}
}

View File

@ -0,0 +1,3 @@
Premain-Class: com.baeldung.agent.LoadTimeAgent
Can-Redefine-Classes: true
Can-Retransform-Classes: true

View File

@ -0,0 +1,3 @@
module baeldung.agent {
requires java.instrument;
}

View File

@ -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);
}
}

View File

@ -0,0 +1,3 @@
module baeldung.intermedium {
exports com.baeldung.intermedium;
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -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()");
}
}

View File

@ -0,0 +1,4 @@
module baeldung.reflected {
exports com.baeldung.reflected.exported;
opens com.baeldung.reflected.opened;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,4 @@
module baeldung.reflecting.named {
requires baeldung.intermedium;
requires baeldung.reflected;
}

View File

@ -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);
}
}