Skip to content

Commit cde6758

Browse files
committed
refactor: simplify ByPassJdkModuleInterceptor
1 parent ce2d3da commit cde6758

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

generator/src/main/java/com/reajason/javaweb/buddy/ByPassJdkModuleInterceptor.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import net.bytebuddy.dynamic.DynamicType;
88
import net.bytebuddy.implementation.FixedValue;
99
import net.bytebuddy.implementation.MethodCall;
10-
import net.bytebuddy.implementation.bytecode.assign.Assigner;
1110

1211
import java.lang.reflect.Field;
1312

@@ -20,8 +19,8 @@
2019
* @author ReaJason
2120
*/
2221
public class ByPassJdkModuleInterceptor {
23-
@Advice.OnMethodExit
24-
public static void enter(@Advice.Origin Class<?> clazz, @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) boolean returnValue) {
22+
@Advice.OnMethodEnter
23+
public static void enter(@Advice.Origin Class<?> clazz) {
2524
try {
2625
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
2726
java.lang.reflect.Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
@@ -32,7 +31,6 @@ public static void enter(@Advice.Origin Class<?> clazz, @Advice.Return(readOnly
3231
Long offset = (Long) objectFieldOffsetM.invoke(unsafe, Class.class.getDeclaredField("module"));
3332
java.lang.reflect.Method getAndSetObjectM = unsafe.getClass().getMethod("getAndSetObject", Object.class, long.class, Object.class);
3433
getAndSetObjectM.invoke(unsafe, clazz, offset, module);
35-
returnValue = true;
3634
} catch (Exception ignored) {
3735
}
3836
}
@@ -41,7 +39,7 @@ public static void enter(@Advice.Origin Class<?> clazz, @Advice.Return(readOnly
4139
* Reference1: <a href="https://stackoverflow.com/questions/62664427/can-i-create-a-bytebuddy-instrumented-type-with-a-private-static-final-methodhan">stackoverflow</a>
4240
* Reference2: <a href="https://github.com/raphw/byte-buddy/issues/1153">issue</a>
4341
* <br>
44-
* 向类中添加 byPassJdkModule 方法,并在创建静态变量初始化,使其能在静态代码块中执行 byPassJdkModule 方法
42+
* 向类中添加 byPassJdkModule 方法,并在静态代码块中执行 byPassJdkModule 方法
4543
* 值得注意的一点,builder 是不可变类型,所以都是需要重新赋值,例如以下代码示例
4644
* # code that not work
4745
* builder = new Bytebuddy().redefine(class);
@@ -59,13 +57,12 @@ public static void enter(@Advice.Origin Class<?> clazz, @Advice.Return(readOnly
5957
*/
6058
public static DynamicType.Builder<?> extend(DynamicType.Builder<?> builder) {
6159
return builder
62-
.defineField("isBypassModule", boolean.class, Visibility.PUBLIC, Ownership.STATIC)
63-
.invokable(isTypeInitializer())
64-
.intercept(MethodCall.invoke(named("byPassJdkModule")))
65-
.defineMethod("byPassJdkModule", Object.class, Visibility.PUBLIC, Ownership.STATIC)
66-
.intercept(FixedValue.value(false))
60+
.defineMethod("byPassJdkModule", void.class, Visibility.PUBLIC, Ownership.STATIC)
61+
.intercept(FixedValue.originType())
6762
.visit(new AsmVisitorWrapper.ForDeclaredMethods()
6863
.method(named("byPassJdkModule"),
69-
Advice.to(ByPassJdkModuleInterceptor.class)));
64+
Advice.to(ByPassJdkModuleInterceptor.class)))
65+
.invokable(isTypeInitializer())
66+
.intercept(MethodCall.invoke(named("byPassJdkModule")));
7067
}
7168
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.reajason.javaweb.buddy;
2+
3+
import lombok.SneakyThrows;
4+
import net.bytebuddy.ByteBuddy;
5+
import net.bytebuddy.dynamic.DynamicType;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
11+
/**
12+
* @author ReaJason
13+
* @since 2024/12/7
14+
*/
15+
class ByPassJdkModuleInterceptorTest {
16+
17+
static class TestClass {
18+
static {
19+
System.out.println("TestClass");
20+
}
21+
22+
public TestClass() {
23+
}
24+
25+
26+
public String hello() {
27+
return "hello";
28+
}
29+
}
30+
31+
@Test
32+
@SneakyThrows
33+
void test() {
34+
DynamicType.Builder<?> builder = new ByteBuddy()
35+
.redefine(TestClass.class)
36+
.name("com.reajason.javaweb.buddy.ByPassJdkModuleInterceptorTest$TestClass1");
37+
builder = ByPassJdkModuleInterceptor.extend(builder);
38+
try (DynamicType.Unloaded<?> make = builder.make()) {
39+
byte[] bytes = make.getBytes();
40+
Files.write(Paths.get("build", "classes", "TestClass1.class"), bytes);
41+
}
42+
}
43+
44+
}

0 commit comments

Comments
 (0)