diff options
author | Claude Brisson <cbrisson@apache.org> | 2016-10-02 07:43:40 +0000 |
---|---|---|
committer | Claude Brisson <cbrisson@apache.org> | 2016-10-02 07:43:40 +0000 |
commit | 436b47be02b2f13c62c5ad563a94fbba1546c18b (patch) | |
tree | 54a3e064f0e426c5a39d86e8352d046e181aea4d /velocity-engine-core/src | |
parent | cdac3ae7b9052741ee162a161c49fc32d7440bd4 (diff) | |
download | apache-velocity-engine-436b47be02b2f13c62c5ad563a94fbba1546c18b.tar.gz |
enhance Uberspector method disambiguation (+testcase)
git-svn-id: https://svn.apache.org/repos/asf/velocity/engine/trunk@1763040 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'velocity-engine-core/src')
3 files changed, 54 insertions, 3 deletions
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 5bdd5a46..3fd61c41 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 @@ -238,6 +238,7 @@ public class MethodMap bestMatchTypes = methodTypes; bestMatchComp = compare(bestMatchTypes, unboxedArgs); equivalentMatches = null; + ambiguities = 0; } break; } @@ -246,6 +247,24 @@ public class MethodMap break; case INCOMPARABLE: + /* do not retain method if it's more specific than (or incomparable to) provided (unboxed) arguments + * while best batch is less specific + */ + if (bestMatchComp == LESS_SPECIFIC && compare(methodTypes, unboxedArgs) != LESS_SPECIFIC) + { + break; + } + /* retain it anyway if less specific than (unboxed) provided args while + * bestmatch is more specific or incomparable + */ + if (bestMatchComp != LESS_SPECIFIC && compare(methodTypes, unboxedArgs) == LESS_SPECIFIC) + { + bestMatch = method; + bestMatchTypes = methodTypes; + bestMatchComp = compare(bestMatchTypes, unboxedArgs); + equivalentMatches = null; + break; + } if (equivalentMatches == null) { equivalentMatches = new ArrayList(bestMatchTypes.length); @@ -255,9 +274,9 @@ public class MethodMap case LESS_SPECIFIC: /* retain it anyway if less specific than (unboxed) provided args while - * bestmatch is more specific + * bestmatch is more specific or incomparable */ - if (bestMatchComp == MORE_SPECIFIC && compare(methodTypes, unboxedArgs) == LESS_SPECIFIC) + if (bestMatchComp != LESS_SPECIFIC && compare(methodTypes, unboxedArgs) == LESS_SPECIFIC) { bestMatch = method; bestMatchTypes = methodTypes; diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/UberspectorTestCase.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/UberspectorTestCase.java index 0f6d5f14..bce0355d 100644 --- a/velocity-engine-core/src/test/java/org/apache/velocity/test/UberspectorTestCase.java +++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/UberspectorTestCase.java @@ -77,7 +77,7 @@ public class UberspectorTestCase // Don't screw up on empty properties. That should map to get("") assertNotNull(getter); - assertEquals("Found wrong method", "get", getter. getMethodName()); + assertEquals("Found wrong method", "get", getter.getMethodName()); } public void testEmptyPropertySetter() @@ -259,6 +259,24 @@ public class UberspectorTestCase assertEquals("Found wrong method", "setpremium", setter.getMethodName()); } + public void testDisambiguation() + throws Exception + { + VelPropertySet setter; + + Uberspect u = ri.getUberspect(); + UberspectorTestObject uto = new UberspectorTestObject(); + + // setUnambigous() - String + setter = u.getPropertySet(uto, "unambiguous", "string", null); + assertNotNull(setter); + setter.invoke(uto, "string"); + + // setUnambigous() - HashMap + setter = u.getPropertySet(uto, "unambiguous", new HashMap(), null); + assertNotNull(setter); + setter.invoke(uto, new HashMap()); + } /* * diff --git a/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/UberspectorTestObject.java b/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/UberspectorTestObject.java index 491779e9..fea2f1ce 100644 --- a/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/UberspectorTestObject.java +++ b/velocity-engine-core/src/test/java/org/apache/velocity/test/misc/UberspectorTestObject.java @@ -19,6 +19,8 @@ package org.apache.velocity.test.misc; * under the License. */ +import java.util.Map; + public class UberspectorTestObject { private String regular; @@ -29,6 +31,8 @@ public class UberspectorTestObject private String ambigous; + private String unambiguous; + /** * @return the premium */ @@ -116,4 +120,14 @@ public class UberspectorTestObject { this.ambigous = ambigous.toString(); } + + public void setUnambiguous(String unambiguous) + { + this.unambiguous = unambiguous; + } + + public void setUnambiguous(Map unambiguous) + { + this.unambiguous = unambiguous.toString(); + } } |