org.springframework.core.BridgeMethodResolver Java Examples

The following examples show how to use org.springframework.core.BridgeMethodResolver. 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: AbstractFallbackJCacheOperationSource.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private JCacheOperation<?> computeCacheOperation(Method method, Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

	// First try is the method in the target class.
	JCacheOperation<?> operation = findCacheOperation(specificMethod, targetClass);
	if (operation != null) {
		return operation;
	}
	if (specificMethod != method) {
		// Fallback is to look at the original method.
		operation = findCacheOperation(method, targetClass);
		if (operation != null) {
			return operation;
		}
	}
	return null;
}
 
Example #2
Source File: HandlerMethod.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
	Assert.hasText(beanName, "Bean name is required");
	Assert.notNull(beanFactory, "BeanFactory is required");
	Assert.notNull(method, "Method is required");
	this.bean = beanName;
	this.beanFactory = beanFactory;
	Class<?> beanType = beanFactory.getType(beanName);
	if (beanType == null) {
		throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
	}
	this.beanType = ClassUtils.getUserClass(beanType);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
}
 
Example #3
Source File: ClassUtil.java    From magic-starter with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Class<?> targetClass = method.getDeclaringClass();
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtil.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	// 先找方法,再找方法上的类
	A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
	;
	if (null != annotation) {
		return annotation;
	}
	// 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
	return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #4
Source File: ClassUtils.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
    Class<?> targetClass = method.getDeclaringClass();
    // The method may be on an interface, but we need attributes from the target class.
    // If the target class is null, the method will be unchanged.
    Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
    // If we are dealing with method with generic parameters, find the original method.
    specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
    // 先找方法,再找方法上的类
    A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
    ;
    if (null != annotation) {
        return annotation;
    }
    // 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
    return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #5
Source File: ApiBootDataSourceSwitchAnnotationInterceptor.java    From api-boot with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object methodResult;
    try {
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
        Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
        Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
        // get class declared DataSourceSwitch annotation
        DataSourceSwitch dataSourceSwitch = targetClass.getDeclaredAnnotation(DataSourceSwitch.class);
        if (dataSourceSwitch == null) {
            // get declared DataSourceSwitch annotation
            dataSourceSwitch = userDeclaredMethod.getDeclaredAnnotation(DataSourceSwitch.class);
        }
        if (dataSourceSwitch != null) {
            // setting current thread use data source pool name
            DataSourceContextHolder.set(dataSourceSwitch.value());
        }
        methodResult = invocation.proceed();
    } finally {
        // remove current thread use datasource pool name
        DataSourceContextHolder.remove();
    }
    return methodResult;

}
 
Example #6
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
	Assert.hasText(beanName, "Bean name is required");
	Assert.notNull(beanFactory, "BeanFactory is required");
	Assert.notNull(method, "Method is required");
	this.bean = beanName;
	this.beanFactory = beanFactory;
	Class<?> beanType = beanFactory.getType(beanName);
	if (beanType == null) {
		throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
	}
	this.beanType = ClassUtils.getUserClass(beanType);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
	this.description = initDescription(this.beanType, this.method);
}
 
Example #7
Source File: ClassUtil.java    From blade-tool with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Class<?> targetClass = method.getDeclaringClass();
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtil.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	// 先找方法,再找方法上的类
	A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
	;
	if (null != annotation) {
		return annotation;
	}
	// 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
	return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #8
Source File: HandlerMethod.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
	Assert.hasText(beanName, "Bean name is required");
	Assert.notNull(beanFactory, "BeanFactory is required");
	Assert.notNull(method, "Method is required");
	this.bean = beanName;
	this.beanFactory = beanFactory;
	Class<?> beanType = beanFactory.getType(beanName);
	if (beanType == null) {
		throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
	}
	this.beanType = ClassUtils.getUserClass(beanType);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
}
 
Example #9
Source File: ClassUtil.java    From albedo with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
public <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Class<?> targetClass = method.getDeclaringClass();
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtil.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	// 先找方法,再找方法上的类
	A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
	if (null != annotation) {
		return annotation;
	}
	// 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
	return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #10
Source File: AnnotationsScanner.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Nullable
private static <C, R> R processMethodAnnotations(C context, int aggregateIndex, Method source,
		AnnotationsProcessor<C, R> processor, @Nullable BiPredicate<C, Class<?>> classFilter) {

	Annotation[] annotations = getDeclaredAnnotations(context, source, classFilter, false);
	R result = processor.doWithAnnotations(context, aggregateIndex, source, annotations);
	if (result != null) {
		return result;
	}
	Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(source);
	if (bridgedMethod != source) {
		Annotation[] bridgedAnnotations = getDeclaredAnnotations(context, bridgedMethod, classFilter, true);
		for (int i = 0; i < bridgedAnnotations.length; i++) {
			if (ObjectUtils.containsElement(annotations, bridgedAnnotations[i])) {
				bridgedAnnotations[i] = null;
			}
		}
		return processor.doWithAnnotations(context, aggregateIndex, source, bridgedAnnotations);
	}
	return null;
}
 
Example #11
Source File: CachingAnnotationsAspect.java    From jim-framework with Apache License 2.0 6 votes vote down vote up
private Method getSpecificmethod(ProceedingJoinPoint pjp) {
	MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
	Method method = methodSignature.getMethod();
	// The method may be on an interface, but we need attributes from the
	// target class. If the target class is null, the method will be
	// unchanged.
	Class<?> targetClass = AopProxyUtils.ultimateTargetClass(pjp.getTarget());
	if (targetClass == null && pjp.getTarget() != null) {
		targetClass = pjp.getTarget().getClass();
	}
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the
	// original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	return specificMethod;
}
 
Example #12
Source File: ResourceInspectorUtil.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Finds methods for the given annotation
 * 
 * It first finds all public member methods of the class or interface represented by objClass, 
 * including those inherited from superclasses and superinterfaces.
 * 
 * It then loops through these methods searching for a single Annotation of annotationType,
 * traversing its super methods if no annotation can be found on the given method itself.
 * 
 * @param objClass - the class
 * @param annotationType - the annotation to find
 * @return - the List of Method or an empty List
 */
@SuppressWarnings("rawtypes")
public static List<Method> findMethodsByAnnotation(Class objClass, Class<? extends Annotation> annotationType)
{

    List<Method> annotatedMethods = new ArrayList<Method>();
    Method[] methods = objClass.getMethods();
    for (Method method : methods)
    {
        Annotation annot = AnnotationUtils.findAnnotation(method, annotationType);
        if (annot != null) {
            //Just to be sure, lets make sure its not a Bridged (Generic) Method
            Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
            annotatedMethods.add(resolvedMethod);
        }
    }
    
    return annotatedMethods;
    
}
 
Example #13
Source File: ClassUtil.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
@Nullable
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Class<?> targetClass = method.getDeclaringClass();
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtil.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	// 先找方法,再找方法上的类
	A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
	if (null != annotation) {
		return annotation;
	}
	// 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
	return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #14
Source File: HandlerMethod.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create an instance from a bean name, a method, and a {@code BeanFactory}.
 * The method {@link #createWithResolvedBean()} may be used later to
 * re-create the {@code HandlerMethod} with an initialized bean.
 */
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method) {
	Assert.hasText(beanName, "Bean name is required");
	Assert.notNull(beanFactory, "BeanFactory is required");
	Assert.notNull(method, "Method is required");
	this.bean = beanName;
	this.beanFactory = beanFactory;
	Class<?> beanType = beanFactory.getType(beanName);
	if (beanType == null) {
		throw new IllegalStateException("Cannot resolve bean type for bean with name '" + beanName + "'");
	}
	this.beanType = ClassUtils.getUserClass(beanType);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
}
 
Example #15
Source File: AbstractFallbackJCacheOperationSource.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private JCacheOperation<?> computeCacheOperation(Method method, Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

	// First try is the method in the target class.
	JCacheOperation<?> operation = findCacheOperation(specificMethod, targetClass);
	if (operation != null) {
		return operation;
	}
	if (specificMethod != method) {
		// Fall back is to look at the original method.
		operation = findCacheOperation(method, targetClass);
		if (operation != null) {
			return operation;
		}
	}
	return null;
}
 
Example #16
Source File: ApiBootDataSourceSwitchAnnotationInterceptor.java    From beihu-boot with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    try {
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
        Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
        Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
        // get class declared DataSourceSwitch annotation
        DataSourceSwitch dataSourceSwitch = targetClass.getDeclaredAnnotation(DataSourceSwitch.class);
        if (dataSourceSwitch == null) {
            // get declared DataSourceSwitch annotation
            dataSourceSwitch = userDeclaredMethod.getDeclaredAnnotation(DataSourceSwitch.class);
        }
        if (dataSourceSwitch != null) {
            // setting current thread use data source pool name
            DataSourceContextHolder.set(dataSourceSwitch.value());
        }
        return invocation.proceed();
    } finally {
        // remove current thread use datasource pool name
        DataSourceContextHolder.remove();
    }

}
 
Example #17
Source File: ClassUtils.java    From smaker with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 获取Annotation
 *
 * @param method         Method
 * @param annotationType 注解类
 * @param <A>            泛型标记
 * @return {Annotation}
 */
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Class<?> targetClass = method.getDeclaringClass();
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
	// 先找方法,再找方法上的类
	A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
	;
	if (null != annotation) {
		return annotation;
	}
	// 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
	return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
}
 
Example #18
Source File: HandlerMethod.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create an instance from a bean instance, method name, and parameter types.
 * @throws NoSuchMethodException when the method cannot be found
 */
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
	Assert.notNull(bean, "Bean is required");
	Assert.notNull(methodName, "Method name is required");
	this.bean = bean;
	this.beanFactory = null;
	this.beanType = ClassUtils.getUserClass(bean);
	this.method = bean.getClass().getMethod(methodName, parameterTypes);
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
	this.parameters = initMethodParameters();
}
 
Example #19
Source File: HandlerMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create an instance from a bean instance, method name, and parameter types.
 * @throws NoSuchMethodException when the method cannot be found
 */
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
	Assert.notNull(bean, "Bean is required");
	Assert.notNull(methodName, "Method name is required");
	this.bean = bean;
	this.beanFactory = null;
	this.beanType = ClassUtils.getUserClass(bean);
	this.method = bean.getClass().getMethod(methodName, parameterTypes);
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
}
 
Example #20
Source File: HandlerMethod.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create an instance from a bean instance and a method.
 */
public HandlerMethod(Object bean, Method method) {
	Assert.notNull(bean, "Bean is required");
	Assert.notNull(method, "Method is required");
	this.bean = bean;
	this.beanFactory = null;
	this.beanType = ClassUtils.getUserClass(bean);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
}
 
Example #21
Source File: ResourceInspectorUtil.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Determine the expected type as the returned type of the method.
 * If the return type is a List it will return the generic element type instead of a List.
 * @param resource - resource with methods
 * @param method Method
 * @return Class - type of class it needs.
 */
@SuppressWarnings("rawtypes")
protected static Class determineType(Class resource, Method method)
{
    Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);

    /*
    * The api is consistent that the object passed in must match the object passed out
    * however, operations are different, if the param is supplied  it doesn't have to match
    * the return type.
    * So we need special logic for operations
     */
    Annotation annot = AnnotationUtils.findAnnotation(resolvedMethod, Operation.class);
    if (annot != null)
    {
        return determineOperationType(resource, method);
    }
    else
    {
        Class returnType = GenericTypeResolver.resolveReturnType(resolvedMethod, resource);
        if (List.class.isAssignableFrom(returnType))
        {
            return ResolvableType.forMethodReturnType(method).asCollection().getGeneric(0).resolve();
        }
        return returnType;
    }
}
 
Example #22
Source File: ApplicationListenerMethodAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
	this.beanName = beanName;
	this.method = method;
	this.targetClass = targetClass;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);

	Method targetMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	EventListener ann = AnnotatedElementUtils.findMergedAnnotation(targetMethod, EventListener.class);

	this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
	this.condition = (ann != null ? ann.condition() : null);
	this.order = resolveOrder(method);

	this.methodKey = new AnnotatedElementKey(method, targetClass);
}
 
Example #23
Source File: AbstractFallbackTransactionAttributeSource.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Same signature as {@link #getTransactionAttribute}, but doesn't cache the result.
 * {@link #getTransactionAttribute} is effectively a caching decorator for this method.
 * <p>As of 4.1.8, this method can be overridden.
 * @since 4.1.8
 * @see #getTransactionAttribute
 */
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// Ignore CGLIB subclasses - introspect the actual user class.
	Class<?> userClass = ClassUtils.getUserClass(targetClass);
	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, userClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

	// First try is the method in the target class.
	TransactionAttribute txAtt = findTransactionAttribute(specificMethod);
	if (txAtt != null) {
		return txAtt;
	}

	// Second try is the transaction attribute on the target class.
	txAtt = findTransactionAttribute(specificMethod.getDeclaringClass());
	if (txAtt != null) {
		return txAtt;
	}

	if (specificMethod != method) {
		// Fallback is to look at the original method.
		txAtt = findTransactionAttribute(method);
		if (txAtt != null) {
			return txAtt;
		}
		// Last fallback is the class of the original method.
		return findTransactionAttribute(method.getDeclaringClass());
	}
	return null;
}
 
Example #24
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void initBinder(Object handler, String attrName, WebDataBinder binder, NativeWebRequest webRequest)
		throws Exception {

	if (this.bindingInitializer != null) {
		this.bindingInitializer.initBinder(binder, webRequest);
	}
	if (handler != null) {
		Set<Method> initBinderMethods = this.methodResolver.getInitBinderMethods();
		if (!initBinderMethods.isEmpty()) {
			boolean debug = logger.isDebugEnabled();
			for (Method initBinderMethod : initBinderMethods) {
				Method methodToInvoke = BridgeMethodResolver.findBridgedMethod(initBinderMethod);
				String[] targetNames = AnnotationUtils.findAnnotation(initBinderMethod, InitBinder.class).value();
				if (targetNames.length == 0 || Arrays.asList(targetNames).contains(attrName)) {
					Object[] initBinderArgs =
							resolveInitBinderArguments(handler, methodToInvoke, binder, webRequest);
					if (debug) {
						logger.debug("Invoking init-binder method: " + methodToInvoke);
					}
					ReflectionUtils.makeAccessible(methodToInvoke);
					Object returnValue = methodToInvoke.invoke(handler, initBinderArgs);
					if (returnValue != null) {
						throw new IllegalStateException(
								"InitBinder methods must not have a return value: " + methodToInvoke);
					}
				}
			}
		}
	}
}
 
Example #25
Source File: HandlerMethod.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an instance from a bean instance and a method.
 */
public HandlerMethod(Object bean, Method method) {
	Assert.notNull(bean, "Bean is required");
	Assert.notNull(method, "Method is required");
	this.bean = bean;
	this.beanFactory = null;
	this.beanType = ClassUtils.getUserClass(bean);
	this.method = method;
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
}
 
Example #26
Source File: HandlerMethod.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an instance from a bean instance, method name, and parameter types.
 * @throws NoSuchMethodException when the method cannot be found
 */
public HandlerMethod(Object bean, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
	Assert.notNull(bean, "Bean is required");
	Assert.notNull(methodName, "Method name is required");
	this.bean = bean;
	this.beanFactory = null;
	this.beanType = ClassUtils.getUserClass(bean);
	this.method = bean.getClass().getMethod(methodName, parameterTypes);
	this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(this.method);
	this.parameters = initMethodParameters();
	evaluateResponseStatus();
}
 
Example #27
Source File: HandlerMethodResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize a new HandlerMethodResolver for the specified handler type.
 * @param handlerType the handler class to introspect
 */
public void init(final Class<?> handlerType) {
	Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
	Class<?> specificHandlerType = null;
	if (!Proxy.isProxyClass(handlerType)) {
		handlerTypes.add(handlerType);
		specificHandlerType = handlerType;
	}
	handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
	for (Class<?> currentHandlerType : handlerTypes) {
		final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType);
		ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) {
				Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
				if (isHandlerMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isHandlerMethod(bridgedMethod))) {
					handlerMethods.add(specificMethod);
				}
				else if (isInitBinderMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isInitBinderMethod(bridgedMethod))) {
					initBinderMethods.add(specificMethod);
				}
				else if (isModelAttributeMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isModelAttributeMethod(bridgedMethod))) {
					modelAttributeMethods.add(specificMethod);
				}
			}
		}, ReflectionUtils.USER_DECLARED_METHODS);
	}
	this.typeLevelMapping = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
	SessionAttributes sessionAttributes = AnnotationUtils.findAnnotation(handlerType, SessionAttributes.class);
	this.sessionAttributesFound = (sessionAttributes != null);
	if (this.sessionAttributesFound) {
		this.sessionAttributeNames.addAll(Arrays.asList(sessionAttributes.names()));
		this.sessionAttributeTypes.addAll(Arrays.asList(sessionAttributes.types()));
	}
}
 
Example #28
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void initBinder(Object handler, String attrName, WebDataBinder binder, NativeWebRequest webRequest)
		throws Exception {

	if (this.bindingInitializer != null) {
		this.bindingInitializer.initBinder(binder, webRequest);
	}
	if (handler != null) {
		Set<Method> initBinderMethods = this.methodResolver.getInitBinderMethods();
		if (!initBinderMethods.isEmpty()) {
			boolean debug = logger.isDebugEnabled();
			for (Method initBinderMethod : initBinderMethods) {
				Method methodToInvoke = BridgeMethodResolver.findBridgedMethod(initBinderMethod);
				String[] targetNames = AnnotationUtils.findAnnotation(initBinderMethod, InitBinder.class).value();
				if (targetNames.length == 0 || Arrays.asList(targetNames).contains(attrName)) {
					Object[] initBinderArgs =
							resolveInitBinderArguments(handler, methodToInvoke, binder, webRequest);
					if (debug) {
						logger.debug("Invoking init-binder method: " + methodToInvoke);
					}
					ReflectionUtils.makeAccessible(methodToInvoke);
					Object returnValue = methodToInvoke.invoke(handler, initBinderArgs);
					if (returnValue != null) {
						throw new IllegalStateException(
								"InitBinder methods must not have a return value: " + methodToInvoke);
					}
				}
			}
		}
	}
}
 
Example #29
Source File: HandlerMethodResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize a new HandlerMethodResolver for the specified handler type.
 * @param handlerType the handler class to introspect
 */
public void init(final Class<?> handlerType) {
	Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>();
	Class<?> specificHandlerType = null;
	if (!Proxy.isProxyClass(handlerType)) {
		handlerTypes.add(handlerType);
		specificHandlerType = handlerType;
	}
	handlerTypes.addAll(Arrays.asList(handlerType.getInterfaces()));
	for (Class<?> currentHandlerType : handlerTypes) {
		final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType);
		ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() {
			@Override
			public void doWith(Method method) {
				Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
				Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
				if (isHandlerMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isHandlerMethod(bridgedMethod))) {
					handlerMethods.add(specificMethod);
				}
				else if (isInitBinderMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isInitBinderMethod(bridgedMethod))) {
					initBinderMethods.add(specificMethod);
				}
				else if (isModelAttributeMethod(specificMethod) &&
						(bridgedMethod == specificMethod || !isModelAttributeMethod(bridgedMethod))) {
					modelAttributeMethods.add(specificMethod);
				}
			}
		}, ReflectionUtils.USER_DECLARED_METHODS);
	}
	this.typeLevelMapping = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
	SessionAttributes sessionAttributes = AnnotationUtils.findAnnotation(handlerType, SessionAttributes.class);
	this.sessionAttributesFound = (sessionAttributes != null);
	if (this.sessionAttributesFound) {
		this.sessionAttributeNames.addAll(Arrays.asList(sessionAttributes.names()));
		this.sessionAttributeTypes.addAll(Arrays.asList(sessionAttributes.types()));
	}
}
 
Example #30
Source File: AbstractFallbackCacheOperationSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Collection<CacheOperation> computeCacheOperations(Method method, Class<?> targetClass) {
	// Don't allow no-public methods as required.
	if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
		return null;
	}

	// The method may be on an interface, but we need attributes from the target class.
	// If the target class is null, the method will be unchanged.
	Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

	// First try is the method in the target class.
	Collection<CacheOperation> opDef = findCacheOperations(specificMethod);
	if (opDef != null) {
		return opDef;
	}

	// Second try is the caching operation on the target class.
	opDef = findCacheOperations(specificMethod.getDeclaringClass());
	if (opDef != null && ClassUtils.isUserLevelMethod(method)) {
		return opDef;
	}

	if (specificMethod != method) {
		// Fallback is to look at the original method.
		opDef = findCacheOperations(method);
		if (opDef != null) {
			return opDef;
		}
		// Last fallback is the class of the original method.
		opDef = findCacheOperations(method.getDeclaringClass());
		if (opDef != null && ClassUtils.isUserLevelMethod(method)) {
			return opDef;
		}
	}

	return null;
}