File tree Expand file tree Collapse file tree 2 files changed +53
-1
lines changed
memshell-party-common/src
main/java/com/reajason/javaweb/buddy
test/java/com/reajason/javaweb/buddy Expand file tree Collapse file tree 2 files changed +53
-1
lines changed Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ public static DynamicType.Builder<?> extend(DynamicType.Builder<?> builder) {
3131 @ Override
3232 public Size apply (MethodVisitor methodVisitor ,
3333 Implementation .Context implementationContext ,
34- MethodDescription instrumentedMethod ) {
34+ MethodDescription methodDescription ) {
3535 methodVisitor .visitTypeInsn (Opcodes .NEW , implementationContext .getInstrumentedType ().getInternalName ());
3636 methodVisitor .visitInsn (Opcodes .DUP );
3737 methodVisitor .visitMethodInsn (Opcodes .INVOKESPECIAL ,
Original file line number Diff line number Diff line change 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+ import static org .junit .jupiter .api .Assertions .assertEquals ;
12+ import static org .junit .jupiter .api .Assertions .assertNull ;
13+
14+ /**
15+ * @author ReaJason
16+ * @since 2025/11/8
17+ */
18+ class StaticBlockSelfConstructorCallTest {
19+ public static class MyClass {
20+ public static String message ;
21+
22+ static {
23+ System .out .println ("hello world" );
24+ }
25+
26+ public MyClass () {
27+ message = "Hello World!" ;
28+ System .out .println ("MyClass constructor called with message: " + message );
29+ }
30+
31+ public String getMessage () {
32+ return message ;
33+ }
34+ }
35+
36+ @ Test
37+ @ SneakyThrows
38+ void test () {
39+ Class <? extends MyClass > rawClass = new ByteBuddy ()
40+ .redefine (MyClass .class )
41+ .name ("MyClass" ).make ().load (StaticBlockSelfConstructorCallTest .class .getClassLoader ()).getLoaded ();
42+ assertNull (rawClass .getDeclaredField ("message" ).get (null ));
43+
44+ Class <?> redefinedClass = StaticBlockSelfConstructorCall .extend (
45+ new ByteBuddy ()
46+ .redefine (MyClass .class )
47+ .name ("MyClass" )
48+ ).make ().load (StaticBlockSelfConstructorCallTest .class .getClassLoader ())
49+ .getLoaded ();
50+ assertEquals ("Hello World!" , redefinedClass .getDeclaredField ("message" ).get (null ));
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments