use of net.sf.cglib.proxy.Enhancer in project blade by biezhi.
the class ProxyFactory method getProxyObj.
/*
* 获得代理对象
*/
public static Object getProxyObj(Class<?> clazz) throws Exception {
Enhancer hancer = new Enhancer();
// 设置代理对象的父类
hancer.setSuperclass(clazz);
// 设置回调对象,即调用代理对象里面的方法时,实际上执行的是回调对象(里的intercept方法)。
MethodInterceptor[] methodInterceptors = filter(clazz);
if (null != methodInterceptors && methodInterceptors.length > 0) {
hancer.setCallbacks(methodInterceptors);
} else {
hancer.setCallback(NoOp.INSTANCE);
}
// 创建代理对象
return hancer.create();
}
use of net.sf.cglib.proxy.Enhancer in project byte-buddy by raphw.
the class ClassByImplementationBenchmark method benchmarkCglib.
/**
* Performs a benchmark of an interface implementation using cglib.
*
* @return The created instance, in order to avoid JIT removal.
*/
@Benchmark
public ExampleInterface benchmarkCglib() {
Enhancer enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setClassLoader(newClassLoader());
enhancer.setSuperclass(baseClass);
CallbackHelper callbackHelper = new CallbackHelper(Object.class, new Class[] { baseClass }) {
@Override
protected Object getCallback(Method method) {
if (method.getDeclaringClass() == baseClass) {
return new FixedValue() {
@Override
public Object loadObject() throws Exception {
return null;
}
};
} else {
return NoOp.INSTANCE;
}
}
};
enhancer.setCallbackFilter(callbackHelper);
enhancer.setCallbacks(callbackHelper.getCallbacks());
return (ExampleInterface) enhancer.create();
}
use of net.sf.cglib.proxy.Enhancer in project roboguice by roboguice.
the class ProxyFactory method create.
public ConstructionProxy<T> create() throws ErrorsException {
if (interceptors.isEmpty()) {
return new DefaultConstructionProxyFactory<T>(injectionPoint).create();
}
@SuppressWarnings("unchecked") Class<? extends Callback>[] callbackTypes = new Class[callbacks.length];
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] == net.sf.cglib.proxy.NoOp.INSTANCE) {
callbackTypes[i] = net.sf.cglib.proxy.NoOp.class;
} else {
callbackTypes[i] = net.sf.cglib.proxy.MethodInterceptor.class;
}
}
// to this injector. Otherwise, the proxies for each injector will waste PermGen memory
try {
Enhancer enhancer = BytecodeGen.newEnhancer(declaringClass, visibility);
enhancer.setCallbackFilter(new IndicesCallbackFilter(methods));
enhancer.setCallbackTypes(callbackTypes);
return new ProxyConstructor<T>(enhancer, injectionPoint, callbacks, interceptors);
} catch (Throwable e) {
throw new Errors().errorEnhancingClass(declaringClass, e).toException();
}
}
use of net.sf.cglib.proxy.Enhancer 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.Enhancer in project fastjson by alibaba.
the class TestCglib method test_cglib.
public void test_cglib() throws Exception {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Entity.class);
enhancer.setCallback(new Proxy());
Entity entity = (Entity) enhancer.create();
entity.setId(3);
entity.setName("Jobs");
String text = JSON.toJSONString(entity);
Assert.assertEquals("{\"id\":3,\"name\":\"Jobs\"}", text);
}
Aggregations