You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/12/19 09:38:32 UTC

[shardingsphere] branch master updated: Refactor MethodAdvisorBuilder (#22972)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b2f0edf0060 Refactor MethodAdvisorBuilder (#22972)
b2f0edf0060 is described below

commit b2f0edf0060aa7e32e76e2d5fbf8ad406d6e5b82
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Dec 19 17:38:26 2022 +0800

    Refactor MethodAdvisorBuilder (#22972)
---
 .../transformer/builder/MethodAdvisorBuilder.java  | 34 +++++++++-------------
 1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/transformer/builder/MethodAdvisorBuilder.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/transformer/builder/MethodAdvisorBuilder.java
index 27845c3de6f..91e0bce6e8a 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/transformer/builder/MethodAdvisorBuilder.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/transformer/builder/MethodAdvisorBuilder.java
@@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
 import net.bytebuddy.description.method.MethodDescription.InDefinedShape;
 import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.dynamic.DynamicType.Builder;
+import org.apache.shardingsphere.agent.advice.AgentAdvice;
 import org.apache.shardingsphere.agent.advice.type.ConstructorAdvice;
 import org.apache.shardingsphere.agent.advice.type.InstanceMethodAdvice;
 import org.apache.shardingsphere.agent.advice.type.StaticMethodAdvice;
@@ -34,7 +35,7 @@ import org.apache.shardingsphere.agent.core.transformer.MethodAdvisor;
 import org.apache.shardingsphere.agent.core.transformer.builder.advise.AdviceFactory;
 
 import java.util.Collection;
-import java.util.LinkedList;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -72,29 +73,22 @@ public final class MethodAdvisorBuilder {
     }
     
     private Collection<MethodAdvisor> getMatchedMethodAdvisors() {
-        Collection<MethodAdvisor> result = new LinkedList<>();
-        for (InDefinedShape each : typePointcut.getDeclaredMethods()) {
-            result.addAll(getMatchedMethodAdvisors(each));
-        }
-        return result;
+        return typePointcut.getDeclaredMethods().stream().map(this::findMatchedMethodAdvisor).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
     }
     
-    private Collection<MethodAdvisor> getMatchedMethodAdvisors(final InDefinedShape methodDescription) {
-        Collection<MethodAdvisor> result = new LinkedList<>();
+    private Optional<MethodAdvisor> findMatchedMethodAdvisor(final InDefinedShape methodDescription) {
+        Collection<AgentAdvice> advices = advisorConfig.getAdvisors().stream()
+                .filter(each -> each.getPointcut().matches(methodDescription)).map(each -> adviceFactory.getAdvice(each.getAdviceClassName())).collect(Collectors.toList());
         if (isConstructor(methodDescription)) {
-            Collection<ConstructorAdvice> advices = advisorConfig.getAdvisors().stream()
-                    .filter(each -> each.getPointcut().matches(methodDescription)).map(each -> (ConstructorAdvice) adviceFactory.getAdvice(each.getAdviceClassName())).collect(Collectors.toList());
-            result.add(new MethodAdvisor(methodDescription, new ConstructorAdviceExecutor(advices)));
-        } else if (isStaticMethod(methodDescription)) {
-            Collection<StaticMethodAdvice> advices = advisorConfig.getAdvisors().stream()
-                    .filter(each -> each.getPointcut().matches(methodDescription)).map(each -> (StaticMethodAdvice) adviceFactory.getAdvice(each.getAdviceClassName())).collect(Collectors.toList());
-            result.add(new MethodAdvisor(methodDescription, new StaticMethodAdviceExecutor(advices)));
-        } else if (isMethod(methodDescription)) {
-            Collection<InstanceMethodAdvice> advices = advisorConfig.getAdvisors().stream()
-                    .filter(each -> each.getPointcut().matches(methodDescription)).map(each -> (InstanceMethodAdvice) adviceFactory.getAdvice(each.getAdviceClassName())).collect(Collectors.toList());
-            result.add(new MethodAdvisor(methodDescription, new InstanceMethodAdviceExecutor(advices)));
+            return Optional.of(new MethodAdvisor(methodDescription, new ConstructorAdviceExecutor(advices.stream().map(each -> (ConstructorAdvice) each).collect(Collectors.toList()))));
         }
-        return result;
+        if (isStaticMethod(methodDescription)) {
+            return Optional.of(new MethodAdvisor(methodDescription, new StaticMethodAdviceExecutor(advices.stream().map(each -> (StaticMethodAdvice) each).collect(Collectors.toList()))));
+        }
+        if (isMethod(methodDescription)) {
+            return Optional.of(new MethodAdvisor(methodDescription, new InstanceMethodAdviceExecutor(advices.stream().map(each -> (InstanceMethodAdvice) each).collect(Collectors.toList()))));
+        }
+        return Optional.empty();
     }
     
     private boolean isConstructor(final InDefinedShape methodDescription) {