Skip to content

Commit 62c55b2

Browse files
committed
fix: add bypassModule in static block
1 parent bbfde26 commit 62c55b2

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

memshell-party-common/src/main/java/com/reajason/javaweb/buddy/ByPassJavaModuleInterceptor.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import net.bytebuddy.asm.Advice;
44
import net.bytebuddy.dynamic.DynamicType;
5+
import net.bytebuddy.implementation.StubMethod;
6+
import org.objectweb.asm.ClassWriter;
57

68
import java.lang.reflect.Field;
79

8-
import static net.bytebuddy.matcher.ElementMatchers.isDefaultConstructor;
10+
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer;
911

1012
/**
1113
* JDK9 引入的 module 系统,只有主动声明 exports 的才能被外部访问。当前用于打破 module 的限制,使我们能像低版本一样任意反射获取方法
@@ -33,12 +35,17 @@ public static void enter(@Advice.Origin Class<?> clazz) {
3335
* Reference1: <a href="https://stackoverflow.com/questions/62664427/can-i-create-a-bytebuddy-instrumented-type-with-a-private-static-final-methodhan">stackoverflow</a>
3436
* Reference2: <a href="https://github.com/raphw/byte-buddy/issues/1153">issue</a>
3537
* <br>
36-
* 在默认构造方法中执行 byPassJdkModule 代码
38+
* 在静态代码块中执行 byPassJdkModule 代码
3739
* @param builder bytebuddy builder
3840
* @return new builder with bypass
3941
*/
4042
public static DynamicType.Builder<?> extend(DynamicType.Builder<?> builder) {
41-
return builder.visit(Advice.to(ByPassJavaModuleInterceptor.class)
42-
.on(isDefaultConstructor()));
43+
return builder
44+
.initializer(StubMethod.INSTANCE)
45+
.visit(
46+
Advice.to(ByPassJavaModuleInterceptor.class)
47+
.on(isTypeInitializer())
48+
.writerFlags(ClassWriter.COMPUTE_FRAMES)
49+
);
4350
}
4451
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.reajason.javaweb.buddy;
2+
3+
import net.bytebuddy.description.method.MethodDescription;
4+
import net.bytebuddy.dynamic.DynamicType;
5+
import net.bytebuddy.implementation.Implementation;
6+
import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
7+
import net.bytebuddy.jar.asm.MethodVisitor;
8+
import net.bytebuddy.jar.asm.Opcodes;
9+
10+
import static net.bytebuddy.matcher.ElementMatchers.isTypeInitializer;
11+
12+
/**
13+
* 在静态代码块中添加构造方法调用例如:
14+
* static {
15+
* new Injector();
16+
* }
17+
*
18+
* @author ReaJason
19+
* @since 2025/8/12
20+
*/
21+
public class StaticBlockSelfConstructorCall implements ByteCodeAppender {
22+
23+
public static StaticBlockSelfConstructorCall INSTANCE = new StaticBlockSelfConstructorCall();
24+
25+
public static DynamicType.Builder<?> extend(DynamicType.Builder<?> builder) {
26+
return builder
27+
.invokable(isTypeInitializer())
28+
.intercept(new Implementation.Simple(StaticBlockSelfConstructorCall.INSTANCE));
29+
}
30+
31+
@Override
32+
public Size apply(MethodVisitor methodVisitor,
33+
Implementation.Context implementationContext,
34+
MethodDescription instrumentedMethod) {
35+
methodVisitor.visitTypeInsn(Opcodes.NEW, implementationContext.getInstrumentedType().getInternalName());
36+
methodVisitor.visitInsn(Opcodes.DUP);
37+
methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL,
38+
implementationContext.getInstrumentedType().getInternalName(),
39+
"<init>",
40+
"()V",
41+
false);
42+
methodVisitor.visitInsn(Opcodes.POP);
43+
methodVisitor.visitInsn(Opcodes.RETURN);
44+
return new Size(2, 2);
45+
}
46+
}

0 commit comments

Comments
 (0)