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/04/16 07:29:07 UTC

[groovy] 02/03: remove codenarc warnings

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

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

commit 1bb3e041672f719745cbc523b7d342296014791a
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Apr 16 17:02:30 2019 +1000

    remove codenarc warnings
---
 src/test/groovy/PrimitiveTypesTest.groovy          | 21 +++--
 src/test/groovy/bugs/Groovy4078Bug.groovy          | 98 +++++++++++-----------
 .../operator/BigIntegerOperationsTest.groovy       |  2 +-
 .../transform/tailrec/TailRecursiveExamples.groovy |  4 +-
 .../src/main/java/groovy/util/AntBuilder.java      |  5 +-
 .../main/java/org/codehaus/groovy/ant/Groovy.java  |  3 +-
 .../tools/shell/util/PackageHelperImpl.groovy      |  4 +-
 .../groovy/binding/ClosureTriggerBinding.java      |  5 +-
 8 files changed, 73 insertions(+), 69 deletions(-)

diff --git a/src/test/groovy/PrimitiveTypesTest.groovy b/src/test/groovy/PrimitiveTypesTest.groovy
index eacd6d8..15cf1b7 100644
--- a/src/test/groovy/PrimitiveTypesTest.groovy
+++ b/src/test/groovy/PrimitiveTypesTest.groovy
@@ -21,35 +21,35 @@ package groovy
 class PrimitiveTypesTest extends GroovyTestCase {
 
     int getInt() {
-        return 1;
+        return 1
     }
     
     short getShort() {
-        return 1;
+        return 1
     }
     
     boolean getBoolean() {
-        return true;
+        return true
     }
     
     double getDouble() {
-        return 1.0;
+        return 1.0
     }
     
     float getFloat() {
-        return 1.0;
+        return 1.0
     }
     
     byte getByte() {
-        return 1;
+        return 1
     }
     
     long getLong() {
-        return 1;
+        return 1
     }
 
     char getChar() {
-        return 'a';
+        return 'a'
     }
     
     int getNextInt(int i) {
@@ -85,15 +85,14 @@ class PrimitiveTypesTest extends GroovyTestCase {
     }
 
     void testBigInteger2BigDecimal() {
-        BigInteger big = new BigInteger(Long.MAX_VALUE)
+        BigInteger big = BigInteger.valueOf(Long.MAX_VALUE)
         assert big.longValue() == testMethod(big).longValueExact()
     }
 
     private testMethod(BigDecimal bd) {
-        return bd;
+        return bd
     }
 
-
     static void main(args) {
         new PrimitiveTypesTest().testPrimitiveTypes()
     }
diff --git a/src/test/groovy/bugs/Groovy4078Bug.groovy b/src/test/groovy/bugs/Groovy4078Bug.groovy
index 1f755d5..9bf88c5 100644
--- a/src/test/groovy/bugs/Groovy4078Bug.groovy
+++ b/src/test/groovy/bugs/Groovy4078Bug.groovy
@@ -1,49 +1,49 @@
-/*
- *  Licensed to the Apache Software Foundation (ASF) under one
- *  or more contributor license agreements.  See the NOTICE file
- *  distributed with this work for additional information
- *  regarding copyright ownership.  The ASF licenses this file
- *  to you under the Apache License, Version 2.0 (the
- *  "License"); you may not use this file except in compliance
- *  with the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing,
- *  software distributed under the License is distributed on an
- *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- *  KIND, either express or implied.  See the License for the
- *  specific language governing permissions and limitations
- *  under the License.
- */
-package groovy.bugs
-
-class Groovy4078Bug extends GroovyTestCase {
-    void testInfiniteLoopDetectionInStepUsage() {
-        (2..2).step 0, {assert it != null} //IntRange
-        
-        ('b'..'b').step 0, {assert it != null} //ObjectRange
-        
-        5.step( 5, 1 ) { assert it != null } // DGM.step(), int
-        
-        5.0.step (5.0, 1 ) { assert it != null } // DGM.step(), BigDecimal
-        
-        def from = new BigInteger(5)
-        def to = new BigInteger(5)
-        from.step (to, 1 ) { assert it != null }  // DGM.step(), BigInteger
-
-        try{
-            (1..2).step 0, {assert it != null} //IntRange
-            fail('Should have failed as step size 0 causes infinite loop')
-        } catch(ex) {
-            assert ex.message.contains('Infinite loop detected due to step size of 0')
-        }
-                      
-        try{
-            ('a'..'b').step 0, {assert it != null} // ObjectRange
-            fail('Should have failed as step size 0 causes infinite loop')
-        } catch(ex) {
-            assert ex.message.contains('Infinite loop detected due to step size of 0')
-        }
-    }
-}
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+class Groovy4078Bug extends GroovyTestCase {
+    void testInfiniteLoopDetectionInStepUsage() {
+        (2..2).step 0, {assert it != null} //IntRange
+        
+        ('b'..'b').step 0, {assert it != null} //ObjectRange
+        
+        5.step( 5, 1 ) { assert it != null } // DGM.step(), int
+        
+        5.0.step (5.0, 1 ) { assert it != null } // DGM.step(), BigDecimal
+        
+        def from = BigInteger.valueOf(5)
+        def to = BigInteger.valueOf(5)
+        from.step (to, 1 ) { assert it != null }  // DGM.step(), BigInteger
+
+        try{
+            (1..2).step 0, {assert it != null} //IntRange
+            fail('Should have failed as step size 0 causes infinite loop')
+        } catch(ex) {
+            assert ex.message.contains('Infinite loop detected due to step size of 0')
+        }
+                      
+        try{
+            ('a'..'b').step 0, {assert it != null} // ObjectRange
+            fail('Should have failed as step size 0 causes infinite loop')
+        } catch(ex) {
+            assert ex.message.contains('Infinite loop detected due to step size of 0')
+        }
+    }
+}
diff --git a/src/test/groovy/operator/BigIntegerOperationsTest.groovy b/src/test/groovy/operator/BigIntegerOperationsTest.groovy
index 22bffc8..21f79e8 100644
--- a/src/test/groovy/operator/BigIntegerOperationsTest.groovy
+++ b/src/test/groovy/operator/BigIntegerOperationsTest.groovy
@@ -58,7 +58,7 @@ class BigIntegerOperationsTest extends GroovyTestCase {
     }
     
     void testAsOperatorPrecisionLoss() {
-        def value = new BigInteger(Long.MAX_VALUE) + 1
+        def value = BigInteger.valueOf(Long.MAX_VALUE) + 1
         def value2 = value as BigInteger
         assert value == value2
     }
diff --git a/src/test/org/codehaus/groovy/transform/tailrec/TailRecursiveExamples.groovy b/src/test/org/codehaus/groovy/transform/tailrec/TailRecursiveExamples.groovy
index 11d916e..a6f54fd 100644
--- a/src/test/org/codehaus/groovy/transform/tailrec/TailRecursiveExamples.groovy
+++ b/src/test/org/codehaus/groovy/transform/tailrec/TailRecursiveExamples.groovy
@@ -66,8 +66,8 @@ class TailRecursiveExamples {
         assert target.reduce(0, plus, 1, 5, 10) == 16
         assert target.reduce(99, plus, 1, 5, 10, 98) == 213
 
-        def numbersFrom1to1000 = (1..1000).collect { new BigInteger(it) }.toArray()
-        assert target.reduce(new BigInteger(1), { BigInteger a, BigInteger b -> a * b }, numbersFrom1to1000).bitCount() == 3788
+        def numbersFrom1to1000 = (1..1000).collect { BigInteger.valueOf(it) }.toArray()
+        assert target.reduce(BigInteger.ONE, { BigInteger a, BigInteger b -> a * b }, numbersFrom1to1000).bitCount() == 3788
     }
 
     @Test
diff --git a/subprojects/groovy-ant/src/main/java/groovy/util/AntBuilder.java b/subprojects/groovy-ant/src/main/java/groovy/util/AntBuilder.java
index ae176e0..63123d9 100644
--- a/subprojects/groovy-ant/src/main/java/groovy/util/AntBuilder.java
+++ b/subprojects/groovy-ant/src/main/java/groovy/util/AntBuilder.java
@@ -35,6 +35,7 @@ import org.apache.tools.ant.dispatch.DispatchUtils;
 import org.apache.tools.ant.helper.AntXMLContext;
 import org.apache.tools.ant.helper.ProjectHelper2;
 import org.codehaus.groovy.ant.FileScanner;
+import org.codehaus.groovy.reflection.ReflectionUtils;
 import org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport;
 import org.xml.sax.Attributes;
 import org.xml.sax.Locator;
@@ -320,7 +321,7 @@ public class AntBuilder extends BuilderSupport {
         try {
             // Have to call fireTestStared/fireTestFinished via reflection as they unfortunately have protected access in Project
             final Method fireTaskStarted = Project.class.getDeclaredMethod("fireTaskStarted", Task.class);
-            fireTaskStarted.setAccessible(true);
+            ReflectionUtils.trySetAccessible(fireTaskStarted);
             fireTaskStarted.invoke(project, task);
 
             Object realThing;
@@ -354,7 +355,7 @@ public class AntBuilder extends BuilderSupport {
         finally {
             try {
                 final Method fireTaskFinished = Project.class.getDeclaredMethod("fireTaskFinished", Task.class, Throwable.class);
-                fireTaskFinished.setAccessible(true);
+                ReflectionUtils.trySetAccessible(fireTaskFinished);
                 fireTaskFinished.invoke(project, task, reason);
             }
             catch (Exception e) {
diff --git a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java
index 15825f5..f235771 100644
--- a/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java
+++ b/subprojects/groovy-ant/src/main/java/org/codehaus/groovy/ant/Groovy.java
@@ -36,6 +36,7 @@ import org.apache.tools.ant.util.FileUtils;
 import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.control.customizers.ImportCustomizer;
+import org.codehaus.groovy.reflection.ReflectionUtils;
 import org.codehaus.groovy.runtime.InvokerHelper;
 import org.codehaus.groovy.runtime.ResourceGroovyMethods;
 import org.codehaus.groovy.tools.ErrorReporter;
@@ -430,7 +431,7 @@ public class Groovy extends Java {
             try {
                 final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
                 final Field contextField = propsHandler.getClass().getDeclaredField("context");
-                contextField.setAccessible(true);
+                ReflectionUtils.trySetAccessible(contextField);
                 final Object context = contextField.get(propsHandler);
                 mavenPom = InvokerHelper.invokeMethod(context, "getProject", EMPTY_OBJECT_ARRAY);
             }
diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
index 17d0e09..7293b3e 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/util/PackageHelperImpl.groovy
@@ -183,12 +183,14 @@ def filterClassName(Path path) {
     }
 }
 
+class GroovyFileVisitor extends SimpleFileVisitor {}
+
 // walk each file and directory, possibly storing directories as packages, and files as classes
 Files.walkFileTree(fs.getPath('modules'),
         [preVisitDirectory: { dir, attrs -> filterPackageName(dir); FileVisitResult.CONTINUE },
          visitFile: { file, attrs -> filterClassName(file); FileVisitResult.CONTINUE}
         ]
-            as SimpleFileVisitor)
+            as GroovyFileVisitor)
 '''
 
         Set<String> jigsawPackages = (Set<String>) shell.getProperty('result')
diff --git a/subprojects/groovy-swing/src/main/java/org/codehaus/groovy/binding/ClosureTriggerBinding.java b/subprojects/groovy-swing/src/main/java/org/codehaus/groovy/binding/ClosureTriggerBinding.java
index f31dbfb..0069d92 100644
--- a/subprojects/groovy-swing/src/main/java/org/codehaus/groovy/binding/ClosureTriggerBinding.java
+++ b/subprojects/groovy-swing/src/main/java/org/codehaus/groovy/binding/ClosureTriggerBinding.java
@@ -21,6 +21,7 @@ package org.codehaus.groovy.binding;
 import groovy.lang.Closure;
 import groovy.lang.GroovyObjectSupport;
 import groovy.lang.Reference;
+import org.codehaus.groovy.reflection.ReflectionUtils;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
@@ -81,13 +82,13 @@ public class ClosureTriggerBinding implements TriggerBinding, SourceBinding {
                     }
                     try {
                         boolean acc = constructor.isAccessible();
-                        constructor.setAccessible(true);
+                        ReflectionUtils.trySetAccessible(constructor);
                         Closure localCopy = (Closure) constructor.newInstance(args);
                         if (!acc) { constructor.setAccessible(false); }
                         localCopy.setResolveStrategy(Closure.DELEGATE_ONLY);
                         for (Field f:closureClass.getDeclaredFields()) {
                             acc = f.isAccessible();
-                            f.setAccessible(true);
+                            ReflectionUtils.trySetAccessible(f);
                             if (f.getType() == Reference.class) {
                                 delegate.fields.put(f.getName(),
                                         (BindPathSnooper) ((Reference) f.get(localCopy)).get());