You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2020/11/05 11:59:49 UTC

[commons-jexl] 01/02: JEXL: Java 8 and formatting

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

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git

commit d5adda773a5df0decdcffb3d4419b8d428849e2b
Author: henrib <he...@apache.org>
AuthorDate: Thu Nov 5 11:18:10 2020 +0100

    JEXL: Java 8 and formatting
---
 .../jexl3/internal/introspection/ClassMap.java     | 45 +++++-----
 .../commons/jexl3/introspection/JexlUberspect.java | 96 ++++++++++------------
 2 files changed, 71 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/internal/introspection/ClassMap.java b/src/main/java/org/apache/commons/jexl3/internal/introspection/ClassMap.java
index 43b9f02..002366f 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/introspection/ClassMap.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/introspection/ClassMap.java
@@ -39,6 +39,7 @@ import java.util.concurrent.ConcurrentMap;
  * <p>
  * Originally taken from the Velocity tree so we can be self-sufficient.
  * </p>
+ *
  * @see MethodKey
  * @since 1.0
  */
@@ -46,6 +47,7 @@ final class ClassMap {
     /**
      * A method that returns itself used as a marker for cache miss,
      * allows the underlying cache map to be strongly typed.
+     *
      * @return itself as a method
      */
     public static Method cacheMiss() {
@@ -57,7 +59,9 @@ final class ClassMap {
         }
     }
 
-    /** The cache miss marker method. */
+    /**
+     * The cache miss marker method.
+     */
     private static final Method CACHE_MISS = cacheMiss();
     /**
      * This is the cache to store and look up the method information.
@@ -73,11 +77,11 @@ final class ClassMap {
      * </p>
      * Uses ConcurrentMap since 3.0, marginally faster than 2.1 under contention.
      */
-    private final ConcurrentMap<MethodKey, Method> byKey = new ConcurrentHashMap<MethodKey, Method>();
+    private final ConcurrentMap<MethodKey, Method> byKey = new ConcurrentHashMap<>();
     /**
      * Keep track of all methods with the same name; this is not modified after creation.
      */
-    private final Map<String, Method[]> byName = new HashMap<String, Method[]>();
+    private final Map<String, Method[]> byName = new HashMap<>();
     /**
      * Cache of fields.
      */
@@ -86,9 +90,9 @@ final class ClassMap {
     /**
      * Standard constructor.
      *
-     * @param aClass the class to deconstruct.
+     * @param aClass      the class to deconstruct.
      * @param permissions the permissions to apply during introspection
-     * @param log    the logger.
+     * @param log         the logger.
      */
     @SuppressWarnings("LeakingThisInConstructor")
     ClassMap(Class<?> aClass, Permissions permissions, Log log) {
@@ -97,7 +101,7 @@ final class ClassMap {
         // eagerly cache public fields
         Field[] fields = aClass.getFields();
         if (fields.length > 0) {
-            Map<String, Field> cache = new HashMap<String, Field>();
+            Map<String, Field> cache = new HashMap<>();
             for (Field field : fields) {
                 if (permissions.allow(field)) {
                     cache.put(field.getName(), field);
@@ -111,6 +115,7 @@ final class ClassMap {
 
     /**
      * Find a Field using its name.
+     *
      * @param fname the field name
      * @return A Field object representing the field to invoke or null.
      */
@@ -120,6 +125,7 @@ final class ClassMap {
 
     /**
      * Gets the field names cached by this map.
+     *
      * @return the array of field names
      */
     String[] getFieldNames() {
@@ -128,6 +134,7 @@ final class ClassMap {
 
     /**
      * Gets the methods names cached by this map.
+     *
      * @return the array of method names
      */
     String[] getMethodNames() {
@@ -136,6 +143,7 @@ final class ClassMap {
 
     /**
      * Gets all the methods with a given name from this map.
+     *
      * @param methodName the seeked methods name
      * @return the array of methods (null or non-empty)
      */
@@ -160,6 +168,7 @@ final class ClassMap {
      * If nothing is found, then we must actually go
      * and introspect the method from the MethodMap.
      * </p>
+     *
      * @param methodKey the method key
      * @return A Method object representing the method to invoke or null.
      * @throws MethodKey.AmbiguousException When more than one method is a match for the parameters.
@@ -196,6 +205,7 @@ final class ClassMap {
     /**
      * Populate the Map of direct hits. These are taken from all the public methods
      * that our class, its parents and their implemented interfaces provide.
+     *
      * @param cache          the ClassMap instance we create
      * @param permissions    the permissions to apply during introspection
      * @param classToReflect the class to cache
@@ -221,15 +231,10 @@ final class ClassMap {
         }
         // now that we've got all methods keyed in, lets organize them by name
         if (!cache.byKey.isEmpty()) {
-            List<Method> lm = new ArrayList<Method>(cache.byKey.size());
+            List<Method> lm = new ArrayList<>(cache.byKey.size());
             lm.addAll(cache.byKey.values());
             // sort all methods by name
-            lm.sort(new Comparator<Method>() {
-                @Override
-                public int compare(Method o1, Method o2) {
-                    return o1.getName().compareTo(o2.getName());
-                }
-            });
+            lm.sort(Comparator.comparing(Method::getName));
             // put all lists of methods with same name in byName cache
             int start = 0;
             while (start < lm.size()) {
@@ -252,10 +257,11 @@ final class ClassMap {
 
     /**
      * Recurses up interface hierarchy to get all super interfaces.
-     * @param cache the cache to fill
+     *
+     * @param cache       the cache to fill
      * @param permissions the permissions to apply during introspection
-     * @param iface the interface to populate the cache from
-     * @param log   the Log
+     * @param iface       the interface to populate the cache from
+     * @param log         the Log
      */
     private static void populateWithInterface(ClassMap cache, Permissions permissions, Class<?> iface, Log log) {
         if (Modifier.isPublic(iface.getModifiers())) {
@@ -269,10 +275,11 @@ final class ClassMap {
 
     /**
      * Recurses up class hierarchy to get all super classes.
-     * @param cache the cache to fill
+     *
+     * @param cache       the cache to fill
      * @param permissions the permissions to apply during introspection
-     * @param clazz the class to populate the cache from
-     * @param log   the Log
+     * @param clazz       the class to populate the cache from
+     * @param log         the Log
      */
     private static void populateWithClass(ClassMap cache, Permissions permissions, Class<?> clazz, Log log) {
         try {
diff --git a/src/main/java/org/apache/commons/jexl3/introspection/JexlUberspect.java b/src/main/java/org/apache/commons/jexl3/introspection/JexlUberspect.java
index 41e014c..362fca8 100644
--- a/src/main/java/org/apache/commons/jexl3/introspection/JexlUberspect.java
+++ b/src/main/java/org/apache/commons/jexl3/introspection/JexlUberspect.java
@@ -39,7 +39,7 @@ public interface JexlUberspect {
      * These are used through 'strategies' to solve properties; a strategy orders a list of resolver types,
      * and each resolver type is tried in sequence; the first resolver that discovers a non null {s,g}etter
      * stops the search.
-     * 
+     *
      * @see JexlResolver
      * @see JexlUberspect#getPropertyGet
      * @see JexlUberspect#getPropertySet
@@ -49,7 +49,7 @@ public interface JexlUberspect {
 
         /**
          * Gets a property getter.
-         * 
+         *
          * @param uber       the uberspect
          * @param obj        the object
          * @param identifier the property identifier
@@ -59,7 +59,7 @@ public interface JexlUberspect {
 
         /**
          * Gets a property setter.
-         * 
+         *
          * @param uber       the uberspect
          * @param obj        the object
          * @param identifier the property identifier
@@ -74,7 +74,7 @@ public interface JexlUberspect {
      * <p>
      * Each resolver discovers how to set/get a property with different techniques; seeking
      * method names or field names, etc.
-     * 
+     *
      * @since 3.0
      */
     enum JexlResolver implements PropertyResolver {
@@ -98,19 +98,19 @@ public interface JexlUberspect {
 
         @Override
         public final JexlPropertyGet getPropertyGet(JexlUberspect uber, Object obj, Object identifier) {
-            return uber.getPropertyGet(Collections.<PropertyResolver>singletonList(this), obj, identifier);
+            return uber.getPropertyGet(Collections.singletonList(this), obj, identifier);
         }
 
         @Override
         public final JexlPropertySet getPropertySet(JexlUberspect uber, Object obj, Object identifier, Object arg) {
-            return uber.getPropertySet(Collections.<PropertyResolver>singletonList(this), obj, identifier, arg);
+            return uber.getPropertySet(Collections.singletonList(this), obj, identifier, arg);
         }
     }
 
     /**
      * A resolver types list tailored for POJOs, favors '.' over '[]'.
      */
-    List<PropertyResolver> POJO = Collections.<PropertyResolver>unmodifiableList(Arrays.asList(
+    List<PropertyResolver> POJO = Collections.unmodifiableList(Arrays.asList(
             JexlResolver.PROPERTY,
             JexlResolver.MAP,
             JexlResolver.LIST,
@@ -122,7 +122,7 @@ public interface JexlUberspect {
     /**
      * A resolver types list tailored for Maps, favors '[]' over '.'.
      */
-    List<PropertyResolver> MAP = Collections.<PropertyResolver>unmodifiableList(Arrays.asList(
+    List<PropertyResolver> MAP = Collections.unmodifiableList(Arrays.asList(
             JexlResolver.MAP,
             JexlResolver.LIST,
             JexlResolver.DUCK,
@@ -133,19 +133,19 @@ public interface JexlUberspect {
 
     /**
      * Determines property resolution strategy.
-     * 
+     *
      * <p>To use a strategy instance, you have to set it at engine creation using
      * {@link org.apache.commons.jexl3.JexlBuilder#strategy(JexlUberspect.ResolverStrategy)}
      * as in:</p>
-     * 
+     *
      * <code>JexlEngine jexl = new JexlBuilder().strategy(MY_STRATEGY).create();</code>
-     * 
+     *
      * @since 3.0
      */
     interface ResolverStrategy {
         /**
          * Applies this strategy to a list of resolver types.
-         * 
+         *
          * @param operator the property access operator, may be null
          * @param obj      the instance we seek to obtain a property setter/getter from, can not be null
          * @return the ordered list of resolvers types, must not be null
@@ -159,47 +159,41 @@ public interface JexlUberspect {
      * If the operator is '[]' or if the operator is null and the object is a map, use the MAP list of resolvers;
      * Other cases use the POJO list of resolvers.
      */
-    ResolverStrategy JEXL_STRATEGY = new ResolverStrategy() {
-        @Override
-        public List<PropertyResolver> apply(JexlOperator op, Object obj) {
-            if (op == JexlOperator.ARRAY_GET) {
-                return MAP;
-            }
-            if (op == JexlOperator.ARRAY_SET) {
-                return MAP;
-            }
-            if (op == null && obj instanceof Map) {
-                return MAP;
-            }
-            return POJO;
+    ResolverStrategy JEXL_STRATEGY = (op, obj) -> {
+        if (op == JexlOperator.ARRAY_GET) {
+            return MAP;
         }
+        if (op == JexlOperator.ARRAY_SET) {
+            return MAP;
+        }
+        if (op == null && obj instanceof Map) {
+            return MAP;
+        }
+        return POJO;
     };
 
     /**
      * The map strategy.
-     * 
+     *
      * <p>If the operator is '[]' or if the object is a map, use the MAP list of resolvers.
      * Otherwise, use the POJO list of resolvers.</p>
      */
-    ResolverStrategy MAP_STRATEGY = new ResolverStrategy() {
-        @Override
-        public List<PropertyResolver> apply(JexlOperator op, Object obj) {
-            if (op == JexlOperator.ARRAY_GET) {
-                return MAP;
-            }
-            if (op == JexlOperator.ARRAY_SET) {
-                return MAP;
-            }
-            if (obj instanceof Map) {
-                return MAP;
-            }
-            return POJO;
+    ResolverStrategy MAP_STRATEGY = (op, obj) -> {
+        if (op == JexlOperator.ARRAY_GET) {
+            return MAP;
+        }
+        if (op == JexlOperator.ARRAY_SET) {
+            return MAP;
+        }
+        if (obj instanceof Map) {
+            return MAP;
         }
+        return POJO;
     };
 
     /**
      * Applies this uberspect property resolver strategy.
-     * 
+     *
      * @param op the operator
      * @param obj the object
      * @return the applied strategy resolver list
@@ -208,13 +202,13 @@ public interface JexlUberspect {
 
     /**
      * Sets the class loader to use.
-     * 
+     *
      * <p>This increments the version.</p>
-     * 
+     *
      * @param loader the class loader
      */
     void setClassLoader(ClassLoader loader);
-    
+
     /**
      * Gets the current class loader.
      * @return the class loader
@@ -223,14 +217,14 @@ public interface JexlUberspect {
 
     /**
      * Gets this uberspect version.
-     * 
+     *
      * @return the class loader modification count
      */
     int getVersion();
 
     /**
      * Returns a class constructor.
-     * 
+     *
      * @param ctorHandle a class or class name
      * @param args       constructor arguments
      * @return a {@link JexlMethod}
@@ -240,7 +234,7 @@ public interface JexlUberspect {
 
     /**
      * Returns a JexlMethod.
-     * 
+     *
      * @param obj    the object
      * @param method the method name
      * @param args   method arguments
@@ -250,9 +244,9 @@ public interface JexlUberspect {
 
     /**
      * Property getter.
-     * 
+     *
      * <p>returns a JelPropertySet apropos to an expression like <code>bar.woogie</code>.</p>
-     * 
+     *
      * @param obj        the object to get the property from
      * @param identifier property name
      * @return a {@link JexlPropertyGet} or null
@@ -277,7 +271,7 @@ public interface JexlUberspect {
      * Property setter.
      * <p>
      * Seeks a JelPropertySet apropos to an expression like  <code>foo.bar = "geir"</code>.</p>
-     * 
+     *
      * @param obj        the object to get the property from.
      * @param identifier property name
      * @param arg        value to set
@@ -302,7 +296,7 @@ public interface JexlUberspect {
 
     /**
      * Gets an iterator from an object.
-     * 
+     *
      * @param obj to get the iterator from
      * @return an iterator over obj or null
      */
@@ -310,7 +304,7 @@ public interface JexlUberspect {
 
     /**
      * Gets an arithmetic operator resolver for a given arithmetic instance.
-     * 
+     *
      * @param arithmetic the arithmetic instance
      * @return the arithmetic uberspect or null if no operator method were overridden
      * @since 3.0