Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004-2008 the original author or authors.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.binding.method;
 
 import java.util.Map;
 
A helper for resolving and caching a Java method by reflection.

Author(s):
Keith Donald
 
 public class MethodKey implements Serializable {

The class the method is a member of.
 
 	private Class declaredType;

The method name.
 
 	private String methodName;

The method's actual parameter types. Could contain null values if the user did not specify a parameter type for the corresponding parameter
 
 	private Class[] parameterTypes;

A cached handle to the resolved method (may be null).
 
 	private transient Method method;

Create a new method key.

Parameters:
declaredType the class the method is a member of
methodName the method name
parameterTypes the method's parameter types, or null if the method has no parameters
 
 	public MethodKey(Class declaredTypeString methodNameClass[] parameterTypes) {
 		Assert.notNull(declaredType"The method's declared type is required");
 		Assert.notNull(methodName"The method name is required");
 		this. = declaredType;
 		this. = methodName;
 		this. = parameterTypes;
 	}

Return the class the method is a member of.
 
 	public Class getDeclaredType() {
 		return ;
 	}

Returns the method name.
 
 	public String getMethodName() {
 		return ;
 	}

Returns the method parameter types. Could contain null values if no type was specified for the corresponding parameter.
 
 	public Class[] getParameterTypes() {
 		return ;
 	}

Returns the keyed method, resolving it if necessary via reflection.
 
 	public Method getMethod() throws InvalidMethodKeyException {
 		if ( == null) {
 		}
 		return ;
 	}
	// internal helpers
Resolve the keyed method.
		try {
catch (NoSuchMethodException e) {
			if (method != null) {
				return method;
else {
				throw new InvalidMethodKeyException(thise);
			}
		}
	}

Find the keyed method using 'relaxed' typing.
		Method[] candidateMethods = getDeclaredType().getMethods();
		for (int i = 0; i < candidateMethods.lengthi++) {
			if (candidateMethods[i].getName().equals()) {
				// Check if the method has the correct number of parameters.
				Class[] candidateParameterTypes = candidateMethods[i].getParameterTypes();
				if (candidateParameterTypes.length == .) {
					int numberOfCorrectArguments = 0;
					for (int j = 0; j < candidateParameterTypes.lengthj++) {
						// Check if the candidate type is assignable to the sig
						// parameter type.
						Class candidateType = candidateParameterTypes[j];
						Class parameterType = [j];
						if (parameterType != null) {
							if (isAssignable(candidateTypeparameterType)) {
								numberOfCorrectArguments++;
							}
else {
							// just match on a null param type (effectively 'any')
							numberOfCorrectArguments++;
						}
					}
					if (numberOfCorrectArguments == .) {
						return candidateMethods[i];
					}
				}
			}
		}
		return null;
	}
	public boolean equals(Object obj) {
		if (!(obj instanceof MethodKey)) {
			return false;
		}
		MethodKey other = (MethodKeyobj;
		return .equals(other.declaredType) && .equals(other.methodName)
				&& parameterTypesEqual(other.parameterTypes);
	}
	private boolean parameterTypesEqual(Class[] other) {
		if ( == other) {
			return true;
		}
		if (. != other.length) {
			return false;
		}
		for (int i = 0; i < this..lengthi++) {
			if (!ObjectUtils.nullSafeEquals([i], other[i])) {
				return false;
			}
		}
		return true;
	}
	public int hashCode() {
	}
	private int parameterTypesHash() {
		if ( == null) {
			return 0;
		}
		int hash = 0;
		for (int i = 0; i < .i++) {
			Class parameterType = [i];
			if (parameterType != null) {
				hash += [i].hashCode();
			}
		}
		return hash;
	}
	public String toString() {
		return  + "(" + parameterTypesString() + ")";
	}

Convenience method that returns the parameter types describing the signature of the method as a string.
		StringBuffer parameterTypesString = new StringBuffer();
		for (int i = 0; i < .i++) {
			if ([i] == null) {
				parameterTypesString.append("<any>");
else {
				parameterTypesString.append(ClassUtils.getShortName([i]));
			}
			if (i < . - 1) {
				parameterTypesString.append(',');
			}
		}
		return parameterTypesString.toString();
	}
	// internal helpers
Determine if the given target type is assignable from the given value type, assuming setting by reflection. Considers primitive wrapper classes as assignable to the corresponding primitive types.

NOTE: Pulled from ClassUtils in Spring 2.0 for 1.2.8 compatability.

Parameters:
targetType the target type
valueType the value type that should be assigned to the target type
Returns:
if the target type is assignable from the value type
	private static boolean isAssignable(Class targetTypeClass valueType) {
		return (targetType.isAssignableFrom(valueType) || targetType.equals(.get(valueType)));
	}

Map with primitive wrapper type as key and corresponding primitive type as value, for example: Integer.class -> int.class.
	private static final Map primitiveWrapperTypeMap = new HashMap(8);
	static {
		.put(Boolean.classboolean.class);
		.put(Byte.classbyte.class);
		.put(Double.classdouble.class);
		.put(Float.classfloat.class);
		.put(Long.classlong.class);
		.put(Short.classshort.class);
	}
New to GrepCode? Check out our FAQ X