Search in sources :

Example 21 with Enhancer

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

use of net.sf.cglib.proxy.Enhancer in project yyl_example by Relucent.

the class CgLibTest method main.

public static void main(String[] args) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(HelloWorld.class);
    //设置回调方法实现类  
    enhancer.setCallback(new MethodInterceptorImpl());
    //实例化已经添加回调实现的HELLOWORLD实例  
    HelloWorld my = (HelloWorld) enhancer.create();
    int result = my.print();
    System.out.println(result);
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer)

Example 23 with Enhancer

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

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

the class AsyncService method proxyClass.

private Object proxyClass(Class<?> mostSpecificClass, TrackingInvocationHandler handler, ClassLoader classLoader) {
    acceptClassType(mostSpecificClass);
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setSuperclass(mostSpecificClass);
    enhancer.setCallback(handler);
    return enhancer.create();
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer)

Example 25 with Enhancer

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

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