org.springframework.core.BridgeMethodResolver#findBridgedMethod ( )源码实例Demo

下面列出了org.springframework.core.BridgeMethodResolver#findBridgedMethod ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: java-technology-stack   文件: HandlerMethod.java
/**
 * 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();
}
 
/**
 * 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;
    
}
 
源代码3 项目: spring-analysis-note   文件: HandlerMethod.java
/**
 * 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);
}
 
源代码4 项目: java-technology-stack   文件: HandlerMethod.java
/**
 * 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();
}
 
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;
}
 
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) {
		return opDef;
	}

	if (specificMethod != method) {
		// Fall back is to look at the original method.
		opDef = findCacheOperations(method);
		if (opDef != null) {
			return opDef;
		}
		// Last fall back is the class of the original method.
		return findCacheOperations(method.getDeclaringClass());
	}
	return null;
}
 
源代码7 项目: lams   文件: HandlerMethod.java
/**
 * 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();
}
 
源代码8 项目: lams   文件: HandlerMethodInvoker.java
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);
					}
				}
			}
		}
	}
}
 
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;
}
 
源代码10 项目: java-technology-stack   文件: CacheAspectSupport.java
public CacheOperationMetadata(CacheOperation operation, Method method, Class<?> targetClass,
		KeyGenerator keyGenerator, CacheResolver cacheResolver) {

	this.operation = operation;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.targetClass = targetClass;
	this.targetMethod = (!Proxy.isProxyClass(targetClass) ?
			AopUtils.getMostSpecificMethod(method, targetClass) : this.method);
	this.methodKey = new AnnotatedElementKey(this.targetMethod, targetClass);
	this.keyGenerator = keyGenerator;
	this.cacheResolver = cacheResolver;
}
 
private ShardingTransactionType getMethodAnnotation(final MethodInvocation invocation, final Class<?> targetClass) {
    Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
    final Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
    return userDeclaredMethod.getAnnotation(ShardingTransactionType.class);
}
 
源代码12 项目: java-technology-stack   文件: AnnotationUtils.java
/**
 * Find a single {@link Annotation} of {@code annotationType} on the supplied
 * {@link Method}, traversing its super methods (i.e. from superclasses and
 * interfaces) if the annotation is not <em>directly present</em> on the given
 * method itself.
 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
 * <p>Meta-annotations will be searched if the annotation is not
 * <em>directly present</em> on the method.
 * <p>Annotations on methods are not inherited by default, so we need to handle
 * this explicitly.
 * @param method the method to look for annotations on
 * @param annotationType the annotation type to look for
 * @return the first matching annotation, or {@code null} if not found
 * @see #getAnnotation(Method, Class)
 */
@SuppressWarnings("unchecked")
@Nullable
public static <A extends Annotation> A findAnnotation(Method method, @Nullable Class<A> annotationType) {
	Assert.notNull(method, "Method must not be null");
	if (annotationType == null) {
		return null;
	}

	AnnotationCacheKey cacheKey = new AnnotationCacheKey(method, annotationType);
	A result = (A) findAnnotationCache.get(cacheKey);

	if (result == null) {
		Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
		result = findAnnotation((AnnotatedElement) resolvedMethod, annotationType);
		if (result == null) {
			result = searchOnInterfaces(method, annotationType, method.getDeclaringClass().getInterfaces());
		}

		Class<?> clazz = method.getDeclaringClass();
		while (result == null) {
			clazz = clazz.getSuperclass();
			if (clazz == null || clazz == Object.class) {
				break;
			}
			Set<Method> annotatedMethods = getAnnotatedMethodsInBaseType(clazz);
			if (!annotatedMethods.isEmpty()) {
				for (Method annotatedMethod : annotatedMethods) {
					if (isOverride(method, annotatedMethod)) {
						Method resolvedSuperMethod = BridgeMethodResolver.findBridgedMethod(annotatedMethod);
						result = findAnnotation((AnnotatedElement) resolvedSuperMethod, annotationType);
						if (result != null) {
							break;
						}
					}
				}
			}
			if (result == null) {
				result = searchOnInterfaces(method, annotationType, clazz.getInterfaces());
			}
		}

		if (result != null) {
			result = synthesizeAnnotation(result, method);
			findAnnotationCache.put(cacheKey, result);
		}
	}

	return result;
}
 
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
		@Nullable Method readMethod, @Nullable Method writeMethod, Class<?> propertyEditorClass)
		throws IntrospectionException {

	super(propertyName, null, null);
	this.beanClass = beanClass;

	Method readMethodToUse = (readMethod != null ? BridgeMethodResolver.findBridgedMethod(readMethod) : null);
	Method writeMethodToUse = (writeMethod != null ? BridgeMethodResolver.findBridgedMethod(writeMethod) : null);
	if (writeMethodToUse == null && readMethodToUse != null) {
		// Fallback: Original JavaBeans introspection might not have found matching setter
		// method due to lack of bridge method resolution, in case of the getter using a
		// covariant return type whereas the setter is defined for the concrete property type.
		Method candidate = ClassUtils.getMethodIfAvailable(
				this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null);
		if (candidate != null && candidate.getParameterCount() == 1) {
			writeMethodToUse = candidate;
		}
	}
	this.readMethod = readMethodToUse;
	this.writeMethod = writeMethodToUse;

	if (this.writeMethod != null) {
		if (this.readMethod == null) {
			// Write method not matched against read method: potentially ambiguous through
			// several overloaded variants, in which case an arbitrary winner has been chosen
			// by the JDK's JavaBeans Introspector...
			Set<Method> ambiguousCandidates = new HashSet<>();
			for (Method method : beanClass.getMethods()) {
				if (method.getName().equals(writeMethodToUse.getName()) &&
						!method.equals(writeMethodToUse) && !method.isBridge() &&
						method.getParameterCount() == writeMethodToUse.getParameterCount()) {
					ambiguousCandidates.add(method);
				}
			}
			if (!ambiguousCandidates.isEmpty()) {
				this.ambiguousWriteMethods = ambiguousCandidates;
			}
		}
		this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
		GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
	}

	if (this.readMethod != null) {
		this.propertyType = GenericTypeResolver.resolveReturnType(this.readMethod, this.beanClass);
	}
	else if (this.writeMethodParameter != null) {
		this.propertyType = this.writeMethodParameter.getParameterType();
	}

	this.propertyEditorClass = propertyEditorClass;
}
 
源代码14 项目: lams   文件: ReflectiveMethodInvocation.java
/**
 * Construct a new ReflectiveMethodInvocation with the given arguments.
 * @param proxy the proxy object that the invocation was made on
 * @param target the target object to invoke
 * @param method the method to invoke
 * @param arguments the arguments to invoke the method with
 * @param targetClass the target class, for MethodMatcher invocations
 * @param interceptorsAndDynamicMethodMatchers interceptors that should be applied,
 * along with any InterceptorAndDynamicMethodMatchers that need evaluation at runtime.
 * MethodMatchers included in this struct must already have been found to have matched
 * as far as was possibly statically. Passing an array might be about 10% faster,
 * but would complicate the code. And it would work only for static pointcuts.
 */
protected ReflectiveMethodInvocation(
		Object proxy, Object target, Method method, Object[] arguments,
		Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {

	this.proxy = proxy;
	this.target = target;
	this.targetClass = targetClass;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
	this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}
 
/**
 * Construct a new ReflectiveMethodInvocation with the given arguments.
 * @param proxy the proxy object that the invocation was made on
 * @param target the target object to invoke
 * @param method the method to invoke
 * @param arguments the arguments to invoke the method with
 * @param targetClass the target class, for MethodMatcher invocations
 * @param interceptorsAndDynamicMethodMatchers interceptors that should be applied,
 * along with any InterceptorAndDynamicMethodMatchers that need evaluation at runtime.
 * MethodMatchers included in this struct must already have been found to have matched
 * as far as was possibly statically. Passing an array might be about 10% faster,
 * but would complicate the code. And it would work only for static pointcuts.
 */
protected ReflectiveMethodInvocation(
		Object proxy, Object target, Method method, Object[] arguments,
		Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {

	this.proxy = proxy;
	this.target = target;
	this.targetClass = targetClass;
	this.method = BridgeMethodResolver.findBridgedMethod(method);
	this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
	this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}
 
源代码16 项目: java-technology-stack   文件: AopUtils.java
/**
 * Given a method, which may come from an interface, and a target class used
 * in the current AOP invocation, find the corresponding target method if there
 * is one. E.g. the method may be {@code IFoo.bar()} and the target class
 * may be {@code DefaultFoo}. In this case, the method may be
 * {@code DefaultFoo.bar()}. This enables attributes on that method to be found.
 * <p><b>NOTE:</b> In contrast to {@link org.springframework.util.ClassUtils#getMostSpecificMethod},
 * this method resolves Java 5 bridge methods in order to retrieve attributes
 * from the <i>original</i> method definition.
 * @param method the method to be invoked, which may come from an interface
 * @param targetClass the target class for the current invocation.
 * May be {@code null} or may not even implement the method.
 * @return the specific target method, or the original method if the
 * {@code targetClass} doesn't implement it or is {@code null}
 * @see org.springframework.util.ClassUtils#getMostSpecificMethod
 */
public static Method getMostSpecificMethod(Method method, @Nullable Class<?> targetClass) {
	Class<?> specificTargetClass = (targetClass != null ? ClassUtils.getUserClass(targetClass) : null);
	Method resolvedMethod = ClassUtils.getMostSpecificMethod(method, specificTargetClass);
	// If we are dealing with method with generic parameters, find the original method.
	return BridgeMethodResolver.findBridgedMethod(resolvedMethod);
}
 
源代码17 项目: api-boot   文件: AopTools.java
/**
 * get method declared annotation
 *
 * @param targetClass     class instance
 * @param method          method instance
 * @param annotationClass annotation class
 * @param <T>             annotation type
 * @return annotation instance
 */
public static <T> T getMethodAnnotation(Class targetClass, Method method, Class annotationClass) {
    Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
    // declared method object instance
    Method declaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
    return (T) declaredMethod.getDeclaredAnnotation(annotationClass);
}
 
源代码18 项目: spring4-understanding   文件: AopUtils.java
/**
 * Given a method, which may come from an interface, and a target class used
 * in the current AOP invocation, find the corresponding target method if there
 * is one. E.g. the method may be {@code IFoo.bar()} and the target class
 * may be {@code DefaultFoo}. In this case, the method may be
 * {@code DefaultFoo.bar()}. This enables attributes on that method to be found.
 * <p><b>NOTE:</b> In contrast to {@link org.springframework.util.ClassUtils#getMostSpecificMethod},
 * this method resolves Java 5 bridge methods in order to retrieve attributes
 * from the <i>original</i> method definition.
 * @param method the method to be invoked, which may come from an interface
 * @param targetClass the target class for the current invocation.
 * May be {@code null} or may not even implement the method.
 * @return the specific target method, or the original method if the
 * {@code targetClass} doesn't implement it or is {@code null}
 * @see org.springframework.util.ClassUtils#getMostSpecificMethod
 */
public static Method getMostSpecificMethod(Method method, Class<?> targetClass) {
	Method resolvedMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
	// If we are dealing with method with generic parameters, find the original method.
	return BridgeMethodResolver.findBridgedMethod(resolvedMethod);
}
 
源代码19 项目: spring-analysis-note   文件: AnnotationUtils.java
/**
 * Get a single {@link Annotation} of {@code annotationType} from the
 * supplied {@link Method}, where the annotation is either <em>present</em>
 * or <em>meta-present</em> on the method.
 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
 * <p>Note that this method supports only a single level of meta-annotations.
 * For support for arbitrary levels of meta-annotations, use
 * {@link #findAnnotation(Method, Class)} instead.
 * @param method the method to look for annotations on
 * @param annotationType the annotation type to look for
 * @return the first matching annotation, or {@code null} if not found
 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
 * @see #getAnnotation(AnnotatedElement, Class)
 */
@Nullable
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
	return getAnnotation((AnnotatedElement) resolvedMethod, annotationType);
}
 
源代码20 项目: lams   文件: AnnotationUtils.java
/**
 * Get a single {@link Annotation} of {@code annotationType} from the
 * supplied {@link Method}, where the annotation is either <em>present</em>
 * or <em>meta-present</em> on the method.
 * <p>Correctly handles bridge {@link Method Methods} generated by the compiler.
 * <p>Note that this method supports only a single level of meta-annotations.
 * For support for arbitrary levels of meta-annotations, use
 * {@link #findAnnotation(Method, Class)} instead.
 * @param method the method to look for annotations on
 * @param annotationType the annotation type to look for
 * @return the first matching annotation, or {@code null} if not found
 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
 * @see #getAnnotation(AnnotatedElement, Class)
 */
public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
	Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);
	return getAnnotation((AnnotatedElement) resolvedMethod, annotationType);
}