diff options
author | Claude Brisson <cbrisson@apache.org> | 2018-10-17 19:18:37 +0000 |
---|---|---|
committer | Claude Brisson <cbrisson@apache.org> | 2018-10-17 19:18:37 +0000 |
commit | b59622f4ea460630091fd731bbd4b10cb355a18b (patch) | |
tree | 91de50771b3616e30b8d3dfd0fe8b52e532e86d4 /velocity-engine-core/src/main/java | |
parent | 087f6f6b200664a12e3748eccdae1489d6709787 (diff) | |
download | apache-velocity-engine-b59622f4ea460630091fd731bbd4b10cb355a18b.tar.gz |
[VELOCITY-892] Better handling of methods disambiguisation
git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/branches/VELOCITY-892@1844155 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src/main/java')
4 files changed, 28 insertions, 21 deletions
diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java index 1eb4fb8f..78e0ad79 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java @@ -74,7 +74,7 @@ public class IntrospectionUtils * @param clazz input class * @return boxed class */ - static Class getBoxedClass(Class clazz) + public static Class getBoxedClass(Class clazz) { Class boxed = boxingMap.get(clazz); return boxed == null ? clazz : boxed; @@ -85,7 +85,7 @@ public class IntrospectionUtils * @param clazz input class * @return unboxed class */ - static Class getUnboxedClass(Class clazz) + public static Class getUnboxedClass(Class clazz) { Class unboxed = unboxingMap.get(clazz); return unboxed == null ? clazz : unboxed; @@ -93,8 +93,10 @@ public class IntrospectionUtils /** * returns the Class corresponding to a Type, if possible + * @param type the input Type + * @return found Class, if any */ - static Class getTypeClass(Type type) + public static Class getTypeClass(Type type) { if (type == null) { diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java index 2f7192eb..f9f1e000 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java @@ -402,13 +402,15 @@ public class MethodMap int fromC2toC1 = STRICTLY_CONVERTIBLE; for(int i = 0; i < t1.length; ++i) { + Class c1 = t1[i] == null ? null : IntrospectionUtils.getTypeClass(t1[i]); + Class c2 = t2[i] == null ? null : IntrospectionUtils.getTypeClass(t2[i]); boolean last = !fixedLengths && (i == t1.length - 1); - if (t1[i] != t2[i]) + if (t1[i] == null && t2[i] != null || t1[i] != null && t2[i] == null || !t1[i].equals(t2[i])) { if (t1[i] == null) { fromC2toC1 = NOT_CONVERTIBLE; - if ((t2[i] instanceof Class) && ((Class)t2[i]).isPrimitive()) + if (c2 != null && c2.isPrimitive()) { fromC1toC2 = NOT_CONVERTIBLE; } @@ -416,16 +418,15 @@ public class MethodMap else if (t2[i] == null) { fromC1toC2 = NOT_CONVERTIBLE; - if ((t1[i] instanceof Class) && ((Class)t1[i]).isPrimitive()) + if (c1 != null && c1.isPrimitive()) { fromC2toC1 = NOT_CONVERTIBLE; } } else { - if (t1[i] instanceof Class) + if (c1 != null) { - Class c1 = (Class)t1[i]; switch (fromC1toC2) { case STRICTLY_CONVERTIBLE: @@ -445,9 +446,8 @@ public class MethodMap Math.min(fromC1toC2, IMPLCITLY_CONVERTIBLE) : NOT_CONVERTIBLE; } - if (t2[i] instanceof Class) + if (c2 != null) { - Class c2 = (Class)t2[i]; switch (fromC2toC1) { case STRICTLY_CONVERTIBLE: diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java index b59cad23..c5a86d9a 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java @@ -536,7 +536,8 @@ public class TypeConversionHandlerImpl implements TypeConversionHandler * for consistency, we also have to check standard implicit convertibility * since it may not have been checked before by the calling code */ - if ((formal instanceof Class) && (Class)formal == actual || + Class formalClass = IntrospectionUtils.getTypeClass(formal); + if (formalClass != null && formalClass == actual || IntrospectionUtils.isMethodInvocationConvertible(formal, actual, possibleVarArg) || getNeededConverter(formal, actual) != null) { @@ -582,15 +583,16 @@ public class TypeConversionHandlerImpl implements TypeConversionHandler converter = converterCacheMap.get(key); if (converter == null) { + Class formalClass = IntrospectionUtils.getTypeClass(formal); /* check for conversion towards string */ if (formal == String.class) { converter = toString; } /* check for String -> Enum constant conversion */ - else if ((formal instanceof Class) && ((Class)formal).isEnum() && actual == String.class) + else if (formalClass != null && formalClass.isEnum() && actual == String.class) { - final Class<Enum> enumClass = (Class<Enum>)formal; + final Class<Enum> enumClass = (Class<Enum>)formalClass; converter = new Converter() { @Override @@ -620,9 +622,9 @@ public class TypeConversionHandlerImpl implements TypeConversionHandler { Pair<String, String> key = Pair.of(formal.getTypeName(), actual.getTypeName()); converterCacheMap.put(key, converter); - if (formal instanceof Class) + Class formalClass = IntrospectionUtils.getTypeClass(formal); + if (formalClass != null) { - Class formalClass = (Class)formal; if (formalClass.isPrimitive()) { key = Pair.of(IntrospectionUtils.getBoxedClass(formalClass).getTypeName(), actual.getTypeName()); diff --git a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java index 209d9f5d..89164575 100644 --- a/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java +++ b/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java @@ -141,22 +141,25 @@ public class UberspectImpl implements Uberspect, RuntimeServicesAware @Override public boolean isExplicitlyConvertible(Type formal, Class actual, boolean possibleVarArg) { - if (formal instanceof Class) return ch.isExplicitlyConvertible((Class)formal, actual, possibleVarArg); - else throw new UnsupportedOperationException("This conversion handler doesn't handle Types which aren't Classes"); + Class formalClass = IntrospectionUtils.getTypeClass(formal); + if (formalClass != null) return ch.isExplicitlyConvertible(formalClass, actual, possibleVarArg); + else return false; } @Override public Converter getNeededConverter(Type formal, Class actual) { - if (formal instanceof Class) return ch.getNeededConverter((Class)formal, actual); - else throw new UnsupportedOperationException("This conversion handler doesn't handle Types which aren't Classes"); + Class formalClass = IntrospectionUtils.getTypeClass(formal); + if (formalClass != null) return ch.getNeededConverter(formalClass, actual); + else return null; } @Override public void addConverter(Type formal, Class actual, Converter converter) { - if (formal instanceof Class) ch.addConverter((Class)formal, actual, converter); - else throw new UnsupportedOperationException("This conversion handler doesn't handle Types which aren't Classes"); + Class formalClass = IntrospectionUtils.getTypeClass(formal); + if (formalClass != null) ch.addConverter(formalClass, actual, converter); + else throw new UnsupportedOperationException("This conversion handler doesn't know how to handle Type: " + formal.getTypeName()); } }; } |