Search in sources :

Example 26 with Enhancer

use of net.sf.cglib.proxy.Enhancer 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;
    }
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method) CallbackFilter(net.sf.cglib.proxy.CallbackFilter)

Example 27 with Enhancer

use of net.sf.cglib.proxy.Enhancer in project cloudstack by apache.

the class ComponentInstantiationPostProcessor method postProcessBeforeInstantiation.

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    if (_interceptors != null && _interceptors.size() > 0) {
        if (ComponentMethodInterceptable.class.isAssignableFrom(beanClass)) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(beanClass);
            enhancer.setCallbacks(getCallbacks());
            enhancer.setCallbackFilter(getCallbackFilter());
            enhancer.setNamingPolicy(ComponentNamingPolicy.INSTANCE);
            Object bean = enhancer.create();
            return bean;
        }
    }
    return null;
}
Also used : 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