Java Code Examples for org.springframework.cglib.proxy.Factory

The following examples show how to use org.springframework.cglib.proxy.Factory. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: CglibAopProxy.java    License: MIT License 6 votes vote down vote up
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) {
	Object other = args[0];
	if (proxy == other) {
		return true;
	}
	if (other instanceof Factory) {
		Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
		if (!(callback instanceof EqualsInterceptor)) {
			return false;
		}
		AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised;
		return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
	}
	else {
		return false;
	}
}
 
Example 2
/**
 * Create a new instance of a dynamically generated subclass implementing the
 * required lookups.
 * @param ctor constructor to use. If this is {@code null}, use the
 * no-arg constructor (no parameterization, or Setter Injection)
 * @param args arguments to use for the constructor.
 * Ignored if the {@code ctor} parameter is {@code null}.
 * @return new instance of the dynamically generated subclass
 */
public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
	Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
	Object instance;
	if (ctor == null) {
		instance = BeanUtils.instantiateClass(subclass);
	}
	else {
		try {
			Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
			instance = enhancedSubclassConstructor.newInstance(args);
		}
		catch (Exception ex) {
			throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
					"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
		}
	}
	// SPR-10785: set callbacks directly on the instance instead of in the
	// enhanced class (via the Enhancer) in order to avoid memory leaks.
	Factory factory = (Factory) instance;
	factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
			new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
			new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
	return instance;
}
 
Example 3
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 6 votes vote down vote up
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) {
	Object other = args[0];
	if (proxy == other) {
		return true;
	}
	if (other instanceof Factory) {
		Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
		if (!(callback instanceof EqualsInterceptor)) {
			return false;
		}
		AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised;
		return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
	}
	else {
		return false;
	}
}
 
Example 4
/**
 * Create a new instance of a dynamically generated subclass implementing the
 * required lookups.
 * @param ctor constructor to use. If this is {@code null}, use the
 * no-arg constructor (no parameterization, or Setter Injection)
 * @param args arguments to use for the constructor.
 * Ignored if the {@code ctor} parameter is {@code null}.
 * @return new instance of the dynamically generated subclass
 */
public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
	Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
	Object instance;
	if (ctor == null) {
		instance = BeanUtils.instantiateClass(subclass);
	}
	else {
		try {
			Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
			instance = enhancedSubclassConstructor.newInstance(args);
		}
		catch (Exception ex) {
			throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
					"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
		}
	}
	// SPR-10785: set callbacks directly on the instance instead of in the
	// enhanced class (via the Enhancer) in order to avoid memory leaks.
	Factory factory = (Factory) instance;
	factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
			new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
			new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
	return instance;
}
 
Example 5
Source Project: eagle   Source File: EagleTraceCglibProxy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) {
    Object other = args[0];
    if (proxy == other) {
        return true;
    }
    if (other instanceof Factory) {
        Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
        if (!(callback instanceof EagleTraceCglibProxy.EqualsInterceptor)) {
            return false;
        }
        AdvisedSupport otherAdvised = ((EagleTraceCglibProxy.EqualsInterceptor) callback).advised;
        return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
    } else {
        return false;
    }
}
 
Example 6
Source Project: spring-data   Source File: AbstractResolver.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object proxy(
	final String id,
	final TypeInformation<?> type,
	final A annotation,
	final ResolverCallback<A> callback) {
	final ProxyInterceptor interceptor = new ProxyInterceptor(id, type, annotation, callback, conversionService);
	if (type.getType().isInterface()) {
		final ProxyFactory proxyFactory = new ProxyFactory(new Class<?>[] { type.getType() });
		for (final Class<?> interf : type.getType().getInterfaces()) {
			proxyFactory.addInterface(interf);
		}
		proxyFactory.addInterface(LazyLoadingProxy.class);
		proxyFactory.addAdvice(interceptor);
		return proxyFactory.getProxy();
	} else {
		final Factory factory = (Factory) objenesis.newInstance(enhancedTypeFor(type.getType()));
		factory.setCallbacks(new Callback[] { interceptor });
		return factory;
	}
}
 
Example 7
/**
 * Create a new instance of a dynamically generated subclass implementing the
 * required lookups.
 * @param ctor constructor to use. If this is {@code null}, use the
 * no-arg constructor (no parameterization, or Setter Injection)
 * @param args arguments to use for the constructor.
 * Ignored if the {@code ctor} parameter is {@code null}.
 * @return new instance of the dynamically generated subclass
 */
public Object instantiate(Constructor<?> ctor, Object... args) {
	Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
	Object instance;
	if (ctor == null) {
		instance = BeanUtils.instantiateClass(subclass);
	}
	else {
		try {
			Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
			instance = enhancedSubclassConstructor.newInstance(args);
		}
		catch (Exception ex) {
			throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
					"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
		}
	}
	// SPR-10785: set callbacks directly on the instance instead of in the
	// enhanced class (via the Enhancer) in order to avoid memory leaks.
	Factory factory = (Factory) instance;
	factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
			new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
			new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
	return instance;
}
 
Example 8
Source Project: lams   Source File: CglibAopProxy.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) {
	Object other = args[0];
	if (proxy == other) {
		return true;
	}
	if (other instanceof Factory) {
		Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
		if (!(callback instanceof EqualsInterceptor)) {
			return false;
		}
		AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised;
		return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
	}
	else {
		return false;
	}
}
 
Example 9
Source Project: spring4-understanding   Source File: CglibAopProxy.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) {
	Object other = args[0];
	if (proxy == other) {
		return true;
	}
	if (other instanceof Factory) {
		Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
		if (!(callback instanceof EqualsInterceptor)) {
			return false;
		}
		AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised;
		return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
	}
	else {
		return false;
	}
}
 
Example 10
/**
 * Create a new instance of a dynamically generated subclass implementing the
 * required lookups.
 * @param ctor constructor to use. If this is {@code null}, use the
 * no-arg constructor (no parameterization, or Setter Injection)
 * @param args arguments to use for the constructor.
 * Ignored if the {@code ctor} parameter is {@code null}.
 * @return new instance of the dynamically generated subclass
 */
public Object instantiate(Constructor<?> ctor, Object... args) {
	Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
	Object instance;
	if (ctor == null) {
		instance = BeanUtils.instantiate(subclass);
	}
	else {
		try {
			Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
			instance = enhancedSubclassConstructor.newInstance(args);
		}
		catch (Exception ex) {
			throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
					"Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
		}
	}
	// SPR-10785: set callbacks directly on the instance instead of in the
	// enhanced class (via the Enhancer) in order to avoid memory leaks.
	Factory factory = (Factory) instance;
	factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
			new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
			new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
	return instance;
}
 
Example 11
Source Project: spring-cloud-gcp   Source File: LazyUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static SimpleLazyDynamicInvocationHandler getProxy(Object object) {
	if (Proxy.isProxyClass(object.getClass())
			&& (Proxy.getInvocationHandler(object) instanceof SimpleLazyDynamicInvocationHandler)) {
		return (SimpleLazyDynamicInvocationHandler) Proxy
				.getInvocationHandler(object);
	}
	else if (object instanceof Factory) {
		Callback[] callbacks = ((Factory) object).getCallbacks();
		if (callbacks != null && callbacks.length == 1
				&& callbacks[0] instanceof SimpleLazyDynamicInvocationHandler) {
			return (SimpleLazyDynamicInvocationHandler) callbacks[0];
		}
	}
	return null;
}
 
Example 12
/**
 * Create a new instance of a dynamically generated subclass implementing the
 * required lookups.
 * @param ctor constructor to use. If this is {@code null}, use the
 * no-arg constructor (no parameterization, or Setter Injection)
 * @param args arguments to use for the constructor.
 * Ignored if the {@code ctor} parameter is {@code null}.
 * @return new instance of the dynamically generated subclass
 */
Object instantiate(Constructor<?> ctor, Object[] args) {
	Class<?> subclass = createEnhancedSubclass(this.beanDefinition);

	Object instance;
	if (ctor == null) {
		instance = BeanUtils.instantiate(subclass);
	}
	else {
		try {
			Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
			instance = enhancedSubclassConstructor.newInstance(args);
		}
		catch (Exception e) {
			throw new BeanInstantiationException(this.beanDefinition.getBeanClass(), String.format(
				"Failed to invoke construcor for CGLIB enhanced subclass [%s]", subclass.getName()), e);
		}
	}

	// SPR-10785: set callbacks directly on the instance instead of in the
	// enhanced class (via the Enhancer) in order to avoid memory leaks.
	Factory factory = (Factory) instance;
	factory.setCallbacks(new Callback[] { NoOp.INSTANCE,//
		new LookupOverrideMethodInterceptor(beanDefinition, owner),//
		new ReplaceOverrideMethodInterceptor(beanDefinition, owner) });

	return instance;
}
 
Example 13
Source Project: COLA   Source File: MockHelper.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Object createMockFor(Class clazz, MethodInterceptor interceptor){
    Class proxyCls = createMockClass(clazz);
    Factory proxy = (Factory)newInstance(proxyCls);
    proxy.setCallbacks(new Callback[] {interceptor});
    return proxy;
}
 
Example 14
Source Project: conf4j   Source File: CglibStaticConfigurationInitializer.java    License: MIT License 4 votes vote down vote up
private CglibStaticConfigurationMethodInterceptor getMethodInterceptor() {
    return (CglibStaticConfigurationMethodInterceptor) ((Factory) configuration).getCallback(ProxyCallbackFilter.CONFIGURATION);
}
 
Example 15
Source Project: conf4j   Source File: CglibDynamicConfigurationInitializer.java    License: MIT License 4 votes vote down vote up
private CglibDynamicConfigurationMethodInterceptor getMethodInterceptor() {
    return (CglibDynamicConfigurationMethodInterceptor) ((Factory) configuration).getCallback(ProxyCallbackFilter.CONFIGURATION);
}
 
Example 16
Source Project: spring-cloud-gcp   Source File: LazyUtil.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Returns a proxy that lazily loads the value provided by a supplier. The proxy also
 * stores the key(s) that can be used in case the value was not loaded. If the type of the
 * value is interface, {@link java.lang.reflect.Proxy} is used, otherwise cglib proxy is
 * used (creates a sub-class of the original type; the original class can't be final and
 * can't have final methods).
 * @param supplierFunc a function that provides the value
 * @param type the type of the value
 * @param keys Datastore key(s) that can be used when the parent entity is saved
 * @return true if the object is a proxy that was not evaluated
 */
static <T> T wrapSimpleLazyProxy(Supplier<T> supplierFunc, Class<T> type, Value keys) {
	if (type.isInterface()) {
		return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[] {type},
			new SimpleLazyDynamicInvocationHandler<T>(supplierFunc, keys));
	}
	Factory factory = (Factory) objenesis.newInstance(getEnhancedTypeFor(type));
	factory.setCallbacks(new Callback[] { new SimpleLazyDynamicInvocationHandler<T>(supplierFunc, keys) });

	return (T) factory;
}