groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] branch master updated: Code optimization
Date Mon, 28 Oct 2019 08:23:27 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 87e2a92  Code optimization
87e2a92 is described below

commit 87e2a926a38344059adcaba7395e05a2e3fc3283
Author: mattisonchao <mattisonchao@gmail.com>
AuthorDate: Mon Oct 28 14:55:05 2019 +0800

    Code optimization
    
    I found several optimizations while learning this code.
---
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   |  8 +--
 .../org/codehaus/groovy/ast/AnnotationNode.java    |  6 +-
 .../codehaus/groovy/reflection/CachedClass.java    | 66 ++++++++++------------
 .../reflection/stdclasses/CachedSAMClass.java      |  6 +-
 .../groovy/runtime/callsite/CallSiteArray.java     | 14 ++---
 .../runtime/powerassert/AssertionRenderer.java     |  6 +-
 6 files changed, 43 insertions(+), 63 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index b25f451..5a71676 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -238,11 +238,9 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin,
Groovy
     }
 
     public Reduction outputAST(final SourceUnit sourceUnit, final SourceBuffer sourceBuffer)
{
-        AccessController.doPrivileged(new PrivilegedAction() {
-            public Object run() {
-                outputASTInVariousFormsIfNeeded(sourceUnit, sourceBuffer);
-                return null;
-            }
+        AccessController.doPrivileged((PrivilegedAction) () -> {
+            outputASTInVariousFormsIfNeeded(sourceUnit, sourceBuffer);
+            return null;
         });
 
         return null; //new Reduction(Tpken.EOF);
diff --git a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
index 3795d66..cb493cc 100644
--- a/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/AnnotationNode.java
@@ -194,16 +194,16 @@ public class AnnotationNode extends ASTNode {
 
     @Override
     public String getText() {
-        String memberText = "";
+        StringBuilder memberText = new StringBuilder();
         if (members != null) {
             boolean first = true;
             for (Map.Entry<String, Expression> next : members.entrySet()) {
                 if (first) {
                     first = false;
                 } else {
-                    memberText += ", ";
+                    memberText.append(", ");
                 }
-                memberText += next.getKey() + ": " + next.getValue().getText();
+                memberText.append(next.getKey()).append(": ").append(next.getValue().getText());
             }
         }
         return "@" + classNode.getText() + "(" + memberText + ")";
diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
index 48a2886..5e4a66e 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedClass.java
@@ -61,15 +61,13 @@ public class CachedClass {
         private static final long serialVersionUID = 5450437842165410025L;
 
         public CachedField[] initValue() {
-            final Field[] declaredFields = AccessController.doPrivileged(new PrivilegedAction<Field[]>()
{
-                public Field[] run() {
-                    Field[] df = getTheClass().getDeclaredFields();
-                    df = Arrays.stream(df)
-                            .filter(f -> checkCanSetAccessible(f, CachedClass.class))
-                            .toArray(Field[]::new);
+            final Field[] declaredFields = AccessController.doPrivileged((PrivilegedAction<Field[]>)
() -> {
+                Field[] df = getTheClass().getDeclaredFields();
+                df = Arrays.stream(df)
+                        .filter(f -> checkCanSetAccessible(f, CachedClass.class))
+                        .toArray(Field[]::new);
 //                    df = (Field[]) ReflectionUtils.makeAccessible(df);
-                    return df;
-                }
+                return df;
             });
             CachedField[] fields = new CachedField[declaredFields.length];
             for (int i = 0; i != fields.length; ++i)
@@ -83,15 +81,13 @@ public class CachedClass {
 
         public CachedConstructor[] initValue() {
             final Constructor[] declaredConstructors = (Constructor[])
-               AccessController.doPrivileged(new PrivilegedAction<Constructor[]>()
{
-                   public Constructor[] run() {
-                       Constructor[] dc = getTheClass().getDeclaredConstructors();
-                       dc = Arrays.stream(dc)
-                               .filter(c -> checkCanSetAccessible(c, CachedClass.class))
-                               .toArray(Constructor[]::new);
-
-                       return dc;
-                   }
+               AccessController.doPrivileged((PrivilegedAction<Constructor[]>) () ->
{
+                   Constructor[] dc = getTheClass().getDeclaredConstructors();
+                   dc = Arrays.stream(dc)
+                           .filter(c -> checkCanSetAccessible(c, CachedClass.class))
+                           .toArray(Constructor[]::new);
+
+                   return dc;
                });
             CachedConstructor[] constructors = new CachedConstructor[declaredConstructors.length];
             for (int i = 0; i != constructors.length; ++i)
@@ -104,22 +100,22 @@ public class CachedClass {
         private static final long serialVersionUID = 6347586066597418308L;
 
         public CachedMethod[] initValue() {
-            final Method[] 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);
+            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);
 //                           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);
             List<CachedMethod> mopMethods = new ArrayList<CachedMethod>(declaredMethods.length);
             for (int i = 0; i != declaredMethods.length; ++i) {
@@ -173,11 +169,7 @@ public class CachedClass {
 
         public CallSiteClassLoader initValue() {
             return
-               AccessController.doPrivileged(new PrivilegedAction<CallSiteClassLoader>()
{
-                   public CallSiteClassLoader run() {
-                       return new CallSiteClassLoader(CachedClass.this.cachedClass);
-                   }
-               });
+               AccessController.doPrivileged((PrivilegedAction<CallSiteClassLoader>)
() -> new CallSiteClassLoader(CachedClass.this.cachedClass));
         }
     };
 
diff --git a/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java b/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
index 7c3291c..f0f7911 100644
--- a/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
+++ b/src/main/java/org/codehaus/groovy/reflection/stdclasses/CachedSAMClass.java
@@ -103,11 +103,7 @@ public class CachedSAMClass extends CachedClass {
 
     private static Method[] getDeclaredMethods(final Class c) {
         try {
-            Method[] methods = AccessController.doPrivileged(new PrivilegedAction<Method[]>()
{
-                public Method[] run() {
-                    return c.getDeclaredMethods();
-                }
-            });
+            Method[] methods = AccessController.doPrivileged((PrivilegedAction<Method[]>)
() -> c.getDeclaredMethods());
             if (methods!=null) return methods;
         } catch (java.security.AccessControlException ace) {
             // swallow and do as if no method is available
diff --git a/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteArray.java b/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteArray.java
index acbb06f..86005cf 100644
--- a/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteArray.java
+++ b/src/main/java/org/codehaus/groovy/runtime/callsite/CallSiteArray.java
@@ -61,15 +61,13 @@ public final class CallSiteArray {
 
     private static CallSite createCallStaticSite(CallSite callSite, final Class receiver,
Object[] args) {
         CallSite site;
-        AccessController.doPrivileged(new PrivilegedAction<Void>() {
-            public Void run() {
-                try {
-                    Class.forName(receiver.getName(), true, receiver.getClassLoader());
-                } catch (Exception e) {
-                    // force <clinit>
-                }
-                return null;
+        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+            try {
+                Class.forName(receiver.getName(), true, receiver.getClassLoader());
+            } catch (Exception e) {
+                // force <clinit>
             }
+            return null;
         });
         MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
         if (metaClass instanceof MetaClassImpl) {
diff --git a/src/main/java/org/codehaus/groovy/runtime/powerassert/AssertionRenderer.java
b/src/main/java/org/codehaus/groovy/runtime/powerassert/AssertionRenderer.java
index 5c8bd87..5644bec 100644
--- a/src/main/java/org/codehaus/groovy/runtime/powerassert/AssertionRenderer.java
+++ b/src/main/java/org/codehaus/groovy/runtime/powerassert/AssertionRenderer.java
@@ -73,11 +73,7 @@ public final class AssertionRenderer {
         // it's important to use a stable sort here, otherwise
         // renderValues() will skip the wrong values
         Collections.sort(recorder.getValues(),
-                new Comparator<Value>() {
-                    public int compare(Value v1, Value v2) {
-                        return v2.getColumn() - v1.getColumn();
-                    }
-                }
+                (v1, v2) -> v2.getColumn() - v1.getColumn()
         );
     }
 


Mime
View raw message