Search in sources :

Example 1 with ObjenesisException

use of org.springframework.objenesis.ObjenesisException 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 ObjenesisException

use of org.springframework.objenesis.ObjenesisException 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 ObjenesisException

use of org.springframework.objenesis.ObjenesisException in project BroadleafCommerce by BroadleafCommerce.

the class FrameworkMvcUriComponentsBuilder 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 = 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) LogFactory(org.apache.commons.logging.LogFactory) Factory(org.springframework.cglib.proxy.Factory) ProxyFactory(org.springframework.aop.framework.ProxyFactory) ObjenesisException(org.springframework.objenesis.ObjenesisException)

Example 4 with ObjenesisException

use of org.springframework.objenesis.ObjenesisException 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 5 with ObjenesisException

use of org.springframework.objenesis.ObjenesisException 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)

Aggregations

LogFactory (org.apache.commons.logging.LogFactory)7 ProxyFactory (org.springframework.aop.framework.ProxyFactory)7 Enhancer (org.springframework.cglib.proxy.Enhancer)7 Factory (org.springframework.cglib.proxy.Factory)7 ObjenesisException (org.springframework.objenesis.ObjenesisException)7