You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/11/18 02:19:51 UTC

[groovy] branch GROOVY_3_0_X updated: favor early return

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

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 9731771  favor early return
9731771 is described below

commit 9731771e3df4ed599162cea89b7b44fface8b7cf
Author: Paul King <pa...@asert.com.au>
AuthorDate: Mon Nov 18 11:33:49 2019 +1000

    favor early return
---
 src/main/java/groovy/grape/Grape.java           |  10 +--
 src/main/java/groovy/lang/Closure.java          |  58 +++++++------
 src/main/java/groovy/lang/ExpandoMetaClass.java |   6 +-
 src/main/java/groovy/lang/MetaClassImpl.java    | 108 +++++++++++-------------
 4 files changed, 90 insertions(+), 92 deletions(-)

diff --git a/src/main/java/groovy/grape/Grape.java b/src/main/java/groovy/grape/Grape.java
index 7ab4328..4b2f22d 100644
--- a/src/main/java/groovy/grape/Grape.java
+++ b/src/main/java/groovy/grape/Grape.java
@@ -189,9 +189,8 @@ public class Grape {
         }
         if (grapes == null) {
             return Collections.emptyMap();
-        } else {
-            return grapes;
         }
+        return grapes;
     }
 
     public static URI[] resolve(Map<String, Object> args, Map... dependencies) {
@@ -214,9 +213,8 @@ public class Grape {
         }
         if (uris == null) {
             return EMPTY_URI_ARRAY;
-        } else {
-            return uris;
         }
+        return uris;
     }
 
     public static Map[] listDependencies(ClassLoader cl) {
@@ -229,10 +227,8 @@ public class Grape {
         }
         if (maps == null) {
             return EMPTY_MAP_ARRAY;
-        } else {
-            return maps;
         }
-
+        return maps;
     }
 
     public static void addResolver(Map<String, Object> args) {
diff --git a/src/main/java/groovy/lang/Closure.java b/src/main/java/groovy/lang/Closure.java
index 6c80414..2328ac0 100644
--- a/src/main/java/groovy/lang/Closure.java
+++ b/src/main/java/groovy/lang/Closure.java
@@ -266,35 +266,42 @@ public abstract class Closure<V> extends GroovyObjectSupport implements Cloneabl
     public Object getProperty(final String property) {
         if ("delegate".equals(property)) {
             return getDelegate();
-        } else if ("owner".equals(property)) {
+        }
+        if ("owner".equals(property)) {
             return getOwner();
-        } else if ("maximumNumberOfParameters".equals(property)) {
+        }
+        if ("maximumNumberOfParameters".equals(property)) {
             return getMaximumNumberOfParameters();
-        } else if ("parameterTypes".equals(property)) {
+        }
+        if ("parameterTypes".equals(property)) {
             return getParameterTypes();
-        } else if ("metaClass".equals(property)) {
+        }
+        if ("metaClass".equals(property)) {
             return getMetaClass();
-        } else if ("class".equals(property)) {
+        }
+        if ("class".equals(property)) {
             return getClass();
-        } else if ("directive".equals(property)) {
+        }
+        if ("directive".equals(property)) {
             return getDirective();
-        } else if ("resolveStrategy".equals(property)) {
+        }
+        if ("resolveStrategy".equals(property)) {
             return getResolveStrategy();
-        } else if ("thisObject".equals(property)) {
+        }
+        if ("thisObject".equals(property)) {
             return getThisObject();
-        } else {
-            switch(resolveStrategy) {
-                case DELEGATE_FIRST:
-                    return getPropertyDelegateFirst(property);
-                case DELEGATE_ONLY:
-                    return InvokerHelper.getProperty(this.delegate, property);
-                case OWNER_ONLY:
-                    return InvokerHelper.getProperty(this.owner, property);
-                case TO_SELF:
-                    return super.getProperty(property);
-                default:
-                    return getPropertyOwnerFirst(property);
-            }
+        }
+        switch(resolveStrategy) {
+            case DELEGATE_FIRST:
+                return getPropertyDelegateFirst(property);
+            case DELEGATE_ONLY:
+                return InvokerHelper.getProperty(this.delegate, property);
+            case OWNER_ONLY:
+                return InvokerHelper.getProperty(this.owner, property);
+            case TO_SELF:
+                return super.getProperty(property);
+            default:
+                return getPropertyOwnerFirst(property);
         }
     }
 
@@ -913,13 +920,14 @@ public abstract class Closure<V> extends GroovyObjectSupport implements Cloneabl
         public Object invokeMethod(String method, Object arguments) {
             if ("clone".equals(method)) {
                 return clone();
-            } else if ("curry".equals(method)) {
+            }
+            if ("curry".equals(method)) {
                 return curry((Object[]) arguments);
-            } else if ("asWritable".equals(method)) {
+            }
+            if ("asWritable".equals(method)) {
                 return asWritable();
-            } else {
-                return Closure.this.invokeMethod(method, arguments);
             }
+            return Closure.this.invokeMethod(method, arguments);
         }
 
         /* (non-Javadoc)
diff --git a/src/main/java/groovy/lang/ExpandoMetaClass.java b/src/main/java/groovy/lang/ExpandoMetaClass.java
index e3bdff7..3ecf073 100644
--- a/src/main/java/groovy/lang/ExpandoMetaClass.java
+++ b/src/main/java/groovy/lang/ExpandoMetaClass.java
@@ -1233,7 +1233,8 @@ public class ExpandoMetaClass extends MetaClassImpl implements GroovyObject {
         if (name.startsWith("get")) {
             name = name.substring(3);
             return isPropertyName(name);
-        } else if (name.startsWith("is")) {
+        }
+        if (name.startsWith("is")) {
             name = name.substring(2);
             return isPropertyName(name);
         }
@@ -1252,7 +1253,8 @@ public class ExpandoMetaClass extends MetaClassImpl implements GroovyObject {
         if (getterName.startsWith("get")) {
             String prop = getterName.substring(3);
             return MetaClassHelper.convertPropertyName(prop);
-        } else if (getterName.startsWith("is")) {
+        }
+        if (getterName.startsWith("is")) {
             String prop = getterName.substring(2);
             return MetaClassHelper.convertPropertyName(prop);
         }
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index 93573d2..cde9109 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -265,9 +265,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         final Object o = getMethods(getTheClass(), name, false);
         if (o instanceof FastArray) {
             return ((FastArray) o).toList();
-        } else {
-            return Collections.singletonList(o);
         }
+        return Collections.singletonList(o);
     }
 
     /**
@@ -815,9 +814,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         }
         if (arguments instanceof Object[]) {
             return invokeMethod(object, methodName, (Object[]) arguments);
-        } else {
-            return invokeMethod(object, methodName, new Object[]{arguments});
         }
+        return invokeMethod(object, methodName, new Object[]{arguments});
     }
 
     /**
@@ -851,9 +849,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 if (!isGetter) {
                     property.setProperty(instance, optionalValue);
                     return null;
-                } else {
-                    return property.getProperty(instance);
                 }
+                return property.getProperty(instance);
             }
             superClass = superClass.getCachedSuperClass();
         }
@@ -900,10 +897,9 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             if (metaProperty != null)
                 if (isGetter) {
                     return metaProperty.getProperty(instance);
-                } else {
-                    metaProperty.setProperty(instance, optionalValue);
-                    return null;
                 }
+                metaProperty.setProperty(instance, optionalValue);
+                return null;
         }
         throw new MissingPropertyExceptionNoStack(propertyName, theClass);
     }
@@ -1354,13 +1350,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         // let's try use the cache to find the method
         if (!isCallToSuper && GroovyCategorySupport.hasCategoryInCurrentThread()) {
             return getMethodWithoutCaching(sender, methodName, MetaClassHelper.convertToTypeArray(arguments), isCallToSuper);
-        } else {
-            final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(sender, methodName);
-            if (e == null)
-                return null;
-
-            return isCallToSuper ? getSuperMethodWithCaching(arguments, e) : getNormalMethodWithCaching(arguments, e);
         }
+        final MetaMethodIndex.Entry e = metaMethodIndex.getMethods(sender, methodName);
+        if (e == null) {
+            return null;
+        }
+
+        return isCallToSuper ? getSuperMethodWithCaching(arguments, e) : getNormalMethodWithCaching(arguments, e);
     }
 
     private static boolean sameClasses(Class[] params, Class[] arguments) {
@@ -1480,9 +1476,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             e.cachedStaticMethod = cacheEntry;
 
             return cacheEntry.method;
-        } else {
-            return pickStaticMethod(methodName, MetaClassHelper.convertToTypeArray(arguments));
         }
+        return pickStaticMethod(methodName, MetaClassHelper.convertToTypeArray(arguments));
     }
 
     public MetaMethod getMethodWithoutCaching(Class sender, String methodName, Class[] arguments, boolean isCallToSuper) {
@@ -1606,14 +1601,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     public int selectConstructorAndTransformArguments(int numberOfConstructors, Object[] arguments) {
         if (numberOfConstructors == -1) {
             return selectConstructorAndTransformArguments1(arguments);
-        } else {
-            // falling back to pre 2.1.9 selection algorithm
-            // in practice this branch will only be reached if the class calling this code is a Groovy class
-            // compiled with an earlier version of the Groovy compiler
-            return selectConstructorAndTransformArguments0(numberOfConstructors, arguments);
         }
-
-
+        // falling back to pre 2.1.9 selection algorithm
+        // in practice this branch will only be reached if the class calling this code is a Groovy class
+        // compiled with an earlier version of the Groovy compiler
+        return selectConstructorAndTransformArguments0(numberOfConstructors, arguments);
     }
 
     private int selectConstructorAndTransformArguments0(final int numberOfConstructors, Object[] arguments) {
@@ -1905,16 +1897,17 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             if (theClass != Class.class && object instanceof Class) {
                 MetaClass mc = registry.getMetaClass(Class.class);
                 return mc.getProperty(Class.class, object, name, useSuper, false);
-            } else if (object instanceof Collection) {
+            }
+            if (object instanceof Collection) {
                 return DefaultGroovyMethods.getAt((Collection) object, name);
-            } else if (object instanceof Object[]) {
+            }
+            if (object instanceof Object[]) {
                 return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
-            } else {
-                MetaMethod addListenerMethod = listeners.get(name);
-                if (addListenerMethod != null) {
-                    //TODO: one day we could try return the previously registered Closure listener for easy removal
-                    return null;
-                }
+            }
+            MetaMethod addListenerMethod = listeners.get(name);
+            if (addListenerMethod != null) {
+                //TODO: one day we could try return the previously registered Closure listener for easy removal
+                return null;
             }
         } else {
             //----------------------------------------------------------------------
@@ -1929,9 +1922,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         //----------------------------------------------------------------------
         if (isStatic || object instanceof Class) {
             return invokeStaticMissingProperty(object, name, null, true);
-        } else {
-            return invokeMissingProperty(object, name, null, true);
         }
+        return invokeMissingProperty(object, name, null, true);
     }
 
     public MetaProperty getEffectiveGetMetaProperty(final Class sender, final Object object, String name, final boolean useSuper) {
@@ -2027,7 +2019,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                     throw new UnsupportedOperationException();
                 }
             };
-        } else if (object instanceof Collection) {
+        }
+        if (object instanceof Collection) {
             return new MetaProperty(name, Object.class) {
                 public Object getProperty(Object object) {
                     return DefaultGroovyMethods.getAt((Collection) object, name);
@@ -2037,7 +2030,8 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                     throw new UnsupportedOperationException();
                 }
             };
-        } else if (object instanceof Object[]) {
+        }
+        if (object instanceof Object[]) {
             return new MetaProperty(name, Object.class) {
                 public Object getProperty(Object object) {
                     return DefaultGroovyMethods.getAt(Arrays.asList((Object[]) object), name);
@@ -2047,39 +2041,28 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                     throw new UnsupportedOperationException();
                 }
             };
-        } else {
-            MetaMethod addListenerMethod = listeners.get(name);
-            if (addListenerMethod != null) {
-                //TODO: one day we could try return the previously registered Closure listener for easy removal
-                return new MetaProperty(name, Object.class) {
-                    public Object getProperty(Object object) {
-                        return null;
-                    }
-
-                    public void setProperty(Object object, Object newValue) {
-                        throw new UnsupportedOperationException();
-                    }
-                };
-            }
         }
-
-        //----------------------------------------------------------------------
-        // error due to missing method/field
-        //----------------------------------------------------------------------
-        if (isStatic || object instanceof Class) {
+        MetaMethod addListenerMethod = listeners.get(name);
+        if (addListenerMethod != null) {
+            //TODO: one day we could try return the previously registered Closure listener for easy removal
             return new MetaProperty(name, Object.class) {
                 public Object getProperty(Object object) {
-                    return invokeStaticMissingProperty(object, name, null, true);
+                    return null;
                 }
 
                 public void setProperty(Object object, Object newValue) {
                     throw new UnsupportedOperationException();
                 }
             };
-        } else {
+        }
+
+        //----------------------------------------------------------------------
+        // error due to missing method/field
+        //----------------------------------------------------------------------
+        if (isStatic || object instanceof Class) {
             return new MetaProperty(name, Object.class) {
                 public Object getProperty(Object object) {
-                    return invokeMissingProperty(object, name, null, true);
+                    return invokeStaticMissingProperty(object, name, null, true);
                 }
 
                 public void setProperty(Object object, Object newValue) {
@@ -2087,6 +2070,15 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
                 }
             };
         }
+        return new MetaProperty(name, Object.class) {
+            public Object getProperty(Object object) {
+                return invokeMissingProperty(object, name, null, true);
+            }
+
+            public void setProperty(Object object, Object newValue) {
+                throw new UnsupportedOperationException();
+            }
+        };
     }
 
     private Tuple2<MetaMethod, MetaProperty> createMetaMethodAndMetaProperty(final Class senderForMP, final Class senderForCMG, final String name, final boolean useSuper, final boolean isStatic) {