Improve the robustness of asBoolean methods

(cherry picked from commit f4d5970)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e01601ed
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e01601ed
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e01601ed

Branch: refs/heads/GROOVY_2_5_X
Commit: e01601edeb40f0e7da6f7985a7bf6563753d4dcc
Parents: 14e5cf5
Author: sunlan <[email protected]>
Authored: Sat Jun 24 15:40:10 2017 +0800
Committer: sunlan <[email protected]>
Committed: Sat Jun 24 16:43:15 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    | 66 +++++++++++++++++++-
 .../groovy/runtime/StringGroovyMethods.java     |  8 +++
 2 files changed, 73 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/e01601ed/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 
b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index de543fa..2581b46 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -104,7 +104,7 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
     private static final BigInteger BI_LONG_MAX = 
BigInteger.valueOf(Long.MAX_VALUE);
     private static final BigInteger BI_LONG_MIN = 
BigInteger.valueOf(Long.MIN_VALUE);
 
-    public static final Class [] ADDITIONAL_CLASSES = {
+    public static final Class[] ADDITIONAL_CLASSES = {
             NumberNumberPlus.class,
             NumberNumberMultiply.class,
             NumberNumberMinus.class,
@@ -10794,6 +10794,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Boolean bool) {
+        if (null == bool) {
+            return false;
+        }
+
         return bool;
     }
 
@@ -10808,6 +10812,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Collection collection) {
+        if (null == collection) {
+            return false;
+        }
+
         return !collection.isEmpty();
     }
 
@@ -10822,6 +10830,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Map map) {
+        if (null == map) {
+            return false;
+        }
+
         return !map.isEmpty();
     }
 
@@ -10835,6 +10847,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Iterator iterator) {
+        if (null == iterator) {
+            return false;
+        }
+
         return iterator.hasNext();
     }
 
@@ -10848,6 +10864,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Enumeration enumeration) {
+        if (null == enumeration) {
+            return false;
+        }
+
         return enumeration.hasMoreElements();
     }
 
@@ -10861,6 +10881,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Object[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10874,6 +10898,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(byte[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10887,6 +10915,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(short[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10900,6 +10932,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(int[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10913,6 +10949,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(long[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10926,6 +10966,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(float[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10939,6 +10983,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(double[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10952,6 +11000,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(boolean[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10965,6 +11017,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.4
      */
     public static boolean asBoolean(char[] array) {
+        if (null == array) {
+            return false;
+        }
+
         return array.length > 0;
     }
 
@@ -10979,6 +11035,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      */
 
     public static boolean asBoolean(Character character) {
+        if (null == character) {
+            return false;
+        }
+
         return character != 0;
     }
 
@@ -10992,6 +11052,10 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Number number) {
+        if (null == number) {
+            return false;
+        }
+
         return number.doubleValue() != 0;
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/e01601ed/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java 
b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
index c6fa15e..02e7199 100644
--- a/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/StringGroovyMethods.java
@@ -84,6 +84,10 @@ public class StringGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(CharSequence string) {
+        if (null == string) {
+            return false;
+        }
+
         return string.length() > 0;
     }
 
@@ -95,6 +99,10 @@ public class StringGroovyMethods extends 
DefaultGroovyMethodsSupport {
      * @since 1.7.0
      */
     public static boolean asBoolean(Matcher matcher) {
+        if (null == matcher) {
+            return false;
+        }
+
         RegexSupport.setLastMatcher(matcher);
         return matcher.find();
     }

Reply via email to