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

The following examples show how to use net.sf.cglib.proxy.Enhancer#setSuperclass() . 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: 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 2
Source File: ComponentInstantiationPostProcessor.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    if (_interceptors != null && _interceptors.size() > 0) {
        if (ComponentMethodInterceptable.class.isAssignableFrom(beanClass)) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(beanClass);
            enhancer.setCallbacks(getCallbacks());
            enhancer.setCallbackFilter(getCallbackFilter());
            enhancer.setNamingPolicy(ComponentNamingPolicy.INSTANCE);

            Object bean = enhancer.create();
            return bean;
        }
    }
    return null;
}
 
Example 3
Source File: LibCGDecoratorGenerator.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <T, D extends Decorator<T>> DecoratorGenerator.Meta<T, D> implement(Class<T> type, Class<D> decorator) {
    final Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(decorator);
    enhancer.setCallbackType(MethodInterceptor.class);
    final Class<? extends D> impl = enhancer.createClass();
    enhancer.setCallback(new Callback<>(type, decorator));
    return new CGMeta(type, decorator, impl, enhancer);
}
 
Example 4
Source File: AopUtils.java    From nuls with MIT License 5 votes vote down vote up
public static final <T> T createProxy(Class<T> clazz,MethodInterceptor interceptor) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(new NulsMethodInterceptor(interceptor));

    return (T) enhancer.create();
}
 
Example 5
Source File: ProxyIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
    // given
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(PersonService.class);
    enhancer.setCallback((FixedValue) () -> "Hello Tom!");
    PersonService proxy = (PersonService) enhancer.create();

    // when
    String res = proxy.sayHello(null);

    // then
    assertEquals("Hello Tom!", res);
}
 
Example 6
Source File: Cglib2AopProxy.java    From tiny-spring with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProxy() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(advised.getTargetSource().getTargetClass());
	enhancer.setInterfaces(advised.getTargetSource().getInterfaces());
	/** Enhancer.java中setCallback方法源码
	 *	public void setCallback(final Callback callback) {
	 *       setCallbacks(new Callback[]{ callback });
	 *  }
    */
    // 设置回调方法
	enhancer.setCallback(new DynamicAdvisedInterceptor(advised));
	Object enhanced = enhancer.create();
	return enhanced;
}
 
Example 7
Source File: SerializationTest.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T proxyFor(Class<T> type) throws Exception {
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(TestCommand.class);
  enhancer.setCallbackType(NoOp.class);
  Class<T> enhanced = enhancer.createClass();
  return enhanced.newInstance();
}
 
Example 8
Source File: ProxyDetectionTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testCglibProxy() {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Person.class);
    enhancer.setCallback((FixedValue) () -> null);
    Person proxyObject = (Person) enhancer.create();
    assertTrue(ClassUtils.isProxy(proxyObject.getClass()));
}
 
Example 9
Source File: RedisPool.java    From redis-limiter with Apache License 2.0 5 votes vote down vote up
public static Jedis getJedis() {
    JedisPoolProxy proxy = new JedisPoolProxy(jedisPool);
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(Jedis.class);
    enhancer.setCallback(proxy);
    return (Jedis) enhancer.create();
}
 
Example 10
Source File: CGLIBLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Class getProxyFactory(Class persistentClass, Class[] interfaces)
		throws HibernateException {
	Enhancer e = new Enhancer();
	e.setSuperclass( interfaces.length == 1 ? persistentClass : null );
	e.setInterfaces(interfaces);
	e.setCallbackTypes(new Class[]{
		InvocationHandler.class,
		NoOp.class,
  		});
 		e.setCallbackFilter(FINALIZE_FILTER);
 		e.setUseFactory(false);
	e.setInterceptDuringConstruction( false );
	return e.createClass();
}
 
Example 11
Source File: MarsBeanProxy.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 获取代理对象
 *
 * @param clazz bean的class
 * @return 对象
 */
public Object getProxy(Class<?> clazz) {
	enhancer = new Enhancer();
	// 设置需要创建子类的类
	enhancer.setSuperclass(clazz);
	enhancer.setCallback(this);
	// 通过字节码技术动态创建子类实例
	return enhancer.create();
}
 
Example 12
Source File: CGLibProxy.java    From thinking-in-spring with Apache License 2.0 5 votes vote down vote up
public Object createProxyObject(Object obj) {
    this.targetObject = obj;
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(obj.getClass());
    enhancer.setCallback(this);
    Object proxyObj = enhancer.create();
    // 返回代理对象
    return proxyObj;
}
 
Example 13
Source File: JedisCglibProxyFactory.java    From pepper-metrics with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T getProxy(Class<T> clz, String namespace, Class[] argumentTypes, Object[] arguments) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clz);
    enhancer.setCallback(new JedisMethodInterceptor(namespace));
    return (T) enhancer.create(argumentTypes, arguments);
}
 
Example 14
Source File: TransactionInterceptor.java    From JavaBase with MIT License 5 votes vote down vote up
public Object createProxy() {
  Enhancer enhancer = new Enhancer();
  //设置代理类为目标类的子类
  enhancer.setSuperclass(this.target.getClass());
  //设置拦截器为回调函数
  enhancer.setCallback(this);
  return enhancer.create();
}
 
Example 15
Source File: CglibProxyTest.java    From thinking-in-spring with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // 代理类class文件存入本地磁盘方便我们反编译查看源码
    System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
    // 通过CGLIB动态代理获取代理对象的过程
    Enhancer enhancer = new Enhancer();
    // 设置enhancer对象的父类
    enhancer.setSuperclass(HelloService.class);
    // 设置enhancer的回调对象
    enhancer.setCallback(new MyMethodInterceptor());
    // 创建代理对象
    HelloService proxy = (HelloService) enhancer.create();
    // 通过代理对象调用目标方法
    proxy.sayHello();
}
 
Example 16
Source File: Client.java    From code with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(RealSubject.class);
    enhancer.setCallback(new DemoMethodInterceptor());
    Subject subject = (Subject) enhancer.create();
    subject.request();
}
 
Example 17
Source File: NoHeapJvmOomExecutor.java    From chaosblade-exec-jvm with Apache License 2.0 5 votes vote down vote up
@Override
protected void innerRun(EnhancerModel enhancerModel, JvmOomConfiguration jvmOomConfiguration) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(OomObject.class);
    enhancer.setUseCache(false);
    enhancer.setCallback(new MethodInterceptor() {
        @Override
        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
            throws Throwable {
            return proxy.invokeSuper(obj, args);
        }
    });
    enhancer.create();

}
 
Example 18
Source File: DynamicProxyPerfClient.java    From JavaDesignPattern with Apache License 2.0 5 votes vote down vote up
private static void testCglibExecution() {
  MethodInterceptor methodInterceptor = new SubjectInterceptor();
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(ConcreteSubject.class);
  enhancer.setCallback(methodInterceptor);
  ISubject subject = (ISubject) enhancer.create();
  long start = System.currentTimeMillis();
  for (int i = 0; i < execution; i++) {
    subject.action();
  }
  long stop = System.currentTimeMillis();
  LOG.info("cglib execution time : {} ms", stop - start);
}
 
Example 19
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 20
Source File: ResourceInjectorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private FieldTarget getEnhancedObject() {
    Enhancer e = new Enhancer();
    e.setSuperclass(FieldTarget.class);
    e.setCallback(new CallInterceptor());
    return (FieldTarget)e.create();
}