Java Code Examples for org.springframework.cglib.proxy.Enhancer#create()

The following examples show how to use org.springframework.cglib.proxy.Enhancer#create() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: DubboUtils.java    From java-master with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ALL")
public static <T> T getService(Class<T> dubboServiceClass, String group, String version, String host) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(dubboServiceClass);
    enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
        ReferenceConfig<GenericService> reference = getReferenceConfig(dubboServiceClass, group, version, host);
        GenericService genericService = reference.get();
        Object result = genericService.$invoke(method.getName(), getMethodParamType(method), args);
        String resJsonStr = OMUtils.objectMapper().writeValueAsString(result);
        return OMUtils.objectMapper().readValue(resJsonStr, method.getReturnType());
    });
    Object service = enhancer.create();
    return (T) service;
}
 
Example 7
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());
}
 
Example 8
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 9
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 10
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 11
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 12
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 13
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 14
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 15
Source File: AbstractBaseServiceImpl.java    From Roothub with GNU Affero General Public License v3.0 4 votes vote down vote up
public Object getInstance() {
	Enhancer en = new Enhancer();
	en.setSuperclass(queryWrapper.getClass());
	en.setCallback(this);
	return en.create();
}