Java Code Examples for net.sf.cglib.proxy.Enhancer#setClassLoader()

The following examples show how to use net.sf.cglib.proxy.Enhancer#setClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CglibProxy.java    From festival with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProxy(ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setCallbackType(MethodInterceptor.class);

    Class<?> targetClass = support.getBeanClass();
    enhancer.setSuperclass(targetClass);
    enhancer.setInterfaces(new Class[]{FestivalProxy.class, TargetClassAware.class});
    Class<?> proxyClass = enhancer.createClass();

    Objenesis objenesis = new ObjenesisStd();
    ObjectInstantiator<?> instantiator = objenesis.getInstantiatorOf(proxyClass);
    Object proxyInstance = instantiator.newInstance();

    ((Factory) proxyInstance).setCallbacks(new Callback[]{new CglibMethodInterceptor(support)});

    return proxyInstance;
}
 
Example 2
Source File: AlbianServiceAopProxy.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Object newInstance(IAlbianService service, Map<String, IAlbianServiceAopAttribute> aopAttributes) {
    this._service = service;
    this._aopAttributes = aopAttributes;
    try {
        Enhancer enhancer = new Enhancer();  //增强类
        //不同于JDK的动态代理。它不能在创建代理时传obj对 象,obj对象必须被CGLIB包来创建
        enhancer.setClassLoader(AlbianClassLoader.getInstance());

        enhancer.setSuperclass(this._service.getClass()); //设置被代理类字节码(obj将被代理类设置成父类;作为产生的代理的父类传进来的)。CGLIB依据字节码生成被代理类的子类
        enhancer.setCallback(this);    //设置回调函数,即一个方法拦截
        Object proxy = enhancer.create(); //创建代理类
        return proxy;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: ClassByImplementationBenchmark.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a benchmark of an interface implementation using cglib.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public ExampleInterface benchmarkCglib() {
    Enhancer enhancer = new Enhancer();
    enhancer.setUseCache(false);
    enhancer.setClassLoader(newClassLoader());
    enhancer.setSuperclass(baseClass);
    CallbackHelper callbackHelper = new CallbackHelper(Object.class, new Class[]{baseClass}) {
        protected Object getCallback(Method method) {
            if (method.getDeclaringClass() == baseClass) {
                return new FixedValue() {
                    public Object loadObject() {
                        return null;
                    }
                };
            } else {
                return NoOp.INSTANCE;
            }
        }
    };
    enhancer.setCallbackFilter(callbackHelper);
    enhancer.setCallbacks(callbackHelper.getCallbacks());
    return (ExampleInterface) enhancer.create();
}
 
Example 4
Source File: DalTransactionManager.java    From das with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetClass) throws InstantiationException, IllegalAccessException {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setClassLoader(targetClass.getClassLoader());
    enhancer.setCallbackFilter(new TransactionalCallbackFilter());
    Callback[] callbacks = new Callback[] {new DasTransactionInterceptor(), NoOp.INSTANCE};
    enhancer.setCallbacks(callbacks);
    enhancer.setInterfaces(new Class[] {TransactionalIntercepted.class});
    return (T) enhancer.create();
}
 
Example 5
Source File: CachedMessagePriorityDaoInterceptor.java    From hermes with Apache License 2.0 5 votes vote down vote up
public static MessagePriorityDao createProxy(MessagePriorityDao target, MySQLCacheConfigProvider config) {
	CachedMessagePriorityDaoInterceptor proxy = new CachedMessagePriorityDaoInterceptor(target, config);
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(target.getClass());
	enhancer.setCallback(proxy);
	enhancer.setClassLoader(target.getClass().getClassLoader());
	return (MessagePriorityDao) enhancer.create();
}
 
Example 6
Source File: DalTransactionManager.java    From dal with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetClass) throws InstantiationException, IllegalAccessException {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setClassLoader(targetClass.getClassLoader());
    enhancer.setCallbackFilter(new TransactionalCallbackFilter());
    Callback[] callbacks = new Callback[] {new DalTransactionInterceptor(), NoOp.INSTANCE};
    enhancer.setCallbacks(callbacks);
    enhancer.setInterfaces(new Class[] {TransactionalIntercepted.class});
    return (T) enhancer.create();
}
 
Example 7
Source File: CglibProxyHelper.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getProxyInternal(ClassLoader loader, Class<?>[] interfaces,
                                  final java.lang.reflect.InvocationHandler h) {

    Class<?> superClass = null;
    List<Class<?>> theInterfaces = new ArrayList<>();

    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<?>[0]));
        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);
}
 
Example 8
Source File: TrivialClassCreationBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark for a trivial class creation using cglib.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public Class<?> benchmarkCglib() {
    Enhancer enhancer = new Enhancer();
    enhancer.setUseCache(false);
    enhancer.setClassLoader(newClassLoader());
    enhancer.setSuperclass(baseClass);
    enhancer.setCallbackType(NoOp.class);
    return enhancer.createClass();
}
 
Example 9
Source File: SecureCoreAdminHandlerTest.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private CoreContainer getZkAwareCoreContainer(final CoreContainer cc) {
  Enhancer e = new Enhancer();
  e.setClassLoader(cc.getClass().getClassLoader());
  e.setSuperclass(CoreContainer.class);
  e.setCallback(new MethodInterceptor() {
    public Object intercept(Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {
      if (method.getName().equals("isZooKeeperAware")) {
        return Boolean.TRUE;
      }
      return method.invoke(cc, args);
    }
  });
  return (CoreContainer)e.create();
}
 
Example 10
Source File: SeiFactoryImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Class enhanceServiceEndpointInterface(Class serviceEndpointInterface, ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setSuperclass(GenericServiceEndpointWrapper.class);
    enhancer.setInterfaces(new Class[]{serviceEndpointInterface});
    enhancer.setCallbackFilter(new NoOverrideCallbackFilter(GenericServiceEndpointWrapper.class));
    enhancer.setCallbackTypes(new Class[]{NoOp.class, MethodInterceptor.class});
    enhancer.setUseFactory(false);
    enhancer.setUseCache(false);

    return enhancer.createClass();
}