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

The following examples show how to use net.sf.cglib.proxy.Enhancer#createClass() . 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: LibCGDecoratorGenerator.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <T, D extends Decorator<T>> DecoratorGenerator.Meta<T, D> implement(Class<T> type, Class<D> decorator) {
    final Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(decorator);
    enhancer.setCallbackType(MethodInterceptor.class);
    final Class<? extends D> impl = enhancer.createClass();
    enhancer.setCallback(new Callback<>(type, decorator));
    return new CGMeta(type, decorator, impl, enhancer);
}
 
Example 5
Source File: ProxyFactory.java    From businessworks with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // the constructor promises to construct 'T's
ProxyConstructor(Enhancer enhancer, InjectionPoint injectionPoint, Callback[] callbacks,
    ImmutableMap<Method, List<MethodInterceptor>> methodInterceptors) {
  this.enhanced = enhancer.createClass(); // this returns a cached class if possible
  this.injectionPoint = injectionPoint;
  this.constructor = (Constructor<T>) injectionPoint.getMember();
  this.callbacks = callbacks;
  this.methodInterceptors = methodInterceptors;
  this.fastClass = newFastClassForMember(enhanced, constructor);
  this.constructorIndex = fastClass.getIndex(constructor.getParameterTypes());
}
 
Example 6
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 7
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 8
Source File: SerializationTest.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T proxyFor(Class<T> type) throws Exception {
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(TestCommand.class);
  enhancer.setCallbackType(NoOp.class);
  Class<T> enhanced = enhancer.createClass();
  return enhanced.newInstance();
}
 
Example 9
Source File: TypesTest.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
public void shouldDeProxyCGLibProxy() throws Exception {
  Enhancer enhancer = new Enhancer();
  enhancer.setSuperclass(ArrayList.class);
  enhancer.setCallbackTypes(new Class[] { NoOp.class });
  Class<?> proxy = enhancer.createClass();

  assertEquals(Types.deProxy(proxy), ArrayList.class);
}
 
Example 10
Source File: TrivialClassCreationBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark for a trivial class creation using cglib.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public Class<?> benchmarkCglib() {
    Enhancer enhancer = new Enhancer();
    enhancer.setUseCache(false);
    enhancer.setClassLoader(newClassLoader());
    enhancer.setSuperclass(baseClass);
    enhancer.setCallbackType(NoOp.class);
    return enhancer.createClass();
}
 
Example 11
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();
}
 
Example 12
Source File: ConcurrentGetInstantiator.java    From objenesis with Apache License 2.0 5 votes vote down vote up
@Setup
public void setUp() {
   for(int i = 0; i < COUNT; i++) {
      Enhancer enhancer = new Enhancer();
      enhancer.setUseCache(false); // deactivate the cache to get a new instance each time
      enhancer.setCallbackType(NoOp.class);
      Class<?> c = enhancer.createClass();
      toInstantiate[i] = c;
   }
}