當前位置: 首頁>>代碼示例>>Java>>正文


Java EvaluationContext.getTypeConverter方法代碼示例

本文整理匯總了Java中org.springframework.expression.EvaluationContext.getTypeConverter方法的典型用法代碼示例。如果您正苦於以下問題:Java EvaluationContext.getTypeConverter方法的具體用法?Java EvaluationContext.getTypeConverter怎麽用?Java EvaluationContext.getTypeConverter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.expression.EvaluationContext的用法示例。


在下文中一共展示了EvaluationContext.getTypeConverter方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: resolve

import org.springframework.expression.EvaluationContext; //導入方法依賴的package包/類
/**
 * Locate a constructor on the type. There are three kinds of match that might occur:
 * <ol>
 * <li>An exact match where the types of the arguments match the types of the constructor
 * <li>An in-exact match where the types we are looking for are subtypes of those defined on the constructor
 * <li>A match where we are able to convert the arguments into those expected by the constructor, according to the
 * registered type converter.
 * </ol>
 */
@Override
public ConstructorExecutor resolve(EvaluationContext context, String typename, List<TypeDescriptor> argumentTypes)
		throws AccessException {

	try {
		TypeConverter typeConverter = context.getTypeConverter();
		Class<?> type = context.getTypeLocator().findType(typename);
		Constructor<?>[] ctors = type.getConstructors();

		Arrays.sort(ctors, new Comparator<Constructor<?>>() {
			@Override
			public int compare(Constructor<?> c1, Constructor<?> c2) {
				int c1pl = c1.getParameterTypes().length;
				int c2pl = c2.getParameterTypes().length;
				return (new Integer(c1pl)).compareTo(c2pl);
			}
		});

		Constructor<?> closeMatch = null;
		Constructor<?> matchRequiringConversion = null;

		for (Constructor<?> ctor : ctors) {
			Class<?>[] paramTypes = ctor.getParameterTypes();
			List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);
			for (int i = 0; i < paramTypes.length; i++) {
				paramDescriptors.add(new TypeDescriptor(new MethodParameter(ctor, i)));
			}
			ReflectionHelper.ArgumentsMatchInfo matchInfo = null;
			if (ctor.isVarArgs() && argumentTypes.size() >= paramTypes.length - 1) {
				// *sigh* complicated
				// Basically.. we have to have all parameters match up until the varargs one, then the rest of what is
				// being provided should be
				// the same type whilst the final argument to the method must be an array of that (oh, how easy...not) -
				// or the final parameter
				// we are supplied does match exactly (it is an array already).
				matchInfo = ReflectionHelper.compareArgumentsVarargs(paramDescriptors, argumentTypes, typeConverter);
			}
			else if (paramTypes.length == argumentTypes.size()) {
				// worth a closer look
				matchInfo = ReflectionHelper.compareArguments(paramDescriptors, argumentTypes, typeConverter);
			}
			if (matchInfo != null) {
				if (matchInfo.isExactMatch()) {
					return new ReflectiveConstructorExecutor(ctor);
				}
				else if (matchInfo.isCloseMatch()) {
					closeMatch = ctor;
				}
				else if (matchInfo.isMatchRequiringConversion()) {
					matchRequiringConversion = ctor;
				}
			}
		}

		if (closeMatch != null) {
			return new ReflectiveConstructorExecutor(closeMatch);
		}
		else if (matchRequiringConversion != null) {
			return new ReflectiveConstructorExecutor(matchRequiringConversion);
		}
		else {
			return null;
		}
	}
	catch (EvaluationException ex) {
		throw new AccessException("Failed to resolve constructor", ex);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:78,代碼來源:ReflectiveConstructorResolver.java


注:本文中的org.springframework.expression.EvaluationContext.getTypeConverter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。