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

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

Introduction

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

Prototype

public Enhancer() 

Source Link

Document

Create a new Enhancer.

Usage

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

/**
 * Creates a new CGLIB {@link Enhancer} instance.
 *///w w  w  .j a v  a 2  s. com
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;
}

From source file:io.neba.core.resourcemodels.mapping.ResourceToModelMapperTest.java

private void withSpringAopProxyFor(Class<TestModel> superclass) throws Exception {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(superclass);//from www . j  a v a2s .  co  m
    enhancer.setCallback(NoOp.INSTANCE);
    enhancer.setInterfaces(new Class[] { Advised.class });
    Object enhanced = spy(enhancer.create());
    assertThat(enhanced).isNotNull();

    this.targetSource = mock(TargetSource.class);
    TestModel unwrappedBeanInstance = new TestModel();
    doReturn(this.targetSource).when((Advised) enhanced).getTargetSource();
    doReturn(unwrappedBeanInstance).when(this.targetSource).getTarget();

    this.model = (TestModel) enhanced;
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Recorder<T> create(Class<T> cls) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(cls);// w w  w  .  j  av a2  s . c  o m
    final RecordingObject recordingObject = new RecordingObject();

    enhancer.setCallback(recordingObject);
    return new Recorder((T) enhancer.create(), recordingObject);
}

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

/**
 * Creates the CGLIB {@link Enhancer}. Subclasses may wish to override this to return a custom
 * {@link Enhancer} implementation./* ww w . j a  v  a 2  s .  c  o m*/
 */
protected Enhancer createEnhancer() {
    return new Enhancer();
}

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  ww  . j a v  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.context.annotation.ConfigurationClassEnhancer.java

/**
 * Creates a new CGLIB {@link Enhancer} instance.
 *//*from  w ww . j av  a  2s  .c om*/
private Enhancer newEnhancer(Class<?> superclass, @Nullable 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;
}

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);// ww  w  . jav a  2  s . co  m
        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  w  w  w  .j a v  a 2  s  .c o 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;
    }
}