Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id BA5D4200CB1 for ; Sat, 24 Jun 2017 10:41:45 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id B9164160BE6; Sat, 24 Jun 2017 08:41:45 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C9DD5160BDA for ; Sat, 24 Jun 2017 10:41:44 +0200 (CEST) Received: (qmail 89141 invoked by uid 500); 24 Jun 2017 08:41:44 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 89104 invoked by uid 99); 24 Jun 2017 08:41:43 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 24 Jun 2017 08:41:43 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id EBE0AE0061; Sat, 24 Jun 2017 08:41:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sunlan@apache.org To: commits@groovy.apache.org Date: Sat, 24 Jun 2017 08:41:41 -0000 Message-Id: <661485b329ba444eaa0dd3d6591c6826@git.apache.org> In-Reply-To: <293d806c3fd24a44b88ba5722f9ac5ce@git.apache.org> References: <293d806c3fd24a44b88ba5722f9ac5ce@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/5] groovy git commit: Improve the robustness of asBoolean methods archived-at: Sat, 24 Jun 2017 08:41:45 -0000 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/4c09080f Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/4c09080f Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/4c09080f Branch: refs/heads/GROOVY_2_6_X Commit: 4c09080f4bb9adf8ee155fd0c1ed3af067a62d03 Parents: 19fc000 Author: sunlan Authored: Sat Jun 24 15:40:10 2017 +0800 Committer: sunlan Committed: Sat Jun 24 16:40:35 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/4c09080f/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/4c09080f/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(); }