Java Code Examples for net.sf.cglib.proxy.Enhancer#setInterfaces()

The following examples show how to use net.sf.cglib.proxy.Enhancer#setInterfaces() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CglibProxy.java    From festival with Apache License 2.0 6 votes vote down vote up
@Override
public Object getProxy(ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setCallbackType(MethodInterceptor.class);

    Class<?> targetClass = support.getBeanClass();
    enhancer.setSuperclass(targetClass);
    enhancer.setInterfaces(new Class[]{FestivalProxy.class, TargetClassAware.class});
    Class<?> proxyClass = enhancer.createClass();

    Objenesis objenesis = new ObjenesisStd();
    ObjectInstantiator<?> instantiator = objenesis.getInstantiatorOf(proxyClass);
    Object proxyInstance = instantiator.newInstance();

    ((Factory) proxyInstance).setCallbacks(new Callback[]{new CglibMethodInterceptor(support)});

    return proxyInstance;
}
 
Example 2
Source File: ReflectiveParserManifest.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
ReflectiveParserImpl(Class<?> base, List<Property<?, ?>> properties) {
    InjectionChecks.checkInjectableCGLibProxyBase(base);

    this.properties = properties;
    this.propertyNames = properties.stream()
                                   .flatMap(property -> property.parser.names().stream())
                                   .collect(Collectors.toImmutableSet());

    final Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(base);
    enhancer.setInterfaces(new Class[]{ type.getRawType() });
    enhancer.setCallbackType(MethodInterceptor.class);
    enhancer.setUseFactory(true);
    this.impl = enhancer.createClass();
    this.injector = getMembersInjector((Class<T>) impl);
}
 
Example 3
Source File: ProxyFactoryFactoryImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) {
	if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) {
		throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
	}

	Enhancer en = new Enhancer();
	en.setUseCache( false );
	en.setInterceptDuringConstruction( false );
	en.setUseFactory( true );
	en.setCallbackTypes( CALLBACK_TYPES );
	en.setCallbackFilter( FINALIZE_FILTER );
	if ( superClass != null ) {
		en.setSuperclass( superClass );
	}
	if ( interfaces != null && interfaces.length > 0 ) {
		en.setInterfaces( interfaces );
	}
	proxyClass = en.createClass();
	try {
		factory = ( Factory ) proxyClass.newInstance();
	}
	catch ( Throwable t ) {
		throw new HibernateException( "Unable to build CGLIB Factory instance" );
	}
}
 
Example 4
Source File: DalTransactionManager.java    From das with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetClass) throws InstantiationException, IllegalAccessException {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setClassLoader(targetClass.getClassLoader());
    enhancer.setCallbackFilter(new TransactionalCallbackFilter());
    Callback[] callbacks = new Callback[] {new DasTransactionInterceptor(), NoOp.INSTANCE};
    enhancer.setCallbacks(callbacks);
    enhancer.setInterfaces(new Class[] {TransactionalIntercepted.class});
    return (T) enhancer.create();
}
 
Example 5
Source File: EagleRpcCglibRemoteInvoke.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
protected void init() {
    Enhancer enhancer = new Enhancer();
    enhancer.setCallback(new EagleMethodInterceptor(methodInvoke));
    enhancer.setInterfaces(new Class[]{interfaceClz});
    initMethodInvoke((T) enhancer.create());
}
 
Example 6
Source File: Cglib2AopProxy.java    From tiny-spring with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProxy() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(advised.getTargetSource().getTargetClass());
	enhancer.setInterfaces(advised.getTargetSource().getInterfaces());
	//设置代理类的通知方法,相当于设置拦截器方法
	enhancer.setCallback(new DynamicAdvisedInterceptor(advised));
	Object enhanced = enhancer.create();
	return enhanced;
}
 
Example 7
Source File: DalTransactionManager.java    From dal with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetClass) throws InstantiationException, IllegalAccessException {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(targetClass);
    enhancer.setClassLoader(targetClass.getClassLoader());
    enhancer.setCallbackFilter(new TransactionalCallbackFilter());
    Callback[] callbacks = new Callback[] {new DalTransactionInterceptor(), NoOp.INSTANCE};
    enhancer.setCallbacks(callbacks);
    enhancer.setInterfaces(new Class[] {TransactionalIntercepted.class});
    return (T) enhancer.create();
}
 
Example 8
Source File: CGLIBLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Class getProxyFactory(Class persistentClass, Class[] interfaces)
		throws HibernateException {
	Enhancer e = new Enhancer();
	e.setSuperclass( interfaces.length == 1 ? persistentClass : null );
	e.setInterfaces(interfaces);
	e.setCallbackTypes(new Class[]{
		InvocationHandler.class,
		NoOp.class,
  		});
 		e.setCallbackFilter(FINALIZE_FILTER);
 		e.setUseFactory(false);
	e.setInterceptDuringConstruction( false );
	return e.createClass();
}
 
Example 9
Source File: NodeDelegate.java    From tddl5 with Apache License 2.0 5 votes vote down vote up
public T getProxy() {
    Class proxyClass = getProxy(delegateClass.getName());
    if (proxyClass == null) {
        Enhancer enhancer = new Enhancer();
        if (delegateClass.isInterface()) { // 判断是否为接口,优先进行接口代理可以解决service为final
            enhancer.setInterfaces(new Class[] { delegateClass });
        } else {
            enhancer.setSuperclass(delegateClass);
        }
        enhancer.setCallbackTypes(new Class[] { ProxyDirect.class, ProxyInterceptor.class });
        enhancer.setCallbackFilter(new ProxyRoute());
        proxyClass = enhancer.createClass();
        // 注册proxyClass
        registerProxy(delegateClass.getName(), proxyClass);
    }

    Enhancer.registerCallbacks(proxyClass, new Callback[] { new ProxyDirect(), new ProxyInterceptor() });
    try {
        Object[] _constructorArgs = new Object[0];
        Constructor _constructor = proxyClass.getConstructor(new Class[] {});// 先尝试默认的空构造函数
        return (T) _constructor.newInstance(_constructorArgs);
    } catch (Throwable e) {
        throw new OptimizerException(e);
    } finally {
        // clear thread callbacks to allow them to be gc'd
        Enhancer.registerStaticCallbacks(proxyClass, null);
    }
}
 
Example 10
Source File: Cglib2AopProxy.java    From tiny-spring with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProxy() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(advised.getTargetSource().getTargetClass());
	enhancer.setInterfaces(advised.getTargetSource().getInterfaces());
	/** Enhancer.java中setCallback方法源码
	 *	public void setCallback(final Callback callback) {
	 *       setCallbacks(new Callback[]{ callback });
	 *  }
    */
    // 设置回调方法
	enhancer.setCallback(new DynamicAdvisedInterceptor(advised));
	Object enhanced = enhancer.create();
	return enhanced;
}
 
Example 11
Source File: Cglib2AopProxy.java    From tiny-spring with Apache License 2.0 5 votes vote down vote up
@Override
public Object getProxy() {
	Enhancer enhancer = new Enhancer();
	enhancer.setSuperclass(advised.getTargetSource().getTargetClass());
	enhancer.setInterfaces(advised.getTargetSource().getInterfaces());
	enhancer.setCallback(new DynamicAdvisedInterceptor(advised));
	Object enhanced = enhancer.create();
	return enhanced;
}
 
Example 12
Source File: CglibProxyHelper.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Object getProxyInternal(ClassLoader loader, Class<?>[] interfaces,
                                  final java.lang.reflect.InvocationHandler h) {

    Class<?> superClass = null;
    List<Class<?>> theInterfaces = new ArrayList<>();

    for (Class<?> c : interfaces) {
        if (!c.isInterface()) {
            if (superClass != null) {
                throw new IllegalArgumentException("Only a single superclass is supported");
            }
            superClass = c;
        } else {
            theInterfaces.add(c);
        }
    }
    if (superClass != null) {
        Enhancer enhancer = new Enhancer();
        enhancer.setClassLoader(loader);
        enhancer.setSuperclass(superClass);
        enhancer.setInterfaces(theInterfaces.toArray(new Class<?>[0]));
        enhancer.setCallback(new MethodInterceptor() {

            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
                throws Throwable {
                return h.invoke(obj, method, args);
            }

        });
        return enhancer.create();
    }
    return super.getProxyInternal(loader, interfaces, h);
}
 
Example 13
Source File: SeiFactoryImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Class enhanceServiceEndpointInterface(Class serviceEndpointInterface, ClassLoader classLoader) {
    Enhancer enhancer = new Enhancer();
    enhancer.setClassLoader(classLoader);
    enhancer.setSuperclass(GenericServiceEndpointWrapper.class);
    enhancer.setInterfaces(new Class[]{serviceEndpointInterface});
    enhancer.setCallbackFilter(new NoOverrideCallbackFilter(GenericServiceEndpointWrapper.class));
    enhancer.setCallbackTypes(new Class[]{NoOp.class, MethodInterceptor.class});
    enhancer.setUseFactory(false);
    enhancer.setUseCache(false);

    return enhancer.createClass();
}