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

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

Introduction

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

Prototype

public void setSuperclass(Class superclass) 

Source Link

Document

Set the class which the generated class will extend.

Usage

From source file:io.syndesis.runtime.Recordings.java

static public <T> T recorder(Object object, Class<T> as) {
    if (as.isInterface()) {
        // If it's just an interface, use standard java reflect proxying
        return as.cast(Proxy.newProxyInstance(as.getClassLoader(), new Class[] { as },
                new RecordingInvocationHandler(object)));
    }//from ww  w. j a  v  a  2  s  .com

    // If it's a class then use gclib to implement a subclass to implement proxying
    RecordingInvocationHandler ih = new RecordingInvocationHandler(object);
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(as);
    enhancer.setInterfaces(new Class[] { RecordingProxy.class });
    enhancer.setCallback(new org.springframework.cglib.proxy.InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            return ih.invoke(o, method, objects);
        }
    });
    return as.cast(enhancer.create());
}

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.mongodb.DBCollectionProxyFactory.java

protected static DBCursor getCursor(final DBCursor from) {
    Enhancer dbcursorEnhancer = new Enhancer();
    dbcursorEnhancer.setSuperclass(com.mongodb.DBCursor.class);

    MethodInterceptor collectionMi = new MethodInterceptor() {
        boolean _top_level = true;

        @Override/*from  w  ww.  ja  va  2s. com*/
        public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy)
                throws Throwable {
            if (_top_level) {
                try {
                    _top_level = false;
                    for (int count = 0;; count++) {
                        //DEBUG
                        //System.out.println("intercepted method: " + method.toString() + ", loop=" + count + ");

                        try {
                            Object o = methodProxy.invokeSuper(object, args);
                            return o;
                        } catch (com.mongodb.MongoException e) {
                            if (count < 60) {
                                continue;
                            }
                            throw e;
                        }
                    }
                } finally {
                    _top_level = true;
                }
            } else {
                return methodProxy.invokeSuper(object, args);
            }
        }
    };
    dbcursorEnhancer.setCallback(collectionMi);
    return (DBCursor) dbcursorEnhancer.create(
            new Class[] { DBCollection.class, DBObject.class, DBObject.class, ReadPreference.class },
            new Object[] { from.getCollection(), from.getQuery(), from.getKeysWanted(),
                    from.getReadPreference() });
}

From source file:com.mongodb.DBCollectionProxyFactory.java

/** Get the enhanced DB collection from the provided one 
 * @param db db name/*from  w  ww  .j a  v  a  2  s  .c  om*/
 * @param name collection name
 * @return the enhanced collection
 */
@SuppressWarnings("deprecation")
public static DBCollection get(final DB db, final String name, final boolean is_mock) {

    Enhancer collectionEnhancer = new Enhancer();
    collectionEnhancer
            .setSuperclass(is_mock ? com.mongodb.FongoDBCollection.class : com.mongodb.DBCollectionImpl.class);
    MethodInterceptor collectionMi = new MethodInterceptor() {
        boolean _top_level = true;

        @Override
        public Object intercept(final Object object, final Method method, final Object[] args,
                final MethodProxy methodProxy) throws Throwable {
            if (_top_level) {
                try {
                    _top_level = false;
                    for (int count = 0;; count++) {
                        //DEBUG
                        //System.out.println("intercepted method: " + method.toString() + ", loop=" + count + ");

                        try {
                            Object o = methodProxy.invokeSuper(object, args);

                            if (o instanceof DBCursor) {
                                o = getCursor((DBCursor) o);
                            }
                            return o;
                        } catch (com.mongodb.MongoException e) {
                            if (count < 60) {
                                continue;
                            }
                            throw e;
                        }
                    }
                } finally {
                    _top_level = true;
                }
            } else {
                return methodProxy.invokeSuper(object, args);
            }
        }

    };
    collectionEnhancer.setCallback(collectionMi);
    return (DBCollection) collectionEnhancer
            .create(is_mock ? new Class[] { com.mongodb.FongoDB.class, String.class }
                    : new Class[] { com.mongodb.DBApiLayer.class, String.class }, new Object[] { db, name });
}

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);
    final RecordingObject recordingObject = new RecordingObject();

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

From source file:org.reindeer.redis.jedis.JedisFactoryBean.java

@Override
public Jedis getObject() throws Exception {
    if (jedis != null) {
        return jedis;
    }/*from   w  w  w .  ja v a2  s  . c  o m*/
    Enhancer en = new Enhancer();
    en.setSuperclass(Jedis.class);
    en.setCallbackFilter(finalizeFilter);
    en.setCallbacks(new Callback[] { NoOp.INSTANCE, jedisCallback });
    jedis = (Jedis) en.create(new Class[] { String.class }, new Object[] { "JedisProxy" });
    return jedis;
}

From source file:org.reindeer.redis.shard.ShardedJedisFactoryBean.java

@Override
public ShardedJedis getObject() throws Exception {
    if (jedis != null) {
        return jedis;
    }//from  w  w w  .  j  a  va  2s.co  m
    Enhancer en = new Enhancer();
    en.setSuperclass(ShardedJedis.class);
    en.setCallbackFilter(finalizeFilter);
    en.setCallbacks(new Callback[] { NoOp.INSTANCE, jedisCallback });
    jedis = (ShardedJedis) en.create(new Class[] { List.class },
            new Object[] { Arrays.asList(new JedisShardInfo("shardedJedisProxy")) });
    return jedis;
}

From source file:com.watchrabbit.executor.spring.ExecutorAnnotationBeanPostProcessor.java

private Enhancer createProxy(Class<?> targetClass, Executor classAnnotation,
        Map<Method, Executor> annotatedMethods, final Object bean) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setCallback((InvocationHandler) (Object proxy, Method method, Object[] args) -> {
        if (annotatedMethods.containsKey(method) || classAnnotation != null) {
            Executor annotation = annotatedMethods.get(method);
            if (annotation != null) {
                return generateExecutor(annotation, method, bean, args);
            } else {
                return generateExecutor(classAnnotation, method, bean, args);
            }//from   w w  w .  ja va2 s . com
        } else {
            return method.invoke(bean, args);
        }
    });
    return enhancer;
}

From source file:cat.albirar.framework.proxy.ProxyFactory.java

/**
 * Create a proxy for the indicated type.
 * @param handler The handler// www .j  a  va2  s  . c  om
 * @param type The type, should to be a concrete class type
 * @return The proxy
 */
@SuppressWarnings("unchecked")
private <T> T newProxyForConcreteClass(org.springframework.cglib.proxy.Callback handler, Class<T> type) {
    Enhancer enhancer;

    Assert.isTrue(!Modifier.isAbstract(type.getModifiers()), "The type should to be a concrete class");

    enhancer = new Enhancer();
    enhancer.setSuperclass(type);
    enhancer.setClassLoader(type.getClassLoader());
    enhancer.setCallback(handler);
    return (T) enhancer.create();
}

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);
    enhancer.setCallback(NoOp.INSTANCE);
    enhancer.setInterfaces(new Class[] { Advised.class });
    Object enhanced = spy(enhancer.create());
    assertThat(enhanced).isNotNull();/*from www .  j  av  a  2  s . c om*/

    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;
}