提交 852aec98 编写于 作者: wu-sheng's avatar wu-sheng

1.增加构造函数拦截的测试。

上级 65679624
package test.ai.cloud.bytebuddy;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.This;
public class ConstructorInterceptor {
@RuntimeType
public void intercept(@AllArguments Object[] allArguments) {
System.out
.println("ConstructorInterceptor size:" + allArguments.length);
if(allArguments.length > 0){
System.out.println("ConstructorInterceptor param[0]=" + allArguments[0]);
}
}
}
......@@ -7,12 +7,13 @@ import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
public class Interceptor{
public class MethodInterceptor{
@RuntimeType
public Object intercept(@AllArguments Object[] allArguments, @Origin Method method, @SuperCall Callable<?> zuper){
public Object intercept(@This Object obj, @AllArguments Object[] allArguments, @Origin Method method, @SuperCall Callable<?> zuper){
try {
return "intercept_" + zuper.call();
return method.getName() + ":intercept_" + zuper.call();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
......
package test.ai.cloud.bytebuddy;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.pool.TypePool;
public class SimulateMain {
public static void main(String[] args) throws NoSuchFieldException,
SecurityException, InstantiationException, IllegalAccessException {
TypePool typePool = TypePool.Default.ofClassPath();
Object newClazzObj = new ByteBuddy()
.redefine(
typePool.describe(
"test.ai.cloud.bytebuddy.TestClass")
.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath())
.name("test.ai.cloud.bytebuddy.TestClass$$Origin")
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded().newInstance();
TestClass t22 = (TestClass)(new ByteBuddy()
.subclass(newClazzObj.getClass())
.method(named("testA"))
.intercept(MethodDelegation.to(new Interceptor()))
Class<?> newClazz = new ByteBuddy()
.redefine(
typePool.describe("test.ai.cloud.bytebuddy.TestClass")
.resolve(),
ClassFileLocator.ForClassLoader.ofClassPath())
.name("test.ai.cloud.bytebuddy.TestClass$$Origin")
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded();
TestClass t22 = (TestClass) (new ByteBuddy()
.subclass(newClazz)
.method(isMethod())
.intercept(MethodDelegation.to(new MethodInterceptor()))
.constructor(isConstructor())
.intercept(MethodDelegation.to(new ConstructorInterceptor()).andThen(SuperMethodCall.INSTANCE))
.name("test.ai.cloud.bytebuddy.TestClass")
.make()
.load(ClassLoader.getSystemClassLoader(),
ClassLoadingStrategy.Default.INJECTION).getLoaded().newInstance());
ClassLoadingStrategy.Default.INJECTION).getLoaded()
.newInstance());
//System.out.println(t22.testA("1"));
TestClass t = new TestClass();
// System.out.println(t22.testA("1"));
TestClass t = new TestClass("abc");
System.out.println(t.testA("1"));
TestClass t2 = null;
try {
t2 = (TestClass)Class.forName("test.ai.cloud.bytebuddy.TestClass").newInstance();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(t2.testA("1"));
// TestClass t2 = null;
// try {
// t2 = (TestClass) Class.forName("test.ai.cloud.bytebuddy.TestClass")
// .newInstance();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// System.out.println(t2.testA("1"));
}
}
package test.ai.cloud.bytebuddy;
public class TestClass {
public TestClass(){
//System.out.println("init:" + this.getClass().getName());
}
public TestClass(String tmp){
//System.out.println("init:" + this.getClass().getName());
}
public String testA(String aa){
// throw new RuntimeException("adfasdfas");
return "TestClass.testA";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册