Example usage for org.springframework.cglib.proxy Enhancer createClass

List of usage examples for org.springframework.cglib.proxy Enhancer createClass

Introduction

In this page you can find the example usage for org.springframework.cglib.proxy Enhancer createClass.

Prototype

public Class createClass() 

Source Link

Document

Generate a new class if necessary and return it without creating a new instance.

Usage

From source file:com.newtranx.util.cassandra.spring.MapperScannerConfigurer.java

@SuppressWarnings("unchecked")
private static <T> T createProxy(final Class<?> classToMock, final MethodInterceptor interceptor) {
    final Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(classToMock);
    enhancer.setCallbackType(interceptor.getClass());
    final Class<?> proxyClass = enhancer.createClass();
    Enhancer.registerCallbacks(proxyClass, new Callback[] { interceptor });
    return (T) ObjenesisHelper.newInstance(proxyClass);
}

From source file:com.example.post.MyConfigurationClassEnhancer.java

/**
 * Uses enhancer to generate a subclass of superclass,
 * ensuring that callbacks are registered for the new subclass.
 *//*from   ww w.  j  a  v  a  2  s  . c  om*/
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;
}

From source file:org.springframework.aop.framework.ObjenesisCglibAopProxy.java

@Override
@SuppressWarnings("unchecked")
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
    Class<?> proxyClass = enhancer.createClass();
    Object proxyInstance = null;//from w ww .  ja v  a 2 s.co m

    if (objenesis.isWorthTrying()) {
        try {
            proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
        } catch (Throwable ex) {
            logger.debug("Unable to instantiate proxy using Objenesis, "
                    + "falling back to regular proxy construction", ex);
        }
    }

    if (proxyInstance == null) {
        // Regular instantiation via default constructor...
        try {
            Constructor<?> ctor = (this.constructorArgs != null
                    ? proxyClass.getDeclaredConstructor(this.constructorArgTypes)
                    : proxyClass.getDeclaredConstructor());
            ReflectionUtils.makeAccessible(ctor);
            proxyInstance = (this.constructorArgs != null ? ctor.newInstance(this.constructorArgs)
                    : ctor.newInstance());
        } catch (Throwable ex) {
            throw new AopConfigException("Unable to instantiate proxy using Objenesis, "
                    + "and regular proxy instantiation via default constructor fails as well", ex);
        }
    }

    ((Factory) proxyInstance).setCallbacks(callbacks);
    return proxyInstance;
}

From source file:org.springframework.cloud.iot.coap.method.ResolvableMethod.java

@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);//from w  w  w.j av a 2 s .c  o m
        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;
    }
}

From source file:org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.java

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) {
    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);/*from  w w w  .  ja  v  a  2s  . com*/
        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;
    }
}

From source file:org.springframework.web.servlet.mvc.support.MvcUrlUtils.java

@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) {

    if (type.isInterface()) {
        ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
        factory.addInterface(type);/*from ww  w .  ja v a  2 s.  co  m*/
        factory.addInterface(ControllerMethodValues.class);
        factory.addAdvice(interceptor);
        return (T) factory.getProxy();
    } else {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(type);
        enhancer.setInterfaces(new Class<?>[] { ControllerMethodValues.class });
        enhancer.setCallbackType(org.springframework.cglib.proxy.MethodInterceptor.class);

        Factory factory = (Factory) OBJENESIS.newInstance(enhancer.createClass());
        factory.setCallbacks(new Callback[] { interceptor });
        return (T) factory;
    }
}