velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1857752 - /velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
Date Thu, 18 Apr 2019 14:53:23 GMT
Author: cbrisson
Date: Thu Apr 18 14:53:23 2019
New Revision: 1857752

URL: http://svn.apache.org/viewvc?rev=1857752&view=rev
Log:
[tools/model] Gather getters/setters discovery code in generic tools' ClassUtils

Modified:
    velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java

Modified: velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java?rev=1857752&r1=1857751&r2=1857752&view=diff
==============================================================================
--- velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
(original)
+++ velocity/tools/branches/model/velocity-tools-generic/src/main/java/org/apache/velocity/tools/ClassUtils.java
Thu Apr 18 14:53:23 2019
@@ -38,6 +38,8 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Predicate;
+
 import org.apache.velocity.util.ArrayIterator;
 import org.apache.velocity.util.EnumerationIterator;
 
@@ -510,4 +512,70 @@ public class ClassUtils
         return ret;
     }
 
+    public static Method findGetter(String getterName, Class clazz) throws NoSuchMethodException
+    {
+        return findGetter(getterName, clazz, true);
+    }
+
+    public static Method findGetter(String getterName, Class clazz, boolean mandatory) throws
NoSuchMethodException
+    {
+        do
+        {
+            for (Method method : clazz.getDeclaredMethods())
+            {
+                // prefix matching: we allow a method name like setWriteAccess for a parameter
like write="..."
+                if (method.getParameterCount() == 0 && method.getName().startsWith(getterName))
+                {
+                    return method;
+                }
+            }
+            clazz = clazz.getSuperclass();
+        }
+        while (clazz != Object.class);
+        if (mandatory)
+        {
+            throw new NoSuchMethodException(clazz.getName() + "::" + getterName);
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+    public static Method findSetter(String setterName, Class clazz) throws NoSuchMethodException
+    {
+        return findSetter(setterName, clazz, x -> true);
+    }
+
+    public static Method findSetter(String setterName, Class clazz, Predicate<Class>
argumentClassFilter) throws NoSuchMethodException
+    {
+        return findSetter(setterName, clazz, argumentClassFilter, true);
+    }
+
+    public static Method findSetter(String setterName, Class clazz, Predicate<Class>
argumentClassFilter, boolean mandatory) throws NoSuchMethodException
+    {
+        do
+        {
+            for (Method method : clazz.getDeclaredMethods())
+            {
+                // prefix matching: we allow a method name like setWriteAccess for a parameter
like write="..."
+                if (method.getParameterCount() == 1 && method.getName().startsWith(setterName)
&& argumentClassFilter.test(method.getParameterTypes()[0]))
+                {
+                    return method;
+                }
+            }
+            clazz = clazz.getSuperclass();
+        }
+        while (clazz != Object.class);
+        if (mandatory)
+        {
+            throw new NoSuchMethodException(clazz.getName() + "::" + setterName);
+        }
+        else
+        {
+            return null;
+        }
+    }
+
+
 }



Mime
View raw message