Search in sources :

Example 1 with Enhancer

use of org.springframework.cglib.proxy.Enhancer in project spring-framework by spring-projects.

the class ResolvableMethod method initProxy.

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, MethodInvocationInterceptor interceptor) {
    Assert.notNull(type, "'type' must not be null");
    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);
        factory.addInterface(Supplier.class);
        factory.addAdvice(interceptor);
        return (T) factory.getProxy();
    } else {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(type);
        enhancer.setInterfaces(new Class<?>[] { Supplier.class });
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
        Class<?> proxyClass = enhancer.createClass();
        Object proxy = null;
        if (objenesis.isWorthTrying()) {
            try {
                proxy = objenesis.newInstance(proxyClass, enhancer.getUseCache());
            } catch (ObjenesisException ex) {
                logger.debug("Objenesis failed, falling back to default constructor", ex);
            }
        }
        if (proxy == null) {
            try {
                proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to instantiate proxy " + "via both Objenesis and default constructor fails as well", ex);
            }
        }
        ((Factory) proxy).setCallbacks(new Callback[] { interceptor });
        return (T) proxy;
    }
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LogFactory(org.apache.commons.logging.LogFactory) ObjenesisException(org.springframework.objenesis.ObjenesisException)

Example 2 with Enhancer

use of org.springframework.cglib.proxy.Enhancer in project spring-framework by spring-projects.

the class MvcUriComponentsBuilder method initProxy.

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) {
    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);
        factory.addInterface(MethodInvocationInfo.class);
        factory.addAdvice(interceptor);
        return (T) factory.getProxy();
    } else {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(type);
        enhancer.setInterfaces(new Class<?>[] { MethodInvocationInfo.class });
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);
        Class<?> proxyClass = enhancer.createClass();
        Object proxy = null;
        if (objenesis.isWorthTrying()) {
            try {
                proxy = objenesis.newInstance(proxyClass, enhancer.getUseCache());
            } catch (ObjenesisException ex) {
                logger.debug("Unable to instantiate controller proxy using Objenesis, " + "falling back to regular construction", ex);
            }
        }
        if (proxy == null) {
            try {
                proxy = ReflectionUtils.accessibleConstructor(proxyClass).newInstance();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to instantiate controller proxy using Objenesis, " + "and regular controller instantiation via default constructor fails as well", ex);
            }
        }
        ((Factory) proxy).setCallbacks(new Callback[] { interceptor });
        return (T) proxy;
    }
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Factory(org.springframework.cglib.proxy.Factory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LogFactory(org.apache.commons.logging.LogFactory) ObjenesisException(org.springframework.objenesis.ObjenesisException)

Example 3 with Enhancer

use of org.springframework.cglib.proxy.Enhancer in project spring-framework by spring-projects.

the class CglibAopProxy method getProxy.

@Override
public Object getProxy(ClassLoader classLoader) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating CGLIB proxy: target source is " + this.advised.getTargetSource());
    }
    try {
        Class<?> rootClass = this.advised.getTargetClass();
        Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
        Class<?> proxySuperClass = rootClass;
        if (ClassUtils.isCglibProxyClass(rootClass)) {
            proxySuperClass = rootClass.getSuperclass();
            Class<?>[] additionalInterfaces = rootClass.getInterfaces();
            for (Class<?> additionalInterface : additionalInterfaces) {
                this.advised.addInterface(additionalInterface);
            }
        }
        // Validate the class, writing log messages as necessary.
        validateClassIfNecessary(proxySuperClass, classLoader);
        // Configure CGLIB Enhancer...
        Enhancer enhancer = createEnhancer();
        if (classLoader != null) {
            enhancer.setClassLoader(classLoader);
            if (classLoader instanceof SmartClassLoader && ((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
                enhancer.setUseCache(false);
            }
        }
        enhancer.setSuperclass(proxySuperClass);
        enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
        enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
        enhancer.setStrategy(new ClassLoaderAwareUndeclaredThrowableStrategy(classLoader));
        Callback[] callbacks = getCallbacks(rootClass);
        Class<?>[] types = new Class<?>[callbacks.length];
        for (int x = 0; x < types.length; x++) {
            types[x] = callbacks[x].getClass();
        }
        // fixedInterceptorMap only populated at this point, after getCallbacks call above
        enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
        enhancer.setCallbackTypes(types);
        // Generate the proxy class and create a proxy instance.
        return createProxyClassAndInstance(enhancer, callbacks);
    } catch (CodeGenerationException | IllegalArgumentException ex) {
        throw new AopConfigException("Could not generate CGLIB subclass of class [" + this.advised.getTargetClass() + "]: " + "Common causes of this problem include using a final class or a non-visible class", ex);
    } catch (Throwable ex) {
        // TargetSource.getTarget() failed
        throw new AopConfigException("Unexpected AOP exception", ex);
    }
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer) SmartClassLoader(org.springframework.core.SmartClassLoader) Callback(org.springframework.cglib.proxy.Callback) CodeGenerationException(org.springframework.cglib.core.CodeGenerationException)

Example 4 with Enhancer

use of org.springframework.cglib.proxy.Enhancer in project spring-framework by spring-projects.

the class ConfigurationClassEnhancer method newEnhancer.

/**
	 * 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;
}
Also used : Enhancer(org.springframework.cglib.proxy.Enhancer)

Aggregations

Enhancer (org.springframework.cglib.proxy.Enhancer)4 LogFactory (org.apache.commons.logging.LogFactory)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 Factory (org.springframework.cglib.proxy.Factory)2 ObjenesisException (org.springframework.objenesis.ObjenesisException)2 CodeGenerationException (org.springframework.cglib.core.CodeGenerationException)1 Callback (org.springframework.cglib.proxy.Callback)1 SmartClassLoader (org.springframework.core.SmartClassLoader)1