use of net.sf.cglib.proxy.MethodInterceptor in project querydsl by querydsl.
the class AliasFactory method createProxy.
/**
* Create a proxy instance for the given class and path
*
* @param <A>
* @param cl type of the proxy
* @param path underlying expression
* @return proxy instance
*/
@SuppressWarnings("unchecked")
protected <A> A createProxy(Class<A> cl, Expression<?> path) {
Enhancer enhancer = new Enhancer();
enhancer.setClassLoader(AliasFactory.class.getClassLoader());
if (cl.isInterface()) {
enhancer.setInterfaces(new Class<?>[] { cl, ManagedObject.class });
} else {
enhancer.setSuperclass(cl);
enhancer.setInterfaces(new Class<?>[] { ManagedObject.class });
}
// creates one handler per proxy
MethodInterceptor handler = new PropertyAccessInvocationHandler(path, this, pathFactory, typeSystem);
enhancer.setCallback(handler);
return (A) enhancer.create();
}
use of net.sf.cglib.proxy.MethodInterceptor in project mastering-java by Kingminghuang.
the class JavaMethodAreaOOM method main.
/**
* Under JDK 1.6 or before
* VM Args: -XX:PermSize=10M -XX:MaxPermSize=10M
* @param args
*/
public static void main(String[] args) {
while (true) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(OOMObject.class);
enhancer.setUseCache(false);
enhancer.setCallback((MethodInterceptor) (obj, method, objArgs, methodProxy) -> methodProxy.invokeSuper(obj, objArgs));
enhancer.create();
}
}
use of net.sf.cglib.proxy.MethodInterceptor in project cloudstack by apache.
the class MethodCapturer method capture.
@SuppressWarnings("unchecked")
public static <T> MethodCapturer<T> capture(T obj) {
synchronized (s_cache) {
MethodCapturer<T> capturer = (MethodCapturer<T>) s_cache.get(obj);
if (capturer != null) {
return capturer;
}
final MethodCapturer<T> capturerNew = new MethodCapturer<T>();
Enhancer en = new Enhancer();
en.setSuperclass(obj.getClass());
en.setCallbacks(new Callback[] { new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
capturerNew.setMethod(arg1);
return null;
}
}, new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
return null;
}
} });
en.setCallbackFilter(new CallbackFilter() {
@Override
public int accept(Method method) {
if (method.getParameterTypes().length == 0 && method.getName().equals("finalize")) {
return 1;
}
return 0;
}
});
capturerNew.setInstance((T) en.create());
// here
if (s_cache.size() < CACHE_SIZE) {
s_cache.put(obj, capturerNew);
}
return capturerNew;
}
}
use of net.sf.cglib.proxy.MethodInterceptor in project cloudstack by apache.
the class AsyncCallbackDispatcher method getTarget.
@SuppressWarnings("unchecked")
public T getTarget() {
Class<?> clz = _targetObject.getClass();
String clzName = clz.getName();
if (clzName.contains("EnhancerByCloudStack"))
clz = clz.getSuperclass();
Enhancer en = null;
synchronized (enMap) {
en = enMap.get(clz);
if (en == null) {
en = new Enhancer();
en.setSuperclass(clz);
en.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
return null;
}
});
enMap.put(clz, en);
}
}
try {
T t = (T) en.create();
Factory factory = (Factory) t;
factory.setCallback(0, new MethodInterceptor() {
@Override
public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable {
if (arg1.getParameterTypes().length == 0 && arg1.getName().equals("finalize")) {
return null;
} else {
_callbackMethod = arg1;
_callbackMethod.setAccessible(true);
return null;
}
}
});
return t;
} catch (Throwable e) {
s_logger.error("Unexpected exception", e);
}
return null;
}
use of net.sf.cglib.proxy.MethodInterceptor in project yyl_example by Relucent.
the class EnhancerTest method main.
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(null);
enhancer.setInterfaces(new Class[] { I.class });
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
return methodProxy.invokeSuper(o, args);
}
System.out.println("->" + method.getName());
return null;
}
});
I a = I.class.cast(enhancer.create());
a.method();
}
Aggregations