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; } }