This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler-java.git

commit 4f0b70d81f317cc3e88ae8075d4b4ff494d3b9ed
Author: Radu Cotescu <[email protected]>
AuthorDate: Fri Aug 25 12:50:08 2017 +0000

    SLING-7085 - Reduce code duplication
    
    * exported org.apache.sling.scripting.sightly.compiler.util.ObjectModel
    * delegated calls from 
org.apache.sling.scripting.sightly.render.AbstractRuntimeObjectModel to 
ObjectModel
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1806167 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../sightly/render/AbstractRuntimeObjectModel.java | 326 ++++++---------------
 1 file changed, 93 insertions(+), 233 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
 
b/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
index 2fa4314..3518f55 100644
--- 
a/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
+++ 
b/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
@@ -16,57 +16,36 @@
  
******************************************************************************/
 package org.apache.sling.scripting.sightly.render;
 
-import java.lang.reflect.Array;
-import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
-import java.util.Enumeration;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.sling.scripting.sightly.Record;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.sling.scripting.sightly.compiler.util.ObjectModel;
 
 /**
  * Default abstract implementation of {@link RuntimeObjectModel}.
  */
 public abstract class AbstractRuntimeObjectModel implements RuntimeObjectModel 
{
 
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractRuntimeObjectModel.class);
-
     /**
      * A {@link Set} that stores all the supported primitive classes.
      */
-    public static final Set<Class<?>> PRIMITIVE_CLASSES = 
Collections.unmodifiableSet(new HashSet<Class<?>>() {{
-        add(Boolean.class);
-        add(Character.class);
-        add(Byte.class);
-        add(Short.class);
-        add(Integer.class);
-        add(Long.class);
-        add(Float.class);
-        add(Double.class);
-        add(Void.class);
-    }});
+    public static final Set<Class<?>> PRIMITIVE_CLASSES = 
ObjectModel.PRIMITIVE_CLASSES;
 
     public static final String TO_STRING_METHOD = "toString";
 
     @Override
     public boolean isPrimitive(Object obj) {
-        return PRIMITIVE_CLASSES.contains(obj.getClass());
+        return ObjectModel.isPrimitive(obj);
     }
 
     @Override
@@ -83,7 +62,7 @@ public abstract class AbstractRuntimeObjectModel implements 
RuntimeObjectModel {
             return true;
         }
         String value = toString(target);
-        return NumberUtils.isNumber(value);
+        return NumberUtils.isCreatable(value);
     }
 
     @Override
@@ -96,7 +75,7 @@ public abstract class AbstractRuntimeObjectModel implements 
RuntimeObjectModel {
     public Object resolveProperty(Object target, Object property) {
         Object resolved;
         if (property instanceof Number) {
-            resolved = getIndex(target, ((Number) property).intValue());
+            resolved = ObjectModel.getIndex(target, ((Number) 
property).intValue());
         } else {
             resolved = getProperty(target, property);
         }
@@ -105,25 +84,15 @@ public abstract class AbstractRuntimeObjectModel 
implements RuntimeObjectModel {
 
     @Override
     public boolean toBoolean(Object object) {
-        return toBooleanInternal(object);
+        return ObjectModel.toBoolean(object);
     }
 
     @Override
     public Number toNumber(Object object) {
-        if (object == null) {
-            return null;
-        }
-        if (object instanceof Number) {
-            return (Number) object;
-        }
-        String stringValue = toString(object);
-        try {
-            return NumberUtils.createNumber(stringValue);
-        } catch (NumberFormatException e) {
-            return null;
-        }
+        return ObjectModel.toNumber(object);
     }
 
+    @Override
     public Date toDate(Object object) {
         if (object instanceof Date) {
             return (Date)object;
@@ -135,12 +104,15 @@ public abstract class AbstractRuntimeObjectModel 
implements RuntimeObjectModel {
 
     @Override
     public String toString(Object target) {
-        return objectToString(target);
+        return ObjectModel.toString(target);
     }
 
     @Override
     public Collection<Object> toCollection(Object object) {
-        return obtainCollection(object);
+        if (object instanceof Record) {
+            return ((Record) object).getPropertyNames();
+        }
+        return ObjectModel.toCollection(object);
     }
 
     @Override
@@ -150,7 +122,6 @@ public abstract class AbstractRuntimeObjectModel implements 
RuntimeObjectModel {
         } else if (object instanceof Record) {
             Map<String, Object> map = new HashMap<>();
             Record record = (Record) object;
-            @SuppressWarnings("unchecked")
             Set<String> properties = record.getPropertyNames();
             for (String property : properties) {
                 map.put(property, record.getProperty(property));
@@ -160,247 +131,136 @@ public abstract class AbstractRuntimeObjectModel 
implements RuntimeObjectModel {
         return Collections.emptyMap();
     }
 
-    protected String objectToString(Object obj) {
-        String output = "";
-        if (obj != null) {
-            if (obj instanceof String) {
-                output = (String) obj;
-            } else if (isPrimitive(obj)) {
-                output = obj.toString();
-            } else if (obj instanceof Enum) {
-                return ((Enum) obj).name();
-            } else {
-                Collection<?> col = obtainCollection(obj);
-                if (col != null) {
-                    output = collectionToString(col);
-                }
-            }
-        }
-        return output;
-    }
-
     protected Object getProperty(Object target, Object propertyObj) {
-        String property = toString(propertyObj);
-        if (StringUtils.isEmpty(property)) {
-            throw new IllegalArgumentException("Invalid property name");
-        }
-        if (target == null) {
-            return null;
-        }
+        String property = ObjectModel.toString(propertyObj);
         Object result = null;
-        if (target instanceof Map) {
-            result = getMapProperty((Map) target, property);
-        }
-        if (result == null && target instanceof Record) {
+        if (target instanceof Record) {
             result = ((Record) target).getProperty(property);
         }
         if (result == null) {
-            result = getObjectProperty(target, property);
+            result = ObjectModel.resolveProperty(target, property);
         }
         return result;
     }
 
-    @SuppressWarnings("unchecked")
+    /**
+     * @deprecated see {@link ObjectModel#toCollection(Object)}
+     */
+    @Deprecated
     protected Collection<Object> obtainCollection(Object obj) {
-        if (obj == null) {
-            return Collections.emptyList();
-        }
-        if (obj instanceof Object[]) {
-            return Arrays.asList((Object[]) obj);
-        }
-        if (obj instanceof Collection) {
-            return (Collection<Object>) obj;
-        }
-        if (obj instanceof Map) {
-            return ((Map) obj).keySet();
-        }
-        if (obj instanceof Record) {
-            return ((Record) obj).getPropertyNames();
-        }
-        if (obj instanceof Enumeration) {
-            return Collections.list((Enumeration<Object>) obj);
-        }
-        if (obj instanceof Iterator) {
-            return fromIterator((Iterator<Object>) obj);
-        }
-        if (obj instanceof Iterable) {
-            Iterable iterable = (Iterable) obj;
-            return fromIterator(iterable.iterator());
-        }
-        if (obj instanceof String || obj instanceof Number) {
-            Collection list = new ArrayList();
-            list.add(obj);
-            return list;
-        }
-        return Collections.emptyList();
+        return ObjectModel.toCollection(obj);
+    }
+
+    /**
+     * @deprecated see {@link ObjectModel#toString(Object)}
+     */
+    @Deprecated
+    protected String objectToString(Object obj) {
+        return ObjectModel.toString(obj);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#collectionToString(Collection)}
+     */
+    @Deprecated
     protected String collectionToString(Collection<?> col) {
-        StringBuilder builder = new StringBuilder();
-        String prefix = "";
-        for (Object o : col) {
-            builder.append(prefix).append(objectToString(o));
-            prefix = ",";
-        }
-        return builder.toString();
+        return ObjectModel.collectionToString(col);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#fromIterator(Iterator)}
+     */
+    @Deprecated
     protected Collection<Object> fromIterator(Iterator<Object> iterator) {
-        ArrayList<Object> result = new ArrayList<>();
-        while (iterator.hasNext()) {
-            result.add(iterator.next());
-        }
-        return result;
+        return ObjectModel.fromIterator(iterator);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#toBoolean(Object)}
+     */
+    @Deprecated
     protected boolean toBooleanInternal(Object obj) {
-        if (obj == null) {
-            return false;
-        }
-
-        if (obj instanceof Number) {
-            Number number = (Number) obj;
-            return !(number.doubleValue() == 0.0);
-        }
-
-        String s = obj.toString().trim();
-        if ("".equals(s)) {
-            return false;
-        } else if ("true".equalsIgnoreCase(s) || "false".equalsIgnoreCase(s)) {
-            return Boolean.parseBoolean(s);
-        }
-
-        if (obj instanceof Collection) {
-            return ((Collection) obj).size() > 0;
-        }
-
-        if (obj instanceof Map) {
-            return ((Map) obj).size() > 0;
-        }
-
-        if (obj instanceof Iterable<?>) {
-            return ((Iterable<?>) obj).iterator().hasNext();
-        }
-
-        if (obj instanceof Iterator<?>) {
-            return ((Iterator<?>) obj).hasNext();
-        }
-
-        return !(obj instanceof Object[]) || ((Object[]) obj).length > 0;
+        return ObjectModel.toBoolean(obj);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#getIndex(Object, int)}
+     */
+    @Deprecated
     protected Object getIndex(Object obj, int index) {
-        if (obj instanceof Map) {
-            Map map = (Map) obj;
-            if (map.containsKey(index)) {
-                return map.get(index);
-            }
-        }
-        Collection collection = toCollection(obj);
-        if (collection instanceof List) {
-            return getIndexSafe((List) collection, index);
-        }
-        return null;
+        return ObjectModel.getIndex(obj, index);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#getIndex(Object, int)}
+     */
+    @Deprecated
     protected Object getIndexSafe(List list, int index) {
-        if (index < 0 || index >= list.size()) {
-            return null;
-        }
-        return list.get(index);
+        return ObjectModel.getIndex(list, index);
     }
 
+    /**
+     * @deprecated use {@link Map#get(Object)}
+     */
+    @Deprecated
     protected Object getMapProperty(Map map, String property) {
         return map.get(property);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#resolveProperty(Object, Object)}
+     */
+    @Deprecated
     protected Object getObjectProperty(Object obj, String property) {
-        Object result = getObjectNoArgMethod(obj, property);
-        if (result == null) {
-            result = getField(obj, property);
-        }
-        return result;
+        return ObjectModel.resolveProperty(obj, property);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#getField(Object, String)}
+     */
+    @Deprecated
     protected static Object getField(Object obj, String property) {
-        Class<?> cls = obj.getClass();
-        if (cls.isArray() && "length".equals(property)) {
-            return Array.getLength(obj);
-        }
-        try {
-            Field field = cls.getDeclaredField(property);
-            return field.get(obj);
-        } catch (Exception e) {
-            return null;
-        }
+        return ObjectModel.getField(obj, property);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#invokeBeanMethod(Object, String)}
+     */
+    @Deprecated
     protected Object getObjectNoArgMethod(Object obj, String property) {
-        Class<?> cls = obj.getClass();
-        Method method = findMethod(cls, property);
-        if (method != null) {
-            method = extractMethodInheritanceChain(cls, method);
-            try {
-                return method.invoke(obj);
-            } catch (Exception e) {
-                LOGGER.error("Cannot access method " + property + " on object 
" + obj.toString(), e);
-            }
-        }
-        return null;
+        return ObjectModel.invokeBeanMethod(obj, property);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#findBeanMethod(Class, String)}
+     */
+    @Deprecated
     protected static Method findMethod(Class<?> cls, String baseName) {
-        Method[] publicMethods = cls.getMethods();
-        String capitalized = StringUtils.capitalize(baseName);
-        for (Method m : publicMethods) {
-            if (m.getParameterTypes().length == 0) {
-                String methodName = m.getName();
-                if (baseName.equals(methodName) || ("get" + 
capitalized).equals(methodName) || ("is" + capitalized).equals(methodName)) {
-                    // this method is good, check whether allowed
-                    if (isMethodAllowed(m)) {
-                        return m;
-                    }
-                    // method would match but is not allowed, abort
-                    break;
-                }
-            }
-        }
-        return null;
+        return ObjectModel.findBeanMethod(cls, baseName);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#isMethodAllowed(Method)}
+     */
+    @Deprecated
     protected static boolean isMethodAllowed(Method method) {
-        Class<?> declaringClass = method.getDeclaringClass();
-        //methods of the Object.class are forbidden (except toString, which is 
allowed)
-        return declaringClass != Object.class || 
TO_STRING_METHOD.equals(method.getName());
+       return ObjectModel.isMethodAllowed(method);
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#findBeanMethod(Class, String)} 
(Class, Method)}
+     */
+    @Deprecated
     protected Method extractMethodInheritanceChain(Class type, Method m) {
-        if (m == null || Modifier.isPublic(type.getModifiers())) {
-            return m;
-        }
-        Class[] iFaces = type.getInterfaces();
-        Method mp;
-        for (Class<?> iFace : iFaces) {
-            mp = getClassMethod(iFace, m);
-            if (mp != null) {
-                return mp;
-            }
-        }
-        return getClassMethod(type.getSuperclass(), m);
+        return ObjectModel.findBeanMethod(type, m.getName());
     }
 
+    /**
+     * @deprecated see {@link ObjectModel#findBeanMethod(Class, String)} 
(Class, Method)}
+     */
+    @Deprecated
     protected Method getClassMethod(Class<?> clazz, Method m) {
-        Method mp;
-        try {
-            mp = clazz.getMethod(m.getName(), m.getParameterTypes());
-            mp = extractMethodInheritanceChain(mp.getDeclaringClass(), mp);
-            if (mp != null) {
-                return mp;
-            }
-        } catch (NoSuchMethodException e) {
-            // do nothing
-        }
-        return null;
+        return ObjectModel.findBeanMethod(clazz, m.getName());
     }
 
 }

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to