First drop

This commit is contained in:
Andy Clement
2012-12-07 08:43:54 -08:00
parent deff1cc219
commit 1bf580f54b
996 changed files with 61618 additions and 3 deletions

View File

@@ -0,0 +1,47 @@
package example;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class MyMethodInterceptor implements MethodInterceptor {
static List<String> interceptedMethods = new ArrayList<String>();
public static boolean callSupers = true;
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// System.out.println("intercepted:" + method);
interceptedMethods.add(method.toString());
if (callSupers) {
try {
proxy.invokeSuper(obj, args);
} catch (Throwable t) {
t.printStackTrace();
}
}
return null;
}
public static String interceptionLog() {
return "Interception list: " + interceptedMethods.toString();
}
public static void clearLog() {
interceptedMethods.clear();
}
public static void setCallSupers(boolean b) {
if (b) {
System.out.println("interceptorConfiguration: turning on super calls");
callSupers = true;
} else {
System.out.println("interceptorConfiguration: turning off super calls");
callSupers = false;
}
}
}

View File

@@ -0,0 +1,48 @@
package example;
import java.lang.reflect.UndeclaredThrowableException;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.transform.impl.UndeclaredThrowableStrategy;
public class ProxyBuilder {
static <T> T createProxyFor(Class<T> clazz, MethodInterceptor mi) {
Enhancer enhancer = new Enhancer();
// if (classLoader != null) {
// enhancer.setClassLoader(classLoader);
// if (classLoader instanceof SmartClassLoader &&
// ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
// enhancer.setUseCache(false);
// }
// }
enhancer.setSuperclass(clazz);
enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class));
enhancer.setInterfaces(null);//AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setInterceptDuringConstruction(false);
Callback[] callbacks = new Callback[] { mi };//getCallbacks(rootClass);
enhancer.setCallbacks(callbacks);
// enhancer.setCallbackFilter(new ProxyCallbackFilter(
// this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
Class<?>[] types = new Class[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
enhancer.setCallbackTypes(types);
// Generate the proxy class and create a proxy instance.
// Object proxy;
// if (this.constructorArgs != null) {
// proxy = enhancer.create(this.constructorArgTypes, this.constructorArgs);
// }
// else {
@SuppressWarnings("unchecked")
T proxy = (T) enhancer.create();
// }
return proxy;
}
}

View File

@@ -0,0 +1,37 @@
package example;
public class ProxyTestcase {
static Simple proxy = ProxyBuilder.createProxyFor(Simple.class, new MyMethodInterceptor());
public static void main(String[] args) {
run();
}
public static void run() {
MyMethodInterceptor.clearLog();
proxy.moo();
System.out.println(MyMethodInterceptor.interceptionLog());
}
public static void runMoo() {
MyMethodInterceptor.clearLog();
proxy.moo();
System.out.println(MyMethodInterceptor.interceptionLog());
}
public static void runBar() {
MyMethodInterceptor.clearLog();
// proxy.bar(1, "abc", 3L); active in ProxyTestcase2
System.out.println(MyMethodInterceptor.interceptionLog());
}
public static void configureTest1() {
MyMethodInterceptor.setCallSupers(false);
}
public static void configureTest2() {
MyMethodInterceptor.setCallSupers(true);
}
}

View File

@@ -0,0 +1,29 @@
package example;
public class ProxyTestcase2 {
static Simple2 proxy;// = ProxyBuilder.createProxyFor(Simple2.class);
public static void main(String[] args) {
run();
}
public static void run() {
MyMethodInterceptor.clearLog();
proxy.boo();
System.out.println(MyMethodInterceptor.interceptionLog());
}
public static void runMoo() {
MyMethodInterceptor.clearLog();
proxy.moo();
System.out.println(MyMethodInterceptor.interceptionLog());
}
public static void runBar() {
MyMethodInterceptor.clearLog();
proxy.bar(1, "abc", 3L);
System.out.println(MyMethodInterceptor.interceptionLog());
}
}

View File

@@ -0,0 +1,8 @@
package example;
public class Simple {
void moo() {
System.out.println("Simple.moo() running");
}
}

View File

@@ -0,0 +1,17 @@
package example;
public class Simple2 {
void moo() {
System.out.println("Simple2.moo() running");
}
void boo() {
System.out.println("Simple2.boo() running");
}
public void bar(int i, String string, long l) {
System.out.println("Simple2.bar(" + i + "," + string + "," + l + ") running");
}
}