You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2015/10/08 10:41:13 UTC

svn commit: r1707449 - /sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java

Author: radu
Date: Thu Oct  8 08:41:13 2015
New Revision: 1707449

URL: http://svn.apache.org/viewvc?rev=1707449&view=rev
Log:
SLING-5124 - Optimise RenderUtils to avoid throwing exceptions when looking for object methods

* switch implementation to return null instead of throwing NoSuchMethodException

Modified:
    sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java

Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java?rev=1707449&r1=1707448&r2=1707449&view=diff
==============================================================================
--- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java (original)
+++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/RenderUtils.java Thu Oct  8 08:41:13 2015
@@ -37,10 +37,13 @@ import org.apache.commons.lang.StringUti
 import org.apache.sling.api.adapter.Adaptable;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.scripting.sightly.Record;
-import org.apache.sling.scripting.sightly.SightlyException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class RenderUtils {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(RenderUtils.class);
+
     public static final String TO_STRING_METHOD = "toString";
     public static final String PROPERTY_ACCESS = "resolveProperty";
     public static final String COLLECTION_COERCE = "toCollection";
@@ -108,6 +111,7 @@ public class RenderUtils {
      * @param object - the target object
      * @return - a map representation of the object. Default is an empty map
      */
+    @SuppressWarnings("unchecked")
     public static Map toMap(Object object) {
         if (object instanceof Map) {
             return (Map) object;
@@ -242,11 +246,7 @@ public class RenderUtils {
             return ((Iterator<?>) obj).hasNext();
         }
 
-        if (obj instanceof Object[]) {
-            return ((Object[]) obj).length > 0;
-        }
-
-        return true;
+        return !(obj instanceof Object[]) || ((Object[]) obj).length > 0;
     }
 
     private static Object getIndex(Object obj, int index) {
@@ -282,11 +282,11 @@ public class RenderUtils {
     }
 
     private static Object getObjectProperty(Object obj, String property) {
-        try {
-            return getObjectNoArgMethod(obj, property);
-        } catch (NoSuchMethodException nsmex) {
-            return getField(obj, property);
+        Object result = getObjectNoArgMethod(obj, property);
+        if (result == null) {
+            result = getField(obj, property);
         }
+        return result;
     }
 
     private static Object getField(Object obj, String property) {
@@ -303,18 +303,21 @@ public class RenderUtils {
         }
     }
 
-    private static Object getObjectNoArgMethod(Object obj, String property) throws NoSuchMethodException {
+    private static Object getObjectNoArgMethod(Object obj, String property) {
         Class<?> cls = obj.getClass();
         Method method = findMethod(cls, property);
-        try {
+        if (method != null) {
             method = extractMethodInheritanceChain(cls, method);
-            return method.invoke(obj);
-        } catch (Exception e) {
-            throw new SightlyException(e);
+            try {
+                return method.invoke(obj);
+            } catch (Exception e) {
+                LOGGER.error("Cannot access method " + property + " on object " + obj.toString(), e);
+            }
         }
+        return null;
     }
 
-    private static Method findMethod(Class<?> cls, String baseName) throws NoSuchMethodException {
+    private static Method findMethod(Class<?> cls, String baseName) {
         Method[] publicMethods = cls.getMethods();
         String capitalized = StringUtils.capitalize(baseName);
         for (Method m : publicMethods) {
@@ -329,13 +332,13 @@ public class RenderUtils {
                         return m;
                     }
 
-                    // method would match but is not allwed, abort
+                    // method would match but is not allowed, abort
                     break;
                 }
             }
         }
 
-        throw new NoSuchMethodException(baseName);
+        return null;
     }
 
     private static boolean isMethodAllowed(Method method) {