Search in sources :

Example 6 with MethodProxy

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

Example 7 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy 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();
}
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)

Example 8 with MethodProxy

use of net.sf.cglib.proxy.MethodProxy in project cxf by apache.

the class CglibProxyHelper method getProxyInternal.

@Override
protected Object getProxyInternal(ClassLoader loader, Class<?>[] interfaces, final java.lang.reflect.InvocationHandler h) {
    Class<?> superClass = null;
    List<Class<?>> theInterfaces = new ArrayList<Class<?>>();
    for (Class<?> c : interfaces) {
        if (!c.isInterface()) {
            if (superClass != null) {
                throw new IllegalArgumentException("Only a single superclass is supported");
            }
            superClass = c;
        } else {
            theInterfaces.add(c);
        }
    }
    if (superClass != null) {
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(loader);
        enhancer.setSuperclass(superClass);
        enhancer.setInterfaces(theInterfaces.toArray(new Class<?>[theInterfaces.size()]));
        enhancer.setCallback(new MethodInterceptor() {

            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                return h.invoke(obj, method, args);
            }
        });
        return enhancer.create();
    }
    return super.getProxyInternal(loader, interfaces, h);
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) ArrayList(java.util.ArrayList) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method)

Aggregations

MethodProxy (net.sf.cglib.proxy.MethodProxy)8 Method (java.lang.reflect.Method)7 Enhancer (net.sf.cglib.proxy.Enhancer)6 MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)6 Signature (net.sf.cglib.core.Signature)2 CallbackFilter (net.sf.cglib.proxy.CallbackFilter)2 Test (org.junit.Test)2 DalAnnotationValidator (com.ctrip.platform.dal.dao.client.DalAnnotationValidator)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 BigInteger (java.math.BigInteger)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Type (net.sf.cglib.asm.Type)1 Callback (net.sf.cglib.proxy.Callback)1 Factory (net.sf.cglib.proxy.Factory)1 FastClass (net.sf.cglib.reflect.FastClass)1 AxisEngine (org.apache.axis.AxisEngine)1 Service (org.apache.axis.client.Service)1 TypeMapping (org.apache.axis.encoding.TypeMapping)1