使用cglib 实例


package com.ssc.gc.hook;

public interface Hook {
public void hook();
}



package com.ssc.gc.hook;

import com.ssc.gc.hook.annotation.Surround;

public class ConcreateHook extends AbstractHook{

@Surround
public void hook(){
super.hook();
System.out.println("Concreate Hook run!");
}
}




package com.ssc.gc.hook;

public interface Trigger {
public void trigger();
}





package com.ssc.gc.hook;

public class ConcreateTrigger implements Trigger{

@Override
public void trigger() {

System.out.println("Concreate Trigger run!");

}

}




package com.ssc.gc.hook;

public class AbstractHook implements Hook{

Trigger trigger;

@Override
public void hook() {
System.out.println("abstractHook speak");
trigger.trigger();
}

public Trigger getTrigger() {
return trigger;
}

public void setTrigger(Trigger trigger) {
this.trigger = trigger;
}

}





package com.ssc.gc.hook.annotation;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target({ METHOD })
public @interface Surround {
/**
*
*/
String name() default "";

/**
*
*/
boolean required() default true;
}





package com.ssc.gc.hook.cglibproxy;

import java.lang.reflect.Method;

import com.ssc.gc.hook.annotation.Surround;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class SurroundAnnoProxy implements MethodInterceptor{

@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
Surround surround = method.getAnnotation(Surround.class);
if(surround == null){
return proxy.invokeSuper(obj, args);
}

System.out.println("do something after original object call");
Object result = proxy.invokeSuper(obj, args);
System.out.println("do something after original object call");

return result;
}

}




package com.ssc.gc.hook;

import java.util.HashSet;
import java.util.Set;

import net.sf.cglib.proxy.Enhancer;

import org.junit.Test;

import cglib.CallBackObj;

import com.ssc.gc.hook.cglibproxy.SurroundAnnoProxy;
import com.ssc.gc.sample.metrics.MetricsMonitorProcessor;

public class HookClient {

public static void main(String[] args) {
Trigger trigger = new ConcreateTrigger();
ConcreateHook hook = new ConcreateHook();
hook.setTrigger(trigger);
hook.hook();
}

@Test
public void test1(){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(ConcreateHook.class);
enhancer.setInterfaces(getInterfaces(ConcreateHook.class).toArray(new Class<?>[0]));
enhancer.setCallback(new SurroundAnnoProxy());
ConcreateHook mmp = (ConcreateHook)enhancer.create();
mmp.setTrigger(new ConcreateTrigger());
mmp.hook();
}

private Set<Class<?>> getInterfaces(Class<?> clazz)
{
Set<Class<?>> interfaceList = new HashSet<Class<?>>();

if (clazz.isInterface()) {
interfaceList.add(clazz);
}

Class<?> superClass = clazz.getSuperclass();
if (superClass != null) {
interfaceList.addAll(getInterfaces(superClass));
}

Class<?>[] superInterfaces = clazz.getInterfaces();
for (int i = 0; i < superInterfaces.length; i++) {
Class<?> superInterface = superInterfaces[i];
interfaceList.addAll(getInterfaces(superInterface));
}

return interfaceList;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值