]> source.dussan.org Git - aspectj.git/commitdiff
Single Map method can be used
authorLars Grefer <eclipse@larsgrefer.de>
Sat, 15 Aug 2020 14:25:42 +0000 (16:25 +0200)
committerLars Grefer <eclipse@larsgrefer.de>
Sat, 15 Aug 2020 14:36:00 +0000 (16:36 +0200)
Reports common usage patterns of java.util.Map that could be replaced with Java 8 methods: getOrDefault(), computeIfAbsent(), putIfAbsent(), merge(), or replaceAll().

Signed-off-by: Lars Grefer <eclipse@larsgrefer.de>
bcel-builder/src/main/java/org/aspectj/apache/bcel/util/NonCachingClassLoaderRepository.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/ast/AccessForInlineVisitor.java
org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/PushinCollector.java
weaver/src/main/java/org/aspectj/weaver/bcel/BcelTypeMunger.java

index a7689b10878cbc8d5f56f9f54cac9e008b584ec9..f07b8f22ed07cb51c583fedf256bb935d8c6280d 100644 (file)
@@ -211,11 +211,7 @@ public class NonCachingClassLoaderRepository implements Repository {
         */
        public JavaClass findClass(String className) {
                synchronized (loadedClasses) {
-                       if (loadedClasses.containsKey(className)) {
-                               return loadedClasses.get(className);
-                       } else {
-                               return null;
-                       }
+                       return loadedClasses.getOrDefault(className, null);
                }
        }
 
index 68fc6f74f27f1f69c278ab3744033c8199aec3d2..a8be5eee7f8e705f50022b05f87457cee9dfe419 100644 (file)
@@ -172,11 +172,7 @@ public class AccessForInlineVisitor extends ASTVisitor {
                }
 
                // Avoid repeatedly building ResolvedMembers by using info on any done previously in this visitor
-               Map<FieldBinding, ResolvedMember> alreadyResolvedMembers = alreadyProcessedReceivers.get(receiverType);
-               if (alreadyResolvedMembers == null) {
-                       alreadyResolvedMembers = new HashMap<>();
-                       alreadyProcessedReceivers.put(receiverType, alreadyResolvedMembers);
-               }
+               Map<FieldBinding, ResolvedMember> alreadyResolvedMembers = alreadyProcessedReceivers.computeIfAbsent(receiverType, k -> new HashMap<>());
                ResolvedMember m = alreadyResolvedMembers.get(binding);
                if (m == null) {
                        m = world.makeResolvedMember(binding, receiverType);
index 894492bf37868444fc5456817dd40fe6fa7ea0b1..2730b5b58ba320572c0c9bbf6237cac1cc8df8e0 100644 (file)
@@ -253,20 +253,12 @@ public class PushinCollector {
                        // can be null for binary weave (there is no source method)
                        return;
                }
-               List<AbstractMethodDeclaration> amds = newDeclarations.get(sourceType);
-               if (amds == null) {
-                       amds = new ArrayList<>();
-                       newDeclarations.put(sourceType, amds);
-               }
+               List<AbstractMethodDeclaration> amds = newDeclarations.computeIfAbsent(sourceType, k -> new ArrayList<>());
                amds.add(sourceMethod);
        }
 
        public void tagAsMunged(SourceTypeBinding sourceType, String annotationString) {
-               List<String> annos = additionalAnnotations.get(sourceType);
-               if (annos == null) {
-                       annos = new ArrayList<>();
-                       additionalAnnotations.put(sourceType, annos);
-               }
+               List<String> annos = additionalAnnotations.computeIfAbsent(sourceType, k -> new ArrayList<>());
                annos.add(annotationString);
        }
 
@@ -320,11 +312,7 @@ public class PushinCollector {
 
        public void tagAsMunged(SourceTypeBinding sourceType, TypePattern typePattern) {
                if (typePattern instanceof ExactTypePattern) {
-                       List<ExactTypePattern> annos = additionalParents.get(sourceType);
-                       if (annos == null) {
-                               annos = new ArrayList<>();
-                               additionalParents.put(sourceType, annos);
-                       }
+                       List<ExactTypePattern> annos = additionalParents.computeIfAbsent(sourceType, k -> new ArrayList<>());
                        annos.add((ExactTypePattern) typePattern);
                }
        }
index 1fb2812bb69c26e0af0527af99faed26898476d0..62674dedf1d369de946b72bdfade184633b7d983 100644 (file)
@@ -167,11 +167,7 @@ public class BcelTypeMunger extends ConcreteTypeMunger {
                                                }
                                                String tname = target.getName();
                                                String pname = newParent.getName();
-                                               List<String> newparents = declareParentsMap.get(tname);
-                                               if (newparents == null) {
-                                                       newparents = new ArrayList<>();
-                                                       declareParentsMap.put(tname, newparents);
-                                               }
+                                               List<String> newparents = declareParentsMap.computeIfAbsent(tname, k -> new ArrayList<>());
                                                newparents.add(pname);
                                                AsmRelationshipProvider.addRelationship(model, weaver.getLazyClassGen().getType(), munger, declaringAspect);
                                        }