You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/06/24 07:40:20 UTC
groovy git commit: Improve the robustness of asBoolean methods
Repository: groovy
Updated Branches:
refs/heads/master 852c85eb5 -> f4d5970f5
Improve the robustness of asBoolean methods
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f4d5970f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f4d5970f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f4d5970f
Branch: refs/heads/master
Commit: f4d5970f575be045697731a1d3e757d571171fa0
Parents: 852c85e
Author: sunlan <su...@apache.org>
Authored: Sat Jun 24 15:40:10 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Jun 24 15:40:10 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/f4d5970f/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 e504371..03f061c 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/f4d5970f/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();
}