|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2002-2015 the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | + * you may not use this file except in compliance with the License. | 
|  | 6 | + * You may obtain a copy of the License at | 
|  | 7 | + * | 
|  | 8 | + *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | + * | 
|  | 10 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | + * See the License for the specific language governing permissions and | 
|  | 14 | + * limitations under the License. | 
|  | 15 | + */ | 
|  | 16 | + | 
|  | 17 | +package org.springframework.core; | 
|  | 18 | + | 
|  | 19 | +import java.lang.reflect.Method; | 
|  | 20 | +import java.lang.reflect.Proxy; | 
|  | 21 | +import java.util.Arrays; | 
|  | 22 | +import java.util.LinkedHashMap; | 
|  | 23 | +import java.util.LinkedHashSet; | 
|  | 24 | +import java.util.Map; | 
|  | 25 | +import java.util.Set; | 
|  | 26 | + | 
|  | 27 | +import org.springframework.util.ClassUtils; | 
|  | 28 | +import org.springframework.util.ReflectionUtils; | 
|  | 29 | + | 
|  | 30 | +/** | 
|  | 31 | + * Defines the algorithm for searching for metadata-associated methods exhaustively | 
|  | 32 | + * including interfaces and parent classes while also dealing with parameterized methods | 
|  | 33 | + * as well as common scenarios encountered with interface and class-based proxies. | 
|  | 34 | + * | 
|  | 35 | + * <p>Typically, but not necessarily, used for finding annotated handler methods. | 
|  | 36 | + * | 
|  | 37 | + * @author Juergen Hoeller | 
|  | 38 | + * @author Rossen Stoyanchev | 
|  | 39 | + * @since 4.2.3 | 
|  | 40 | + */ | 
|  | 41 | +public abstract class MethodIntrospector { | 
|  | 42 | + | 
|  | 43 | +	/** | 
|  | 44 | +	 * Select methods on the given target type based on the lookup of associated metadata. | 
|  | 45 | +	 * <p>Callers define methods of interest through the {@link MetadataLookup} parameter, | 
|  | 46 | +	 * allowing to collect the associated metadata into the result map. | 
|  | 47 | +	 * @param targetType the target type to search methods on | 
|  | 48 | +	 * @param metadataLookup a {@link MetadataLookup} callback to inspect methods of interest, | 
|  | 49 | +	 * returning non-null metadata to be associated with a given method if there is a match, | 
|  | 50 | +	 * or {@code null} for no match | 
|  | 51 | +	 * @return the selected methods associated with their metadata (in the order of retrieval), | 
|  | 52 | +	 * or an empty map in case of no match | 
|  | 53 | +	 */ | 
|  | 54 | +	public static <T> Map<Method, T> selectMethods(Class<?> targetType, final MetadataLookup<T> metadataLookup) { | 
|  | 55 | +		final Map<Method, T> methodMap = new LinkedHashMap<Method, T>(); | 
|  | 56 | +		Set<Class<?>> handlerTypes = new LinkedHashSet<Class<?>>(); | 
|  | 57 | +		Class<?> specificHandlerType = null; | 
|  | 58 | + | 
|  | 59 | +		if (!Proxy.isProxyClass(targetType)) { | 
|  | 60 | +			handlerTypes.add(targetType); | 
|  | 61 | +			specificHandlerType = targetType; | 
|  | 62 | +		} | 
|  | 63 | +		handlerTypes.addAll(Arrays.asList(targetType.getInterfaces())); | 
|  | 64 | + | 
|  | 65 | +		for (Class<?> currentHandlerType : handlerTypes) { | 
|  | 66 | +			final Class<?> targetClass = (specificHandlerType != null ? specificHandlerType : currentHandlerType); | 
|  | 67 | + | 
|  | 68 | +			ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() { | 
|  | 69 | +				@Override | 
|  | 70 | +				public void doWith(Method method) { | 
|  | 71 | +					Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass); | 
|  | 72 | +					T result = metadataLookup.inspect(specificMethod); | 
|  | 73 | +					if (result != null) { | 
|  | 74 | +						Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); | 
|  | 75 | +						if (bridgedMethod == specificMethod || metadataLookup.inspect(bridgedMethod) == null) { | 
|  | 76 | +							methodMap.put(specificMethod, result); | 
|  | 77 | +						} | 
|  | 78 | +					} | 
|  | 79 | +				} | 
|  | 80 | +			}, ReflectionUtils.USER_DECLARED_METHODS); | 
|  | 81 | +		} | 
|  | 82 | + | 
|  | 83 | +		return methodMap; | 
|  | 84 | +	} | 
|  | 85 | + | 
|  | 86 | +	/** | 
|  | 87 | +	 * Select methods on the given target type based on a filter. | 
|  | 88 | +	 * <p>Callers define methods of interest through the | 
|  | 89 | +	 * {@link ReflectionUtils.MethodFilter} parameter. | 
|  | 90 | +	 * @param targetType the target type to search methods on | 
|  | 91 | +	 * @param methodFilter a {@link ReflectionUtils.MethodFilter} to help | 
|  | 92 | +	 * recognize handler methods of interest | 
|  | 93 | +	 * @return the selected methods, or an empty set in case of no match | 
|  | 94 | +	 */ | 
|  | 95 | +	public static Set<Method> selectMethods(Class<?> targetType, final ReflectionUtils.MethodFilter methodFilter) { | 
|  | 96 | +		return selectMethods(targetType, new MetadataLookup<Boolean>() { | 
|  | 97 | +			@Override | 
|  | 98 | +			public Boolean inspect(Method method) { | 
|  | 99 | +				return (methodFilter.matches(method) ? Boolean.TRUE : null); | 
|  | 100 | +			} | 
|  | 101 | +		}).keySet(); | 
|  | 102 | +	} | 
|  | 103 | + | 
|  | 104 | +	/** | 
|  | 105 | +	 * Select an invocable method on the target type: either the given method itself | 
|  | 106 | +	 * if actually exposed on the target type, or otherwise a corresponding method | 
|  | 107 | +	 * on one of the target type's interfaces or on the target type itself. | 
|  | 108 | +	 * <p>Matches on user-declared interfaces will be preferred since they are likely | 
|  | 109 | +	 * to contain relevant metadata that corresponds to the method on the target class. | 
|  | 110 | +	 * @param method the method to check | 
|  | 111 | +	 * @param targetType the target type to search methods on | 
|  | 112 | +	 * (typically an interface-based JDK proxy) | 
|  | 113 | +	 * @return a corresponding invocable method on the target type | 
|  | 114 | +	 */ | 
|  | 115 | +	public static Method selectInvocableMethod(Method method, Class<?> targetType) { | 
|  | 116 | +		if (method.getDeclaringClass().isAssignableFrom(targetType)) { | 
|  | 117 | +			return method; | 
|  | 118 | +		} | 
|  | 119 | +		try { | 
|  | 120 | +			for (Class<?> ifc : targetType.getInterfaces()) { | 
|  | 121 | +				try { | 
|  | 122 | +					return ifc.getMethod(method.getName(), method.getParameterTypes()); | 
|  | 123 | +				} | 
|  | 124 | +				catch (NoSuchMethodException ex) { | 
|  | 125 | +					// Alright, not on this interface then... | 
|  | 126 | +				} | 
|  | 127 | +			} | 
|  | 128 | +			// A final desperate attempt on the proxy class itself... | 
|  | 129 | +			return targetType.getMethod(method.getName(), method.getParameterTypes()); | 
|  | 130 | +		} | 
|  | 131 | +		catch (NoSuchMethodException ex) { | 
|  | 132 | +			throw new IllegalStateException(String.format( | 
|  | 133 | +					"Need to invoke method '%s' declared on target class '%s', " + | 
|  | 134 | +					"but not found in any interface(s) of the exposed proxy type. " + | 
|  | 135 | +					"Either pull the method up to an interface or switch to CGLIB " + | 
|  | 136 | +					"proxies by enforcing proxy-target-class mode in your configuration.", | 
|  | 137 | +					method.getName(), method.getDeclaringClass().getSimpleName())); | 
|  | 138 | +		} | 
|  | 139 | +	} | 
|  | 140 | + | 
|  | 141 | + | 
|  | 142 | +	/** | 
|  | 143 | +	 * A callback interface for metadata lookup on a given method. | 
|  | 144 | +	 * @param <T> the type of metadata returned | 
|  | 145 | +	 */ | 
|  | 146 | +	public interface MetadataLookup<T> { | 
|  | 147 | + | 
|  | 148 | +		/** | 
|  | 149 | +		 * Perform a lookup on the given method and return associated metadata, if any. | 
|  | 150 | +		 * @param method the method to inspect | 
|  | 151 | +		 * @return non-null metadata to be associated with a method if there is a match, | 
|  | 152 | +		 * or {@code null} for no match | 
|  | 153 | +		 */ | 
|  | 154 | +		T inspect(Method method); | 
|  | 155 | +	} | 
|  | 156 | + | 
|  | 157 | +} | 
0 commit comments