Search in sources :

Example 1 with CallbackFilter

use of net.sf.cglib.proxy.CallbackFilter 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)

Aggregations

Method (java.lang.reflect.Method)1 CallbackFilter (net.sf.cglib.proxy.CallbackFilter)1 Enhancer (net.sf.cglib.proxy.Enhancer)1 MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)1 MethodProxy (net.sf.cglib.proxy.MethodProxy)1