You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by cb...@apache.org on 2018/10/17 19:18:37 UTC

svn commit: r1844155 - in /velocity/engine/branches/VELOCITY-892/velocity-engine-core/src: main/java/org/apache/velocity/util/introspection/ test/java/org/apache/velocity/test/util/introspection/

Author: cbrisson
Date: Wed Oct 17 19:18:37 2018
New Revision: 1844155

URL: http://svn.apache.org/viewvc?rev=1844155&view=rev
Log:
[VELOCITY-892] Better handling of methods disambiguisation

Modified:
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
    velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java?rev=1844155&r1=1844154&r2=1844155&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/IntrospectionUtils.java Wed Oct 17 19:18:37 2018
@@ -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)
         {

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java?rev=1844155&r1=1844154&r2=1844155&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/MethodMap.java Wed Oct 17 19:18:37 2018
@@ -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:

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java?rev=1844155&r1=1844154&r2=1844155&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/TypeConversionHandlerImpl.java Wed Oct 17 19:18:37 2018
@@ -536,7 +536,8 @@ public class TypeConversionHandlerImpl i
          * 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 i
             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 i
     {
         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());

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=1844155&r1=1844154&r2=1844155&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/UberspectImpl.java Wed Oct 17 19:18:37 2018
@@ -141,22 +141,25 @@ public class UberspectImpl implements Ub
                     @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());
                     }
                 };
             }

Modified: velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java?rev=1844155&r1=1844154&r2=1844155&view=diff
==============================================================================
--- velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java (original)
+++ velocity/engine/branches/VELOCITY-892/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java Wed Oct 17 19:18:37 2018
@@ -19,6 +19,7 @@
 package org.apache.velocity.test.util.introspection;
 
 import junit.framework.TestSuite;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.reflect.TypeUtils;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
@@ -198,6 +199,15 @@ public class ConversionHandlerTestCase e
         }
     }
 
+    public void testOtherConversions() throws Exception
+    {
+        VelocityEngine ve = createEngine(false);
+        VelocityContext context = createContext();
+        StringWriter writer = new StringWriter();
+        ve.evaluate(context, writer,"test", "$strings.join(['foo', 'bar'], ',')");
+        assertEquals("foo,bar", writer.toString());
+    }
+
     /**
      * Return and initialize engine
      * @return
@@ -287,6 +297,7 @@ public class ConversionHandlerTestCase e
                 };
         context.put("types", types);
         context.put("introspect", new Introspect());
+        context.put("strings", new StringUtils());
         return context;
     }