Search in sources :

Example 6 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor 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 7 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor in project mastering-java by Kingminghuang.

the class JavaMethodAreaOOM method main.

/**
     * Under JDK 1.6 or before
     * VM Args: -XX:PermSize=10M -XX:MaxPermSize=10M
     * @param args
     */
public static void main(String[] args) {
    while (true) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(OOMObject.class);
        enhancer.setUseCache(false);
        enhancer.setCallback((MethodInterceptor) (obj, method, objArgs, methodProxy) -> methodProxy.invokeSuper(obj, objArgs));
        enhancer.create();
    }
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) Enhancer(net.sf.cglib.proxy.Enhancer)

Example 8 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor 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 9 with MethodInterceptor

use of net.sf.cglib.proxy.MethodInterceptor 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 10 with MethodInterceptor

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

Aggregations

MethodInterceptor (net.sf.cglib.proxy.MethodInterceptor)11 Enhancer (net.sf.cglib.proxy.Enhancer)10 Method (java.lang.reflect.Method)6 MethodProxy (net.sf.cglib.proxy.MethodProxy)6 ArrayList (java.util.ArrayList)2 CallbackFilter (net.sf.cglib.proxy.CallbackFilter)2 Test (org.junit.Test)2 Aop (com.blade.aop.annotation.Aop)1 DalAnnotationValidator (com.ctrip.platform.dal.dao.client.DalAnnotationValidator)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 ProjectBuildFileParserFactory (com.facebook.buck.json.ProjectBuildFileParserFactory)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 TestConsole (com.facebook.buck.testutil.TestConsole)1