[groovy] branch GROOVY_3_0_X updated (50e0190 -> 49b7e3e)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[groovy] branch GROOVY_3_0_X updated (50e0190 -> 49b7e3e)

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

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


    from 50e0190  GROOVY-9183: remove unused imports
     new d4ec7f0  GROOVY-9318: add support for ** syntax in static star import white/black lists
     new 51ed9e1  fix style items
     new ebb56e1  Trivial refactoring: extract common variables
     new 49b7e3e  Trivial refactoring: statement lambda can be replaced with expression lambda

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../control/customizers/SecureASTCustomizer.java   |  8 +++--
 .../codehaus/groovy/reflection/CachedClass.java    | 20 +++++-------
 .../groovy/runtime/DefaultGroovyMethods.java       |  6 ++--
 .../customizers/SecureASTCustomizerTest.groovy     | 36 +++++++++++++++++++++-
 4 files changed, 53 insertions(+), 17 deletions(-)

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

[groovy] 01/04: GROOVY-9318: add support for ** syntax in static star import white/black lists

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

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

commit d4ec7f0e336520d6b6c40ddbf690a1d6ff026eac
Author: Martin Grofčík <[hidden email]>
AuthorDate: Mon Dec 2 01:35:09 2019 +0100

    GROOVY-9318: add support for ** syntax in static star import white/black lists
   
    (cherry picked from commit 3b8c172a86184d13b9b06aeb1dcd43803080d857)
---
 .../control/customizers/SecureASTCustomizer.java   |  8 +++--
 .../customizers/SecureASTCustomizerTest.groovy     | 34 ++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
index 572d596..fe4622e 100644
--- a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
+++ b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
@@ -694,7 +694,9 @@ public class SecureASTCustomizer extends CompilationCustomizer {
         if (staticImportsWhitelist != null && !staticImportsWhitelist.contains(fqn)) {
             if (staticStarImportsWhitelist != null) {
                 // we should now check if the import is in the star imports
-                if (!staticStarImportsWhitelist.contains(className + ".*")) {
+                String packageName = className.substring(0, className.lastIndexOf('.') + 1) + "*";
+                if (!staticStarImportsWhitelist.contains(className + ".*") &&
+                    !staticStarImportsWhitelist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
                     throw new SecurityException("Importing [" + fqn + "] is not allowed");
                 }
             } else {
@@ -706,7 +708,9 @@ public class SecureASTCustomizer extends CompilationCustomizer {
         }
         // check that there's no star import blacklist
         if (staticStarImportsBlacklist != null) {
-            if (staticStarImportsBlacklist.contains(className + ".*")) {
+            String packageName = className.substring(0, className.lastIndexOf('.') + 1) + "*";
+            if (staticStarImportsBlacklist.contains(className + ".*") ||
+                staticStarImportsBlacklist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
                 throw new SecurityException("Importing [" + fqn + "] is not allowed");
             }
         }
diff --git a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
index 2218f9b..ad645ae 100644
--- a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
+++ b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
@@ -415,6 +415,40 @@ final class SecureASTCustomizerTest {
     }
 
     @Test
+    void testStaticDoubleStarImportWhiteList() {
+        customizer.staticStarImportsWhitelist = ['java.lang.**']
+        def shell = new GroovyShell(configuration)
+        shell.evaluate('''
+            import static java.lang.Math.PI
+            import static java.lang.Math.cos
+            cos(PI)
+        ''')
+        assert hasSecurityException {
+            shell.evaluate('''
+                import static java.util.Collections.*
+                sort([5,4,2])
+            ''')
+        }
+    }
+
+    @Test
+    void testStaticDoubleStarImportBlackList() {
+        customizer.staticStarImportsBlacklist = ['java.lang.**']
+        def shell = new GroovyShell(configuration)
+        assert hasSecurityException {
+            shell.evaluate('''
+                import static java.lang.Math.PI
+                import static java.lang.Math.cos
+                cos(PI)
+            ''')
+        }
+        shell.evaluate('''
+            import static java.util.Collections.*
+            sort([5,4,2])
+        ''')
+    }
+
+    @Test
     void testIndirectStaticImport() {
         customizer.staticImportsWhitelist = ['java.lang.Math.PI']
         customizer.indirectImportCheckEnabled = true

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

[groovy] 02/04: fix style items

Daniel.Sun
In reply to this post by Daniel.Sun
This is an automated email from the ASF dual-hosted git repository.

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

commit 51ed9e1ce56ad093fd995441f1b780ab3f5bbb4b
Author: Eric Milles <[hidden email]>
AuthorDate: Sun Dec 1 18:40:59 2019 -0600

    fix style items
   
    (cherry picked from commit 78525bb50924ec6a35db665bc4b9053a0c18a3c8)
---
 .../codehaus/groovy/control/customizers/SecureASTCustomizer.java  | 8 ++++----
 .../groovy/control/customizers/SecureASTCustomizerTest.groovy     | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
index fe4622e..0a743ba 100644
--- a/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
+++ b/src/main/java/org/codehaus/groovy/control/customizers/SecureASTCustomizer.java
@@ -695,8 +695,8 @@ public class SecureASTCustomizer extends CompilationCustomizer {
             if (staticStarImportsWhitelist != null) {
                 // we should now check if the import is in the star imports
                 String packageName = className.substring(0, className.lastIndexOf('.') + 1) + "*";
-                if (!staticStarImportsWhitelist.contains(className + ".*") &&
-                    !staticStarImportsWhitelist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
+                if (!staticStarImportsWhitelist.contains(className + ".*")
+                        && !staticStarImportsWhitelist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
                     throw new SecurityException("Importing [" + fqn + "] is not allowed");
                 }
             } else {
@@ -709,8 +709,8 @@ public class SecureASTCustomizer extends CompilationCustomizer {
         // check that there's no star import blacklist
         if (staticStarImportsBlacklist != null) {
             String packageName = className.substring(0, className.lastIndexOf('.') + 1) + "*";
-            if (staticStarImportsBlacklist.contains(className + ".*") ||
-                staticStarImportsBlacklist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
+            if (staticStarImportsBlacklist.contains(className + ".*")
+                    || staticStarImportsBlacklist.stream().filter(it -> it.endsWith(".")).anyMatch(packageName::startsWith)) {
                 throw new SecurityException("Importing [" + fqn + "] is not allowed");
             }
         }
diff --git a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
index ad645ae..6feb2ba 100644
--- a/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
+++ b/src/test/org/codehaus/groovy/control/customizers/SecureASTCustomizerTest.groovy
@@ -398,7 +398,7 @@ final class SecureASTCustomizerTest {
     }
 
     @Test
-    void testStaticStarImportWhiteList() {
+    void testStaticStarImportWhiteList1() {
         customizer.staticStarImportsWhitelist = ['java.lang.Math.*']
         def shell = new GroovyShell(configuration)
         shell.evaluate('''
@@ -415,7 +415,7 @@ final class SecureASTCustomizerTest {
     }
 
     @Test
-    void testStaticDoubleStarImportWhiteList() {
+    void testStaticStarImportWhiteList2() {
         customizer.staticStarImportsWhitelist = ['java.lang.**']
         def shell = new GroovyShell(configuration)
         shell.evaluate('''
@@ -432,7 +432,7 @@ final class SecureASTCustomizerTest {
     }
 
     @Test
-    void testStaticDoubleStarImportBlackList() {
+    void testStaticStarImportBlackList1() {
         customizer.staticStarImportsBlacklist = ['java.lang.**']
         def shell = new GroovyShell(configuration)
         assert hasSecurityException {

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

[groovy] 03/04: Trivial refactoring: extract common variables

Daniel.Sun
In reply to this post by Daniel.Sun
This is an automated email from the ASF dual-hosted git repository.

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

commit ebb56e169a900e4daf389413f44b0fced69f9b8b
Author: Daniel Sun <[hidden email]>
AuthorDate: Mon Dec 2 17:41:51 2019 +0800

    Trivial refactoring: extract common variables
   
    (cherry picked from commit 9d53f2a019275e5c34e0aa90ef7f680ab2429e3c)
---
 src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 747570a..ea986f4 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -16620,10 +16620,12 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.6.0
      */
     public static float trunc(Float number, int precision) {
+        final double p = Math.pow(10, precision);
+        final double n = number.doubleValue() * p;
         if (number < 0f) {
-            return (float)(Math.ceil(number.doubleValue()*Math.pow(10,precision))/Math.pow(10,precision));
+            return (float) (Math.ceil(n) / p);
         }
-        return (float)(Math.floor(number.doubleValue()*Math.pow(10,precision))/Math.pow(10,precision));
+        return (float) (Math.floor(n) / p);
     }
 
     /**

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun
Reply | Threaded
Open this post in threaded view
|

[groovy] 04/04: Trivial refactoring: statement lambda can be replaced with expression lambda

Daniel.Sun
In reply to this post by Daniel.Sun
This is an automated email from the ASF dual-hosted git repository.

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

commit 49b7e3e67eda452660cb8842ee28223f0462ac09
Author: Daniel Sun <[hidden email]>
AuthorDate: Mon Dec 2 17:48:59 2019 +0800

    Trivial refactoring: statement lambda can be replaced with expression lambda
   
    (cherry picked from commit 65b9a1c8342e6c30eef7f1da8c6f99785eb4935b)
---
 .../org/codehaus/groovy/reflection/CachedClass.java  | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
index 79a7756..04a3347 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
@@ -56,11 +56,9 @@ public class CachedClass {
 
         @Override
         public CachedField[] initValue() {
-            PrivilegedAction<CachedField[]> action = () -> {
-                return Arrays.stream(getTheClass().getDeclaredFields())
-                    .filter(f -> checkCanSetAccessible(f, CachedClass.class))
-                    .map(CachedField::new).toArray(CachedField[]::new);
-            };
+            PrivilegedAction<CachedField[]> action = () -> Arrays.stream(getTheClass().getDeclaredFields())
+                .filter(f -> checkCanSetAccessible(f, CachedClass.class))
+                .map(CachedField::new).toArray(CachedField[]::new);
             return AccessController.doPrivileged(action);
         }
     };
@@ -70,13 +68,11 @@ public class CachedClass {
 
         @Override
         public CachedConstructor[] initValue() {
-            PrivilegedAction<CachedConstructor[]> action = () -> {
-                return Arrays.stream(getTheClass().getDeclaredConstructors())
-                    .filter(c -> !c.isSynthetic()) // GROOVY-9245: exclude inner class ctors
-                    .filter(c -> checkCanSetAccessible(c, CachedClass.class))
-                    .map(c -> new CachedConstructor(CachedClass.this, c))
-                    .toArray(CachedConstructor[]::new);
-            };
+            PrivilegedAction<CachedConstructor[]> action = () -> Arrays.stream(getTheClass().getDeclaredConstructors())
+                .filter(c -> !c.isSynthetic()) // GROOVY-9245: exclude inner class ctors
+                .filter(c -> checkCanSetAccessible(c, CachedClass.class))
+                .map(c -> new CachedConstructor(CachedClass.this, c))
+                .toArray(CachedConstructor[]::new);
             return AccessController.doPrivileged(action);
         }
     };

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun