org.springframework.cglib.proxy.Enhancer Java Examples

The following examples show how to use org.springframework.cglib.proxy.Enhancer. 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: EnhancerUtil.java    From specification-arg-resolver with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) {
   	Enhancer enhancer = new Enhancer();
	enhancer.setInterfaces(new Class[] { iface });
	enhancer.setCallback(new MethodInterceptor() {
           @Override
           public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
           	if ("toString".equals(method.getName())) {
           		return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]";
           	}
           	return proxy.invoke(targetSpec, args);
           }
       });
	
	return (T) enhancer.create();
   }
 
Example #2
Source File: CglibStaticConfigurationInstanceCreator.java    From conf4j with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T createInstance(ConfigurationModel configurationModel, ClassLoader classLoader) {
    Class<?> configurationType = configurationModel.getConfigurationType();

    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setNamingPolicy(Conf4jNamingPolicy.INSTANCE);
    enhancer.setInterceptDuringConstruction(false);
    if (configurationType.isInterface()) {
        enhancer.setInterfaces(new Class<?>[]{configurationType, Serializable.class});
    } else {
        enhancer.setSuperclass(configurationType);
        enhancer.setInterfaces(new Class<?>[]{Serializable.class});
    }
    enhancer.setCallbackFilter(new ProxyCallbackFilter(configurationModel));
    enhancer.setCallbacks(new Callback[]{
            new CglibStaticConfigurationMethodInterceptor(configurationModel),
            new SerializableNoOp(),
    });

    return (T) enhancer.create();
}
 
Example #3
Source File: CglibDynamicConfigurationInstanceCreator.java    From conf4j with MIT License 6 votes vote down vote up
@Override
public <T> T createInstance(ConfigurationModel configurationModel, ClassLoader classLoader) {
    Class<?> configurationType = configurationModel.getConfigurationType();

    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setNamingPolicy(Conf4jNamingPolicy.INSTANCE);
    enhancer.setInterceptDuringConstruction(false);
    if (configurationType.isInterface()) {
        enhancer.setInterfaces(new Class<?>[]{configurationType, DynamicConfiguration.class});
    } else {
        enhancer.setSuperclass(configurationType);
        enhancer.setInterfaces(new Class<?>[]{DynamicConfiguration.class});
    }
    enhancer.setCallbackFilter(new ProxyCallbackFilter(configurationModel));
    enhancer.setCallbacks(new Callback[]{
            new CglibDynamicConfigurationMethodInterceptor(configurationModel),
            new SerializableNoOp()
    });

    @SuppressWarnings("unchecked")
    T instance = (T) enhancer.create();
    return instance;
}
 
Example #4
Source File: Recordings.java    From syndesis with Apache License 2.0 6 votes vote down vote up
static public <T> T recorder(Object object, Class<T> as) {
    if (as.isInterface()) {
        // If it's just an interface, use standard java reflect proxying
        return as.cast(Proxy.newProxyInstance(as.getClassLoader(), new Class<?>[] { as }, new RecordingInvocationHandler(object)));
    }

    // If it's a class then use gclib to implement a subclass to implement proxying
    RecordingInvocationHandler ih = new RecordingInvocationHandler(object);
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(as);
    enhancer.setInterfaces(new Class<?>[]{RecordingProxy.class});
    enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            return ih.invoke(o, method, objects);
        }
    });
    return as.cast(enhancer.create());
}
 
Example #5
Source File: CglibAopProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
	enhancer.setInterceptDuringConstruction(false);
	enhancer.setCallbacks(callbacks);
	return (this.constructorArgs != null ?
			enhancer.create(this.constructorArgTypes, this.constructorArgs) :
			enhancer.create());
}
 
Example #6
Source File: EagleTraceCglibProxy.java    From eagle with Apache License 2.0 5 votes vote down vote up
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
    enhancer.setInterceptDuringConstruction(false);
    enhancer.setCallbacks(callbacks);
    return (this.constructorArgs != null ?
            enhancer.create(this.constructorArgTypes, this.constructorArgs) :
            enhancer.create());
}
 
Example #7
Source File: AbstractResolver.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private Class<?> enhancedTypeFor(final Class<?> type) {
	final Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(type);
	enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
	enhancer.setInterfaces(new Class[] { LazyLoadingProxy.class });
	return enhancer.createClass();
}
 
Example #8
Source File: ConfigurationClassEnhancer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new CGLIB {@link Enhancer} instance.
 */
private Enhancer newEnhancer(Class<?> superclass, ClassLoader classLoader) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(superclass);
	enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
	enhancer.setUseFactory(false);
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
	enhancer.setCallbackFilter(CALLBACK_FILTER);
	enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
	return enhancer;
}
 
Example #9
Source File: ConfigurationClassEnhancer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Uses enhancer to generate a subclass of superclass,
 * ensuring that callbacks are registered for the new subclass.
 */
private Class<?> createClass(Enhancer enhancer) {
	Class<?> subclass = enhancer.createClass();
	// Registering callbacks statically (as opposed to thread-local)
	// is critical for usage in an OSGi environment (SPR-5932)...
	Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
	return subclass;
}
 
Example #10
Source File: CglibSubclassingInstantiationStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example #11
Source File: CglibAopProxy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
	enhancer.setInterceptDuringConstruction(false);
	enhancer.setCallbacks(callbacks);
	return (this.constructorArgs != null && this.constructorArgTypes != null ?
			enhancer.create(this.constructorArgTypes, this.constructorArgs) :
			enhancer.create());
}
 
Example #12
Source File: LazyUtil.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
private static Class<?> getEnhancedTypeFor(Class<?> type) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(type);
	enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);

	return enhancer.createClass();
}
 
Example #13
Source File: CglibSubclassingInstantiationStrategy.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example #14
Source File: CglibAopProxy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
	enhancer.setInterceptDuringConstruction(false);
	enhancer.setCallbacks(callbacks);
	return (this.constructorArgs != null ?
			enhancer.create(this.constructorArgTypes, this.constructorArgs) :
			enhancer.create());
}
 
Example #15
Source File: ConfigurationClassEnhancer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new CGLIB {@link Enhancer} instance.
 */
private Enhancer newEnhancer(Class<?> superclass, ClassLoader classLoader) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(superclass);
	enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
	enhancer.setUseFactory(false);
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
	enhancer.setCallbackFilter(CALLBACK_FILTER);
	enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
	return enhancer;
}
 
Example #16
Source File: ConfigurationClassEnhancer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Uses enhancer to generate a subclass of superclass,
 * ensuring that callbacks are registered for the new subclass.
 */
private Class<?> createClass(Enhancer enhancer) {
	Class<?> subclass = enhancer.createClass();
	// Registering callbacks statically (as opposed to thread-local)
	// is critical for usage in an OSGi environment (SPR-5932)...
	Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
	return subclass;
}
 
Example #17
Source File: CglibSubclassingInstantiationStrategy.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example #18
Source File: AddEnhancerMethodProxyTest.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
private A createEnhancer(Callback cb) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(AImpl.class);

    enhancer.setCallback(cb);
    final A a = (A) enhancer.create();
    return a;
}
 
Example #19
Source File: DomMapper.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 读取 xml 信息为 java Bean
 *
 * @param doc   xml element
 * @param clazz bean Class
 * @param <T>   泛型
 * @return 对象
 */
@SuppressWarnings("unchecked")
public static <T> T readValue(final Element doc, final Class<T> clazz) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(clazz);
	enhancer.setUseCache(true);
	enhancer.setCallback(new CssQueryMethodInterceptor(clazz, doc));
	return (T) enhancer.create();
}
 
Example #20
Source File: BladeFallbackFactory.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public T create(Throwable cause) {
	final Class<T> targetType = target.type();
	final String targetName = target.name();
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(targetType);
	enhancer.setUseCache(true);
	enhancer.setCallback(new BladeFeignFallback<>(targetType, targetName, cause));
	return (T) enhancer.create();
}
 
Example #21
Source File: MockHelper.java    From COLA with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Class createMockClass(Class clazz){
    Enhancer enhancer = new Enhancer();
    enhancer.setUseFactory(true);
    enhancer.setNamingPolicy(ColaNamingPolicy.INSTANCE);
    enhancer.setSerialVersionUID(42L);
    enhancer.setSuperclass(clazz);
    enhancer.setCallbackTypes(new Class[]{MethodInterceptor.class});
    return enhancer.createClass();
}
 
Example #22
Source File: HttpClientProxyConfig.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Bean
public CloseableHttpClient httpClientProxy() {
    Enhancer e = new Enhancer();
    e.setSuperclass(CloseableHttpClient.class);
    e.setCallback((MethodInterceptor) (o, method, objects, methodProxy) ->
        {
            if (method.getName().equals("execute") && objects.length > 0 && objects[0] instanceof HttpRequest) {
                serviceAuthenticationDecorator.process((HttpRequest) objects[0]);
            }
            return method.invoke(clientChooser.chooseClient(), objects);
        }
    );
    return (CloseableHttpClient) e.create();
}
 
Example #23
Source File: ConfigurationClassEnhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Creates a new CGLIB {@link Enhancer} instance.
 */
private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(configSuperClass);
	enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
	enhancer.setUseFactory(false);
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
	enhancer.setCallbackFilter(CALLBACK_FILTER);
	enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
	return enhancer;
}
 
Example #24
Source File: ProxyFactory.java    From DesignPatterns with Apache License 2.0 5 votes vote down vote up
public Object getProxyInstance(){
    //1.工具类
    Enhancer en = new Enhancer();
    //2.设置父类
    en.setSuperclass(target.getClass());
    //3.设置回调函数
    en.setCallback(this);
    //4.创建子类(代理对象)
    return en.create();
}
 
Example #25
Source File: CglibSubclassingInstantiationStrategy.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example #26
Source File: ConfigurationClassEnhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Uses enhancer to generate a subclass of superclass,
 * ensuring that callbacks are registered for the new subclass.
 */
private Class<?> createClass(Enhancer enhancer) {
	Class<?> subclass = enhancer.createClass();
	// Registering callbacks statically (as opposed to thread-local)
	// is critical for usage in an OSGi environment (SPR-5932)...
	Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
	return subclass;
}
 
Example #27
Source File: ConfigurationClassEnhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Creates a new CGLIB {@link Enhancer} instance.
 */
private Enhancer newEnhancer(Class<?> configSuperClass, @Nullable ClassLoader classLoader) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(configSuperClass);
	enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
	enhancer.setUseFactory(false);
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
	enhancer.setCallbackFilter(CALLBACK_FILTER);
	enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
	return enhancer;
}
 
Example #28
Source File: ConfigurationClassEnhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Uses enhancer to generate a subclass of superclass,
 * ensuring that callbacks are registered for the new subclass.
 */
private Class<?> createClass(Enhancer enhancer) {
	Class<?> subclass = enhancer.createClass();
	// Registering callbacks statically (as opposed to thread-local)
	// is critical for usage in an OSGi environment (SPR-5932)...
	Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
	return subclass;
}
 
Example #29
Source File: CglibSubclassingInstantiationStrategy.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create an enhanced subclass of the bean class for the provided bean
 * definition, using CGLIB.
 */
private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(beanDefinition.getBeanClass());
	enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
	if (this.owner instanceof ConfigurableBeanFactory) {
		ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
		enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
	}
	enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
	enhancer.setCallbackTypes(CALLBACK_TYPES);
	return enhancer.createClass();
}
 
Example #30
Source File: CglibAopProxy.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
	enhancer.setInterceptDuringConstruction(false);
	enhancer.setCallbacks(callbacks);
	return (this.constructorArgs != null && this.constructorArgTypes != null ?
			enhancer.create(this.constructorArgTypes, this.constructorArgs) :
			enhancer.create());
}