Search in sources :

Example 16 with Enhancer

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();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer)

Example 17 with Enhancer

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();
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) FixedValue(net.sf.cglib.proxy.FixedValue) StubMethod(net.bytebuddy.implementation.StubMethod) Method(java.lang.reflect.Method) ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface)

Example 18 with Enhancer

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();
    }
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) InjectionPoint(com.google.inject.spi.InjectionPoint) Callback(net.sf.cglib.proxy.Callback) BytecodeGen.newFastClass(com.google.inject.internal.BytecodeGen.newFastClass) FastClass(net.sf.cglib.reflect.FastClass)

Example 19 with Enhancer

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();
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer)

Example 20 with Enhancer

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);
}
Also used : MethodProxy(net.sf.cglib.proxy.MethodProxy) Enhancer(net.sf.cglib.proxy.Enhancer)

Aggregations

Enhancer (net.sf.cglib.proxy.Enhancer)27 MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)8 Callback (net.sf.cglib.proxy.Callback)6 Method (java.lang.reflect.Method)5 MethodProxy (net.sf.cglib.proxy.MethodProxy)5 FastClass (net.sf.cglib.reflect.FastClass)3 InjectionPoint (com.google.inject.spi.InjectionPoint)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 Factory (net.sf.cglib.proxy.Factory)2 UndeclaredThrowableStrategy (net.sf.cglib.transform.impl.UndeclaredThrowableStrategy)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ShardingJdbcException (com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException)1 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)1 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)1 BuckConfig (com.facebook.buck.cli.BuckConfig)1 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)1 CellConfig (com.facebook.buck.config.CellConfig)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Watchman (com.facebook.buck.io.Watchman)1 NULL_WATCHMAN (com.facebook.buck.io.Watchman.NULL_WATCHMAN)1