groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 07/09: java 8 refacotor
Date Fri, 01 Nov 2019 16:45:45 GMT
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 32a198bf487c94be0e2c4a0fb73a006411ebe818
Author: mattisonchao <mattisonchao@gmail.com>
AuthorDate: Fri Nov 1 13:34:16 2019 +0800

    java 8 refacotor
    
    I found several code again that can be changed to a lambda expression.
    
    (cherry picked from commit 77c2dd4d4b21df580b04ec6d68054c2b560e5a71)
---
 src/main/java/groovy/lang/GroovyClassLoader.java   | 56 +++++++++-------------
 .../groovy/classgen/asm/ClosureWriter.java         |  6 +--
 .../codehaus/groovy/reflection/CachedClass.java    | 22 ++++-----
 .../org/codehaus/groovy/reflection/ClassInfo.java  |  6 +--
 .../reflection/ClassLoaderForClassArtifacts.java   |  6 +--
 .../groovy/runtime/DefaultGroovyMethods.java       |  8 ++--
 .../groovy/runtime/metaclass/ClosureMetaClass.java | 44 +++++++----------
 7 files changed, 56 insertions(+), 92 deletions(-)

diff --git a/src/main/java/groovy/lang/GroovyClassLoader.java b/src/main/java/groovy/lang/GroovyClassLoader.java
index 726c5d4..59741fb 100644
--- a/src/main/java/groovy/lang/GroovyClassLoader.java
+++ b/src/main/java/groovy/lang/GroovyClassLoader.java
@@ -109,18 +109,16 @@ public class GroovyClassLoader extends URLClassLoader {
 
     private GroovyResourceLoader resourceLoader = new GroovyResourceLoader() {
         public URL loadGroovySource(final String filename) throws MalformedURLException {
-            return AccessController.doPrivileged(new PrivilegedAction<URL>() {
-                public URL run() {
-                    for (String extension : config.getScriptExtensions()) {
-                        try {
-                            URL ret = getSourceFile(filename, extension);
-                            if (ret != null)
-                                return ret;
-                        } catch (Throwable t) { //
-                        }
+            return AccessController.doPrivileged((PrivilegedAction<URL>) () -> {
+                for (String extension : config.getScriptExtensions()) {
+                    try {
+                        URL ret = getSourceFile(filename, extension);
+                        if (ret != null)
+                            return ret;
+                    } catch (Throwable t) { //
                     }
-                    return null;
                 }
+                return null;
             });
         }
     };
@@ -253,11 +251,7 @@ public class GroovyClassLoader extends URLClassLoader {
      * @return the main class defined in the given script
      */
     public Class parseClass(final String text, final String fileName) throws CompilationFailedException
{
-        GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>()
{
-            public GroovyCodeSource run() {
-                return new GroovyCodeSource(text, fileName, "/groovy/script");
-            }
-        });
+        GroovyCodeSource gcs = AccessController.doPrivileged((PrivilegedAction<GroovyCodeSource>)
() -> new GroovyCodeSource(text, fileName, "/groovy/script"));
         gcs.setCachable(false);
         return parseClass(gcs);
     }
@@ -279,14 +273,12 @@ public class GroovyClassLoader extends URLClassLoader {
     }
 
     public Class parseClass(final Reader reader, final String fileName) throws CompilationFailedException
{
-        GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>()
{
-            public GroovyCodeSource run() {
-                try {
-                    String scriptText = IOGroovyMethods.getText(reader);
-                    return new GroovyCodeSource(scriptText, fileName, "/groovy/script");
-                } catch (IOException e) {
-                    throw new RuntimeException("Impossible to read the content of the reader
for file named: " + fileName, e);
-                }
+        GroovyCodeSource gcs = AccessController.doPrivileged((PrivilegedAction<GroovyCodeSource>)
() -> {
+            try {
+                String scriptText = IOGroovyMethods.getText(reader);
+                return new GroovyCodeSource(scriptText, fileName, "/groovy/script");
+            } catch (IOException e) {
+                throw new RuntimeException("Impossible to read the content of the reader
for file named: " + fileName, e);
             }
         });
         return parseClass(gcs);
@@ -301,16 +293,14 @@ public class GroovyClassLoader extends URLClassLoader {
         // For generic input streams, provide a catch-all codebase of GroovyScript
         // Security for these classes can be administered via policy grants with
         // a codebase of file:groovy.script
-        GroovyCodeSource gcs = AccessController.doPrivileged(new PrivilegedAction<GroovyCodeSource>()
{
-            public GroovyCodeSource run() {
-                try {
-                    String scriptText = config.getSourceEncoding() != null ?
-                            IOGroovyMethods.getText(in, config.getSourceEncoding()) :
-                            IOGroovyMethods.getText(in);
-                    return new GroovyCodeSource(scriptText, fileName, "/groovy/script");
-                } catch (IOException e) {
-                    throw new RuntimeException("Impossible to read the content of the input
stream for file named: " + fileName, e);
-                }
+        GroovyCodeSource gcs = AccessController.doPrivileged((PrivilegedAction<GroovyCodeSource>)
() -> {
+            try {
+                String scriptText = config.getSourceEncoding() != null ?
+                        IOGroovyMethods.getText(in, config.getSourceEncoding()) :
+                        IOGroovyMethods.getText(in);
+                return new GroovyCodeSource(scriptText, fileName, "/groovy/script");
+            } catch (IOException e) {
+                throw new RuntimeException("Impossible to read the content of the input stream
for file named: " + fileName, e);
             }
         });
         return parseClass(gcs);
diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
index d9a783b..4b6cfc9 100644
--- a/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
+++ b/src/main/java/org/codehaus/groovy/classgen/asm/ClosureWriter.java
@@ -77,11 +77,7 @@ public class ClosureWriter {
     public ClosureWriter(WriterController wc) {
         this.controller = wc;
         closureClassMap = new HashMap<Expression,ClassNode>();
-        factory = new WriterControllerFactory() {
-            public WriterController makeController(final WriterController normalController)
{
-                return controller;
-            }
-        };
+        factory = normalController -> controller;
     }
 
     public void writeClosure(ClosureExpression expression) {
diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
index 5e4a66e..51c2083 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
@@ -101,19 +101,17 @@ public class CachedClass {
 
         public CachedMethod[] initValue() {
             final Method[] declaredMethods;
-            declaredMethods = AccessController.doPrivileged(new PrivilegedAction<Method[]>()
{
-                public Method[] run() {
-                    try {
-                        Method[] dm = getTheClass().getDeclaredMethods();
-                        dm = Arrays.stream(dm)
-                                .filter(m -> checkCanSetAccessible(m, CachedClass.class))
-                                .toArray(Method[]::new);
+            declaredMethods = AccessController.doPrivileged((PrivilegedAction<Method[]>)
() -> {
+                try {
+                    Method[] dm = getTheClass().getDeclaredMethods();
+                    dm = Arrays.stream(dm)
+                            .filter(m -> checkCanSetAccessible(m, CachedClass.class))
+                            .toArray(Method[]::new);
 //                           dm = (Method[]) ReflectionUtils.makeAccessible(dm);
-                        return dm;
-                    } catch (Throwable e) {
-                        // Typically, Android can throw ClassNotFoundException
-                        return EMPTY_METHOD_ARRAY;
-                    }
+                    return dm;
+                } catch (Throwable e) {
+                    // Typically, Android can throw ClassNotFoundException
+                    return EMPTY_METHOD_ARRAY;
                 }
             });
             List<CachedMethod> methods = new ArrayList<CachedMethod>(declaredMethods.length);
diff --git a/src/main/java/org/codehaus/groovy/reflection/ClassInfo.java b/src/main/java/org/codehaus/groovy/reflection/ClassInfo.java
index 2c4ae9a..c7d1a51 100644
--- a/src/main/java/org/codehaus/groovy/reflection/ClassInfo.java
+++ b/src/main/java/org/codehaus/groovy/reflection/ClassInfo.java
@@ -462,11 +462,7 @@ public class ClassInfo implements Finalizable {
         }
 
         public ClassLoaderForClassArtifacts initValue() {
-            return AccessController.doPrivileged(new PrivilegedAction<ClassLoaderForClassArtifacts>()
{
-                public ClassLoaderForClassArtifacts run() {
-                    return new ClassLoaderForClassArtifacts(info.classRef.get());
-                }
-            });
+            return AccessController.doPrivileged((PrivilegedAction<ClassLoaderForClassArtifacts>)
() -> new ClassLoaderForClassArtifacts(info.classRef.get()));
         }
     }
 
diff --git a/src/main/java/org/codehaus/groovy/reflection/ClassLoaderForClassArtifacts.java
b/src/main/java/org/codehaus/groovy/reflection/ClassLoaderForClassArtifacts.java
index f8f0dfa..5087a84 100644
--- a/src/main/java/org/codehaus/groovy/reflection/ClassLoaderForClassArtifacts.java
+++ b/src/main/java/org/codehaus/groovy/reflection/ClassLoaderForClassArtifacts.java
@@ -75,11 +75,7 @@ public class ClassLoaderForClassArtifacts extends ClassLoader {
     }
 
     public Constructor defineClassAndGetConstructor(final String name, final byte[] bytes)
{
-        final Class cls = AccessController.doPrivileged( new PrivilegedAction<Class>(){
-            public Class run() {
-                return define(name, bytes);
-            }
-        });
+        final Class cls = AccessController.doPrivileged((PrivilegedAction<Class>) ()
-> define(name, bytes));
 
         if (cls != null) {
             try {
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 69ec742..64b3603 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -451,11 +451,9 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
                     if (groovyObject && field.getName().equals("metaClass")) {
                         continue;
                     }
-                    AccessController.doPrivileged(new PrivilegedAction<Object>() {
-                        public Object run() {
-                            ReflectionUtils.trySetAccessible(field);
-                            return null;
-                        }
+                    AccessController.doPrivileged((PrivilegedAction<Object>) () ->
{
+                        ReflectionUtils.trySetAccessible(field);
+                        return null;
                     });
                     buffer.append(" ");
                     buffer.append(field.getName());
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
index bdf8c8d..786a0ba 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
@@ -458,21 +458,17 @@ public final class ClosureMetaClass extends MetaClassImpl {
             int length = c.length;
             if (length == 0) {
                 // no arg method
-                chooser = new MethodChooser() {
-                    public Object chooseMethod(Class[] arguments, boolean coerce) {
-                        if (arguments.length == 0) return doCall;
-                        return null;
-                    }
+                chooser = (arguments, coerce) -> {
+                    if (arguments.length == 0) return doCall;
+                    return null;
                 };
             } else {
                 if (length == 1 && c[0].getTheClass() == Object.class) {
                     // Object fits all, so simple dispatch rule here
-                    chooser = new MethodChooser() {
-                        public Object chooseMethod(Class[] arguments, boolean coerce) {
-                            // <2, because foo() is same as foo(null)
-                            if (arguments.length < 2) return doCall;
-                            return null;
-                        }
+                    chooser = (arguments, coerce) -> {
+                        // <2, because foo() is same as foo(null)
+                        if (arguments.length < 2) return doCall;
+                        return null;
                     };
                 } else {
                     boolean allObject = true;
@@ -484,11 +480,9 @@ public final class ClosureMetaClass extends MetaClassImpl {
                     }
                     if (allObject && c[c.length - 1].getTheClass() == Object.class)
{
                         // all arguments are object, so test only if argument number is correct
-                        chooser = new MethodChooser() {
-                            public Object chooseMethod(Class[] arguments, boolean coerce)
{
-                                if (arguments.length == c.length) return doCall;
-                                return null;
-                            }
+                        chooser = (arguments, coerce) -> {
+                            if (arguments.length == c.length) return doCall;
+                            return null;
                         };
                     } else {
                         if (allObject && c[c.length - 1].getTheClass() == Object[].class)
{
@@ -496,21 +490,17 @@ public final class ClosureMetaClass extends MetaClassImpl {
                             // will fit all, so just test if the number of argument is equal
or
                             // more than the parameters we have.
                             final int minimumLength = c.length - 2;
-                            chooser = new MethodChooser() {
-                                public Object chooseMethod(Class[] arguments, boolean coerce)
{
-                                    if (arguments.length > minimumLength) return doCall;
-                                    return null;
-                                }
+                            chooser = (arguments, coerce) -> {
+                                if (arguments.length > minimumLength) return doCall;
+                                return null;
                             };
                         } else {
                             // general case for single method
-                            chooser = new MethodChooser() {
-                                public Object chooseMethod(Class[] arguments, boolean coerce)
{
-                                    if (doCall.isValidMethod(arguments)) {
-                                        return doCall;
-                                    }
-                                    return null;
+                            chooser = (arguments, coerce) -> {
+                                if (doCall.isValidMethod(arguments)) {
+                                    return doCall;
                                 }
+                                return null;
                             };
                         }
                     }


Mime
View raw message