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

The following examples show how to use org.springframework.cglib.proxy.MethodProxy. 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
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object oldProxy = null;
	Object target = this.targetSource.getTarget();
	try {
		oldProxy = AopContext.setCurrentProxy(proxy);
		Object retVal = methodProxy.invoke(target, args);
		return processReturnType(proxy, target, method, retVal);
	}
	finally {
		AopContext.setCurrentProxy(oldProxy);
		if (target != null) {
			this.targetSource.releaseTarget(target);
		}
	}
}
 
Example 2
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 3
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object oldProxy = null;
	Object target = this.targetSource.getTarget();
	try {
		oldProxy = AopContext.setCurrentProxy(proxy);
		Object retVal = methodProxy.invoke(target, args);
		return processReturnType(proxy, target, method, retVal);
	}
	finally {
		AopContext.setCurrentProxy(oldProxy);
		if (target != null) {
			this.targetSource.releaseTarget(target);
		}
	}
}
 
Example 4
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 5
Source Project: specification-arg-resolver   Source File: EnhancerUtil.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> T wrapWithIfaceImplementation(final Class<T> iface, final Specification<Object> targetSpec) {
   	Enhancer enhancer = new Enhancer();
	enhancer.setInterfaces(new Class[] { iface });
	enhancer.setCallback(new MethodInterceptor() {
           @Override
           public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
           	if ("toString".equals(method.getName())) {
           		return iface.getSimpleName() + "[" + proxy.invoke(targetSpec, args) + "]";
           	}
           	return proxy.invoke(targetSpec, args);
           }
       });
	
	return (T) enhancer.create();
   }
 
Example 6
Source Project: COLA   Source File: MainRecordProxy.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object invokeMethod(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    Object result = null;
    method.setAccessible(true);
    if(ColaMockito.g().getCurrentTestModel() != null
        && ColaMockito.g().getCurrentTestModel().getTestClazz().isAssignableFrom(mapperInterface)){
        ColaMockito.g().getFileDataEngine().clean();
    }
    Method oriMethod = instance.getClass().getDeclaredMethod(method.getName(), method.getParameterTypes());
    result = oriMethod.invoke(instance, objects);
    //result = method.invoke(instance, objects);
    if(!MockHelper.isMonitorMethod(method.getName())){
        return result;
    }
    //if(mapperInterface.getName().indexOf("CustomerStatHandler") > 0){
    //    System.out.println("===" + mapperInterface);
    //}

    if(ColaMockito.g().getCurrentTestModel() != null
        && ColaMockito.g().getCurrentTestModel().getTestClazz().isAssignableFrom(mapperInterface)){
        ColaMockito.g().getFileDataEngine().flushOutputData();
        ColaMockito.g().getFileDataEngine().flushInputParamsFile();
    }
    return result;
}
 
Example 7
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 8
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    String methodName = method.getName();
    switch (methodName) {
        case "equals":
            Class<?>[] methodParameterTypes = method.getParameterTypes();
            if (methodParameterTypes.length == 1 && methodParameterTypes[0] == Object.class) {
                return proxy.invokeSuper(obj, args);
            }
            break;
        case "hashCode":
        case "toString":
            if (method.getParameterTypes().length == 0) {
                return proxy.invokeSuper(obj, args);
            }
            break;
        default:
            break;
    }

    return interceptInternal(obj, method, args, proxy);
}
 
Example 9
Source Project: conf4j   Source File: CglibStaticConfigurationMethodInterceptor.java    License: MIT License 6 votes vote down vote up
@Override
protected Object interceptInternal(Object obj, Method method, Object[] args, MethodProxy proxy) {
    String propertyName = getPropertyName(method);

    if (subConfigurationProperties.contains(propertyName)) {
        // returns sub-configuration
        return getSubConfigurationProperty(propertyName);
    }

    if (subConfigurationListProperties.contains(propertyName)) {
        // returns sub-configuration list
        Integer size = (Integer) getValueProperty(propertyName + COLLECTION_SIZE_SUFFIX);
        SubConfigurationList list = getSubConfigurationListProperty(propertyName);
        return list.asUnmodifiableList(size);
    }

    // returns value property
    return getValueProperty(propertyName);
}
 
Example 10
Source Project: lams   Source File: MvcUriComponentsBuilder.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) {
	if (getControllerMethod.equals(method)) {
		return this.controllerMethod;
	}
	else if (getArgumentValues.equals(method)) {
		return this.argumentValues;
	}
	else if (getControllerType.equals(method)) {
		return this.controllerType;
	}
	else if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, obj, args);
	}
	else {
		this.controllerMethod = method;
		this.argumentValues = args;
		Class<?> returnType = method.getReturnType();
		return (void.class == returnType ? null : returnType.cast(initProxy(returnType, this)));
	}
}
 
Example 11
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 12
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 13
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) {
	if (getControllerMethod.equals(method)) {
		return this.controllerMethod;
	}
	else if (getArgumentValues.equals(method)) {
		return this.argumentValues;
	}
	else if (getControllerType.equals(method)) {
		return this.controllerType;
	}
	else if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, obj, args);
	}
	else {
		this.controllerMethod = method;
		this.argumentValues = args;
		Class<?> returnType = method.getReturnType();
		return (void.class == returnType ? null : returnType.cast(initProxy(returnType, this)));
	}
}
 
Example 14
Source Project: spring-analysis-note   Source File: ResolvableMethod.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) {
	if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, object, args);
	}
	else {
		this.invokedMethod = method;
		return null;
	}
}
 
Example 15
Source Project: spring-analysis-note   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object oldProxy = null;
	try {
		oldProxy = AopContext.setCurrentProxy(proxy);
		Object retVal = methodProxy.invoke(this.target, args);
		return processReturnType(proxy, this.target, method, retVal);
	}
	finally {
		AopContext.setCurrentProxy(oldProxy);
	}
}
 
Example 16
Source Project: spring-analysis-note   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object target = this.targetSource.getTarget();
	try {
		Object retVal = methodProxy.invoke(target, args);
		return processReturnType(proxy, target, method, retVal);
	}
	finally {
		if (target != null) {
			this.targetSource.releaseTarget(target);
		}
	}
}
 
Example 17
Source Project: spring-analysis-note   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	MethodInvocation invocation = new CglibMethodInvocation(proxy, this.target, method, args,
			this.targetClass, this.adviceChain, methodProxy);
	// If we get here, we need to create a MethodInvocation.
	Object retVal = invocation.proceed();
	retVal = processReturnType(proxy, this.target, method, retVal);
	return retVal;
}
 
Example 18
Source Project: spring-analysis-note   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
public CglibMethodInvocation(Object proxy, @Nullable Object target, Method method,
		Object[] arguments, @Nullable Class<?> targetClass,
		List<Object> interceptorsAndDynamicMethodMatchers, MethodProxy methodProxy) {

	super(proxy, target, method, arguments, targetClass, interceptorsAndDynamicMethodMatchers);

	// Only use method proxy for public methods not derived from java.lang.Object
	this.methodProxy = (Modifier.isPublic(method.getModifiers()) &&
			method.getDeclaringClass() != Object.class && !AopUtils.isEqualsMethod(method) &&
			!AopUtils.isHashCodeMethod(method) && !AopUtils.isToStringMethod(method) ?
			methodProxy : null);
}
 
Example 19
Source Project: spring-analysis-note   Source File: MvcUriComponentsBuilder.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, @Nullable MethodProxy proxy) {
	if (method.getName().equals("getControllerType")) {
		return this.controllerType;
	}
	else if (method.getName().equals("getControllerMethod")) {
		return this.controllerMethod;
	}
	else if (method.getName().equals("getArgumentValues")) {
		return this.argumentValues;
	}
	else if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, obj, args);
	}
	else {
		this.controllerMethod = method;
		this.argumentValues = args;
		Class<?> returnType = method.getReturnType();
		try {
			return (returnType == void.class ? null : returnType.cast(initProxy(returnType, this)));
		}
		catch (Throwable ex) {
			throw new IllegalStateException(
					"Failed to create proxy for controller method return type: " + method, ex);
		}
	}
}
 
Example 20
Source Project: spring-analysis-note   Source File: ResolvableMethod.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) {
	if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, object, args);
	}
	else {
		this.invokedMethod = method;
		return null;
	}
}
 
Example 21
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
	// Cast is safe, as CallbackFilter filters are used selectively.
	LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
	Assert.state(lo != null, "LookupOverride not found");
	Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
	if (StringUtils.hasText(lo.getBeanName())) {
		return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
				this.owner.getBean(lo.getBeanName()));
	}
	else {
		return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
				this.owner.getBean(method.getReturnType()));
	}
}
 
Example 22
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
	ReplaceOverride ro = (ReplaceOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
	Assert.state(ro != null, "ReplaceOverride not found");
	// TODO could cache if a singleton for minor performance optimization
	MethodReplacer mr = this.owner.getBean(ro.getMethodReplacerBeanName(), MethodReplacer.class);
	return mr.reimplement(obj, method, args);
}
 
Example 23
Source Project: java-technology-stack   Source File: ResolvableMethod.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) {
	if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, object, args);
	}
	else {
		this.invokedMethod = method;
		return null;
	}
}
 
Example 24
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object oldProxy = null;
	try {
		oldProxy = AopContext.setCurrentProxy(proxy);
		Object retVal = methodProxy.invoke(this.target, args);
		return processReturnType(proxy, this.target, method, retVal);
	}
	finally {
		AopContext.setCurrentProxy(oldProxy);
	}
}
 
Example 25
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	Object target = this.targetSource.getTarget();
	try {
		Object retVal = methodProxy.invoke(target, args);
		return processReturnType(proxy, target, method, retVal);
	}
	finally {
		if (target != null) {
			this.targetSource.releaseTarget(target);
		}
	}
}
 
Example 26
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	MethodInvocation invocation = new CglibMethodInvocation(proxy, this.target, method, args,
			this.targetClass, this.adviceChain, methodProxy);
	// If we get here, we need to create a MethodInvocation.
	Object retVal = invocation.proceed();
	retVal = processReturnType(proxy, this.target, method, retVal);
	return retVal;
}
 
Example 27
Source Project: java-technology-stack   Source File: CglibAopProxy.java    License: MIT License 5 votes vote down vote up
public CglibMethodInvocation(Object proxy, @Nullable Object target, Method method,
		Object[] arguments, @Nullable Class<?> targetClass,
		List<Object> interceptorsAndDynamicMethodMatchers, MethodProxy methodProxy) {

	super(proxy, target, method, arguments, targetClass, interceptorsAndDynamicMethodMatchers);

	// Only use method proxy for public methods not derived from java.lang.Object
	this.methodProxy = (Modifier.isPublic(method.getModifiers()) &&
			method.getDeclaringClass() != Object.class && !AopUtils.isEqualsMethod(method) &&
			!AopUtils.isHashCodeMethod(method) && !AopUtils.isToStringMethod(method) ?
			methodProxy : null);
}
 
Example 28
Source Project: java-technology-stack   Source File: ConfigurationClassEnhancer.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
	Assert.state(field != null, "Unable to find generated BeanFactory field");
	field.set(obj, args[0]);

	// Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
	// If so, call its setBeanFactory() method. If not, just exit.
	if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
		return proxy.invokeSuper(obj, args);
	}
	return null;
}
 
Example 29
Source Project: java-technology-stack   Source File: MvcUriComponentsBuilder.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, @Nullable MethodProxy proxy) {
	if (method.getName().equals("getControllerType")) {
		return this.controllerType;
	}
	else if (method.getName().equals("getControllerMethod")) {
		return this.controllerMethod;
	}
	else if (method.getName().equals("getArgumentValues")) {
		return this.argumentValues;
	}
	else if (ReflectionUtils.isObjectMethod(method)) {
		return ReflectionUtils.invokeMethod(method, obj, args);
	}
	else {
		this.controllerMethod = method;
		this.argumentValues = args;
		Class<?> returnType = method.getReturnType();
		try {
			return (returnType == void.class ? null : returnType.cast(initProxy(returnType, this)));
		}
		catch (Throwable ex) {
			throw new IllegalStateException(
					"Failed to create proxy for controller method return type: " + method, ex);
		}
	}
}
 
Example 30
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
	// Cast is safe, as CallbackFilter filters are used selectively.
	LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
	Assert.state(lo != null, "LookupOverride not found");
	Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
	if (StringUtils.hasText(lo.getBeanName())) {
		return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
				this.owner.getBean(lo.getBeanName()));
	}
	else {
		return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
				this.owner.getBean(method.getReturnType()));
	}
}