mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-22 15:10:43 +08:00
refactor: simplify memshell generate map register
This commit is contained in:
+62
@@ -0,0 +1,62 @@
|
||||
package com.reajason.javaweb.buddy;
|
||||
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.asm.AsmVisitorWrapper;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2025/2/22
|
||||
*/
|
||||
class MethodCallReplaceVisitorWrapperTest {
|
||||
|
||||
public static class ExternalClass {
|
||||
public static String externalMethod(String input) {
|
||||
return "External: " + input;
|
||||
}
|
||||
|
||||
public static String replacementMethod(String input) {
|
||||
return "Replaced: " + input;
|
||||
}
|
||||
}
|
||||
|
||||
public static class TargetClass {
|
||||
public String targetMethod(String input) {
|
||||
System.out.println("targetMethod");
|
||||
return ExternalClass.replacementMethod(input);
|
||||
}
|
||||
|
||||
public static String replacementMethod(String input) {
|
||||
return "Replaced: " + input;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() throws Exception {
|
||||
String newClassName = TargetClass.class.getName() + "Redefinition";
|
||||
DynamicType.Unloaded<TargetClass> dynamicType = new ByteBuddy()
|
||||
.redefine(TargetClass.class)
|
||||
.name(newClassName)
|
||||
.visit(new AsmVisitorWrapper
|
||||
.ForDeclaredMethods()
|
||||
.method(named("targetMethod"),
|
||||
new MethodCallReplaceVisitorWrapper(
|
||||
newClassName,
|
||||
Collections.singleton(ExternalClass.class.getName())
|
||||
)
|
||||
)
|
||||
)
|
||||
.make();
|
||||
Class<?> redefinedClass = dynamicType.load(MethodCallReplaceVisitorWrapperTest.class.getClassLoader())
|
||||
.getLoaded();
|
||||
Object object = redefinedClass.newInstance();
|
||||
Object result = object.getClass().getMethod("targetMethod", String.class).invoke(object, "xixi");
|
||||
assertEquals("Replaced: xixi", result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user