From 5c27b8062dad34885933dedce67f3a5942c85925 Mon Sep 17 00:00:00 2001 From: ReaJason Date: Sun, 16 Nov 2025 11:57:48 +0800 Subject: [PATCH] test: add static block self constructor testcase --- .../buddy/StaticBlockSelfConstructorCall.java | 2 +- .../StaticBlockSelfConstructorCallTest.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 memshell-party-common/src/test/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCallTest.java diff --git a/memshell-party-common/src/main/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCall.java b/memshell-party-common/src/main/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCall.java index 982792bb..8869f49f 100644 --- a/memshell-party-common/src/main/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCall.java +++ b/memshell-party-common/src/main/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCall.java @@ -31,7 +31,7 @@ public class StaticBlockSelfConstructorCall implements ByteCodeAppender { @Override public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext, - MethodDescription instrumentedMethod) { + MethodDescription methodDescription) { methodVisitor.visitTypeInsn(Opcodes.NEW, implementationContext.getInstrumentedType().getInternalName()); methodVisitor.visitInsn(Opcodes.DUP); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, diff --git a/memshell-party-common/src/test/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCallTest.java b/memshell-party-common/src/test/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCallTest.java new file mode 100644 index 00000000..aaed8e4f --- /dev/null +++ b/memshell-party-common/src/test/java/com/reajason/javaweb/buddy/StaticBlockSelfConstructorCallTest.java @@ -0,0 +1,52 @@ +package com.reajason.javaweb.buddy; + +import lombok.SneakyThrows; +import net.bytebuddy.ByteBuddy; +import net.bytebuddy.dynamic.DynamicType; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author ReaJason + * @since 2025/11/8 + */ +class StaticBlockSelfConstructorCallTest { + public static class MyClass { + public static String message; + + static { + System.out.println("hello world"); + } + + public MyClass() { + message = "Hello World!"; + System.out.println("MyClass constructor called with message: " + message); + } + + public String getMessage() { + return message; + } + } + + @Test + @SneakyThrows + void test() { + Class rawClass = new ByteBuddy() + .redefine(MyClass.class) + .name("MyClass").make().load(StaticBlockSelfConstructorCallTest.class.getClassLoader()).getLoaded(); + assertNull(rawClass.getDeclaredField("message").get(null)); + + Class redefinedClass = StaticBlockSelfConstructorCall.extend( + new ByteBuddy() + .redefine(MyClass.class) + .name("MyClass") + ).make().load(StaticBlockSelfConstructorCallTest.class.getClassLoader()) + .getLoaded(); + assertEquals("Hello World!", redefinedClass.getDeclaredField("message").get(null)); + } +} \ No newline at end of file