groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 12/23: Java 8 refactor
Date Fri, 11 Oct 2019 13:54:25 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 2bc96c112fa85141f2c517b6668a460578551b4c
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Fri Oct 11 00:02:56 2019 -0500

    Java 8 refactor
    
    (cherry picked from commit 6e70afb86079b9650fd6aec1c57418818e3acdfc)
---
 .../java/org/codehaus/groovy/ast/ClassHelper.java  | 12 ++--
 .../java/org/codehaus/groovy/ast/ClassNode.java    | 75 +++++++---------------
 .../groovy/classgen/ClassCompletionVerifier.java   |  9 +--
 .../org/codehaus/groovy/classgen/Verifier.java     |  4 +-
 4 files changed, 31 insertions(+), 69 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
index d1d8790..b5b5170 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassHelper.java
@@ -444,15 +444,11 @@ public class ClassHelper {
             return found;
 
         } else {
-
-            List<MethodNode> methods = type.getAbstractMethods();
             MethodNode found = null;
-            if (methods != null) {
-                for (MethodNode mi : methods) {
-                    if (!hasUsableImplementation(type, mi)) {
-                        if (found != null) return null;
-                        found = mi;
-                    }
+            for (MethodNode mi : type.getAbstractMethods()) {
+                if (!hasUsableImplementation(type, mi)) {
+                    if (found != null) return null;
+                    found = mi;
                 }
             }
             return found;
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassNode.java b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
index 8a0c249..4e6ea90 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
@@ -45,7 +45,9 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import static java.util.Arrays.stream;
 import static java.util.stream.Collectors.joining;
@@ -109,32 +111,19 @@ import static java.util.stream.Collectors.joining;
 public class ClassNode extends AnnotatedNode implements Opcodes {
 
     private static class MapOfLists {
-        private Map<Object, List<MethodNode>> map;
+        Map<Object, List<MethodNode>> map;
 
-        public List<MethodNode> get(Object key) {
-            return map == null ? null : map.get(key);
+        List<MethodNode> get(Object key) {
+            return Optional.ofNullable(map)
+                .map(m -> m.get(key)).orElseGet(Collections::emptyList);
         }
 
-        public List<MethodNode> getNotNull(Object key) {
-            List<MethodNode> ret = get(key);
-            if (ret == null) ret = Collections.emptyList();
-            return ret;
+        void put(Object key, MethodNode value) {
+            if (map == null) map = new LinkedHashMap<>();
+            map.computeIfAbsent(key, k -> new ArrayList<>(2)).add(value);
         }
 
-        public void put(Object key, MethodNode value) {
-            if (map == null) {
-                 map = new LinkedHashMap<Object, List<MethodNode>>();
-            }
-            if (map.containsKey(key)) {
-                get(key).add(value);
-            } else {
-                List<MethodNode> list = new ArrayList<MethodNode>(2);
-                list.add(value);
-                map.put(key, list);
-            }
-        }
-
-        public void remove(Object key, MethodNode value) {
+        void remove(Object key, MethodNode value) {
             get(key).remove(value);
         }
     }
@@ -268,8 +257,7 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
         clazz = c;
         lazyInitDone = false;
         isPrimaryNode = false;
-        CompileUnit cu = getCompileUnit();
-        if (cu != null) cu.addClass(this);
+        Optional.ofNullable(getCompileUnit()).ifPresent(cu -> cu.addClass(this));
     }
 
     /**
@@ -405,27 +393,18 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
      * @return the methods associated with this {@code ClassNode}
      */
     public List<MethodNode> getMethods() {
-        if (redirect != null) return redirect().getMethods();
+        if (redirect != null)
+            return redirect().getMethods();
         lazyClassInit();
         return methodsList;
     }
 
     /**
-     * @return the abstract methods associated with this ClassNode or {@code null} if there
are no such methods
+     * @return the abstract methods associated with this {@code ClassNode}
      */
     public List<MethodNode> getAbstractMethods() {
-        List<MethodNode> result = new ArrayList<>(3);
-        for (MethodNode method : getDeclaredMethodsMap().values()) {
-            if (method.isAbstract()) {
-                result.add(method);
-            }
-        }
-
-        if (result.isEmpty()) {
-            return null;
-        } else {
-            return result;
-        }
+        return getDeclaredMethodsMap().values().stream()
+            .filter(MethodNode::isAbstract).collect(Collectors.toList());
     }
 
     public List<MethodNode> getAllDeclaredMethods() {
@@ -517,7 +496,7 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
     }
 
     public PackageNode getPackage() {
-        return getModule() == null ? null : getModule().getPackage();
+        return Optional.ofNullable(getModule()).map(ModuleNode::getPackage).orElse(null);
     }
 
     public void setModule(ModuleNode module) {
@@ -589,14 +568,11 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
     }
 
     public boolean hasProperty(String name) {
-        return getProperty(name) != null;
+        return getProperties().stream().map(PropertyNode::getName).anyMatch(name::equals);
     }
 
     public PropertyNode getProperty(String name) {
-        for (PropertyNode pn : getProperties()) {
-            if (pn.getName().equals(name)) return pn;
-        }
-        return null;
+        return getProperties().stream().filter(pn -> pn.getName().equals(name)).findFirst().orElse(null);
     }
 
     public void addConstructor(ConstructorNode node) {
@@ -886,7 +862,7 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
     public List<MethodNode> getDeclaredMethods(String name) {
         if (redirect != null) return redirect().getDeclaredMethods(name);
         lazyClassInit();
-        return methods.getNotNull(name);
+        return methods.get(name);
     }
 
     /**
@@ -1505,14 +1481,11 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
 
     public void addTransform(Class<? extends ASTTransformation> transform, ASTNode
node) {
         GroovyASTTransformation annotation = transform.getAnnotation(GroovyASTTransformation.class);
-        if (annotation == null) return;
-
-        Set<ASTNode> nodes = getTransformInstances().get(annotation.phase()).get(transform);
-        if (nodes == null) {
-            nodes = new LinkedHashSet<>();
-            getTransformInstances().get(annotation.phase()).put(transform, nodes);
+        if (annotation != null) {
+            Map<Class<? extends ASTTransformation>, Set<ASTNode>> transforms
= getTransforms(annotation.phase());
+            Set<ASTNode> nodes = transforms.computeIfAbsent(transform, k -> new
LinkedHashSet<>());
+            nodes.add(node);
         }
-        nodes.add(node);
     }
 
     public Map<Class <? extends ASTTransformation>, Set<ASTNode>> getTransforms(CompilePhase
phase) {
diff --git a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
index d380d7d..4102c28 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ClassCompletionVerifier.java
@@ -188,10 +188,7 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport
{
         // we only do check abstract classes (including enums), no interfaces or non-abstract
classes
         if (!isAbstract(node.getModifiers()) || isInterface(node.getModifiers())) return;
 
-        List<MethodNode> abstractMethods = node.getAbstractMethods();
-        if (abstractMethods == null || abstractMethods.isEmpty()) return;
-
-        for (MethodNode method : abstractMethods) {
+        for (MethodNode method : node.getAbstractMethods()) {
             if (method.isPrivate()) {
                 addError("Method '" + method.getName() + "' from " + getDescription(node)
+
                         " must not be private as it is declared as an abstract method.",
method);
@@ -201,9 +198,7 @@ public class ClassCompletionVerifier extends ClassCodeVisitorSupport {
 
     private void checkNoAbstractMethodsNonabstractClass(ClassNode node) {
         if (isAbstract(node.getModifiers())) return;
-        List<MethodNode> abstractMethods = node.getAbstractMethods();
-        if (abstractMethods == null) return;
-        for (MethodNode method : abstractMethods) {
+        for (MethodNode method : node.getAbstractMethods()) {
             MethodNode sameArgsMethod = node.getMethod(method.getName(), method.getParameters());
             if (null == sameArgsMethod) {
                 sameArgsMethod = ClassHelper.GROOVY_OBJECT_TYPE.getMethod(method.getName(),
method.getParameters());
diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index c379462..bb0402e 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -733,12 +733,10 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
         classNode.addMethod(method);
         markAsGenerated(classNode, method);
         // GROOVY-4415 / GROOVY-4645: check that there's no abstract method which corresponds
to this one
-        List<MethodNode> abstractMethods = classNode.getAbstractMethods();
-        if (abstractMethods == null) return;
         String methodName = method.getName();
         Parameter[] parameters = method.getParameters();
         ClassNode methodReturnType = method.getReturnType();
-        for (MethodNode node : abstractMethods) {
+        for (MethodNode node : classNode.getAbstractMethods()) {
             if (!node.getDeclaringClass().equals(classNode)) continue;
             if (node.getName().equals(methodName) && node.getParameters().length
== parameters.length) {
                 if (parameters.length == 1) {


Mime
View raw message