You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/12/11 14:52:33 UTC

[shardingsphere] branch master updated: Simplify PluginPointcuts (#22812)

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

zhonghongsheng 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 98ad9898edb Simplify PluginPointcuts (#22812)
98ad9898edb is described below

commit 98ad9898edb80ddd87a90660aed51cfe5aed4cc0
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Dec 11 22:52:27 2022 +0800

    Simplify PluginPointcuts (#22812)
---
 .../agent/pointcut/PluginPointcuts.java              | 20 --------------------
 .../core/definition/InterceptorPointRegistry.java    |  2 +-
 .../agent/core/plugin/AgentPluginLoader.java         | 13 +++++++------
 .../transformer/ShardingSphereTransformerTest.java   | 13 +++++++------
 .../core/plugin/loader/AgentPluginLoaderTest.java    |  9 +++++----
 5 files changed, 20 insertions(+), 37 deletions(-)

diff --git a/agent/api/src/main/java/org/apache/shardingsphere/agent/pointcut/PluginPointcuts.java b/agent/api/src/main/java/org/apache/shardingsphere/agent/pointcut/PluginPointcuts.java
index a83c503fa30..d5e67f45d68 100644
--- a/agent/api/src/main/java/org/apache/shardingsphere/agent/pointcut/PluginPointcuts.java
+++ b/agent/api/src/main/java/org/apache/shardingsphere/agent/pointcut/PluginPointcuts.java
@@ -24,7 +24,6 @@ import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatchers;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 /**
@@ -42,25 +41,6 @@ public final class PluginPointcuts {
     
     private final List<StaticMethodPointcut> staticMethodPointcuts;
     
-    /**
-     * Create plugin interceptor point.
-     *
-     * @return plugin interceptor point
-     */
-    public static PluginPointcuts createDefault() {
-        return new PluginPointcuts("", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
-    }
-    
-    /**
-     * Intercept target class.
-     *
-     * @param targetClassName target class name
-     * @return builder
-     */
-    public static Builder intercept(final String targetClassName) {
-        return new Builder(targetClassName);
-    }
-    
     /**
      * Plugin advice definition configuration builder.
      */
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/definition/InterceptorPointRegistry.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/definition/InterceptorPointRegistry.java
index b90673dbd8e..84a80635560 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/definition/InterceptorPointRegistry.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/definition/InterceptorPointRegistry.java
@@ -39,7 +39,7 @@ public final class InterceptorPointRegistry {
      * @return interceptor point builder
      */
     public Builder getInterceptorPointBuilder(final String targetClassName) {
-        return builders.computeIfAbsent(targetClassName, PluginPointcuts::intercept);
+        return builders.computeIfAbsent(targetClassName, Builder::new);
     }
     
     /**
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
index bfe8cbd02e5..cbdff7bf3a1 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/AgentPluginLoader.java
@@ -23,18 +23,19 @@ import lombok.Setter;
 import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatcher.Junction;
-import org.apache.shardingsphere.agent.pointcut.PluginPointcuts;
 import org.apache.shardingsphere.agent.config.AgentConfiguration;
 import org.apache.shardingsphere.agent.core.common.AgentClassLoader;
 import org.apache.shardingsphere.agent.core.config.path.AgentPathBuilder;
 import org.apache.shardingsphere.agent.core.config.registry.AgentConfigurationRegistry;
 import org.apache.shardingsphere.agent.core.logging.LoggerFactory;
 import org.apache.shardingsphere.agent.core.spi.PluginServiceLoader;
+import org.apache.shardingsphere.agent.pointcut.PluginPointcuts;
 import org.apache.shardingsphere.agent.spi.PluginDefinitionService;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -52,7 +53,7 @@ public final class AgentPluginLoader implements PluginLoader {
     
     private final Collection<PluginJar> pluginJars = new LinkedList<>();
     
-    private Map<String, PluginPointcuts> interceptorPointMap;
+    private Map<String, PluginPointcuts> pointcuts;
     
     @Getter
     @Setter
@@ -85,7 +86,7 @@ public final class AgentPluginLoader implements PluginLoader {
         Collection<String> pluginNames = getPluginNames();
         Map<String, PluginPointcuts> pointMap = new HashMap<>();
         loadPluginDefinitionServices(pluginNames, pointMap, classLoader);
-        interceptorPointMap = ImmutableMap.<String, PluginPointcuts>builder().putAll(pointMap).build();
+        pointcuts = ImmutableMap.<String, PluginPointcuts>builder().putAll(pointMap).build();
     }
     
     private Collection<String> getPluginNames() {
@@ -128,7 +129,7 @@ public final class AgentPluginLoader implements PluginLoader {
             
             @Override
             public boolean matches(final TypeDescription target) {
-                return interceptorPointMap.containsKey(target.getTypeName());
+                return pointcuts.containsKey(target.getTypeName());
             }
             
             @Override
@@ -145,12 +146,12 @@ public final class AgentPluginLoader implements PluginLoader {
     
     @Override
     public boolean containsType(final TypeDescription typeDescription) {
-        return interceptorPointMap.containsKey(typeDescription.getTypeName());
+        return pointcuts.containsKey(typeDescription.getTypeName());
     }
     
     @Override
     public PluginPointcuts loadPluginInterceptorPoint(final TypeDescription typeDescription) {
-        return interceptorPointMap.getOrDefault(typeDescription.getTypeName(), PluginPointcuts.createDefault());
+        return pointcuts.getOrDefault(typeDescription.getTypeName(), new PluginPointcuts("", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
     }
     
     @Override
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformerTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformerTest.java
index 8e28b75cbfd..f2f29157f2f 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformerTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/bytebuddy/transformer/ShardingSphereTransformerTest.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.agent.core.mock.material.RepeatedAdviceMaterial
 import org.apache.shardingsphere.agent.core.plugin.AdviceInstanceLoader;
 import org.apache.shardingsphere.agent.core.common.AgentClassLoader;
 import org.apache.shardingsphere.agent.core.plugin.AgentPluginLoader;
+import org.apache.shardingsphere.agent.pointcut.PluginPointcuts.Builder;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -70,8 +71,8 @@ public final class ShardingSphereTransformerTest {
         objectPool.put(MockConstructorAdvice.class.getTypeName(), new MockConstructorAdvice());
         objectPool.put(MockInstanceMethodAroundAdvice.class.getTypeName(), new MockInstanceMethodAroundAdvice());
         objectPool.put(MockStaticMethodAroundAdvice.class.getTypeName(), new MockStaticMethodAroundAdvice());
-        Map<String, PluginPointcuts> interceptorPointMap = new HashMap<>(2, 1);
-        PluginPointcuts interceptorPoint = PluginPointcuts.intercept("org.apache.shardingsphere.agent.core.mock.material.Material")
+        Map<String, PluginPointcuts> pointcutsMap = new HashMap<>(2, 1);
+        PluginPointcuts pluginPointcuts = new Builder("org.apache.shardingsphere.agent.core.mock.material.Material")
                 .aroundInstanceMethod(ElementMatchers.named("mock"))
                 .implement(MockInstanceMethodAroundAdvice.class.getTypeName())
                 .build()
@@ -82,8 +83,8 @@ public final class ShardingSphereTransformerTest {
                 .implement(MockConstructorAdvice.class.getTypeName())
                 .build()
                 .install();
-        interceptorPointMap.put(interceptorPoint.getTargetClassName(), interceptorPoint);
-        PluginPointcuts interceptorPointInTwice = PluginPointcuts.intercept("org.apache.shardingsphere.agent.core.mock.material.RepeatedAdviceMaterial")
+        pointcutsMap.put(pluginPointcuts.getTargetClassName(), pluginPointcuts);
+        PluginPointcuts pluginPointcutsInTwice = new Builder("org.apache.shardingsphere.agent.core.mock.material.RepeatedAdviceMaterial")
                 .aroundInstanceMethod(ElementMatchers.named("mock"))
                 .implement(MockInstanceMethodAroundAdvice.class.getTypeName())
                 .build()
@@ -91,9 +92,9 @@ public final class ShardingSphereTransformerTest {
                 .implement(MockInstanceMethodAroundRepeatedAdvice.class.getTypeName())
                 .build()
                 .install();
-        interceptorPointMap.put(interceptorPointInTwice.getTargetClassName(), interceptorPointInTwice);
+        pointcutsMap.put(pluginPointcutsInTwice.getTargetClassName(), pluginPointcutsInTwice);
         MemberAccessor accessor = Plugins.getMemberAccessor();
-        accessor.set(PLUGIN_LOADER.getClass().getDeclaredField("interceptorPointMap"), PLUGIN_LOADER, interceptorPointMap);
+        accessor.set(PLUGIN_LOADER.getClass().getDeclaredField("pointcuts"), PLUGIN_LOADER, pointcutsMap);
         byteBuddyAgent = new AgentBuilder.Default().with(new ByteBuddy().with(TypeValidation.ENABLED))
                 .ignore(ElementMatchers.isSynthetic()).or(ElementMatchers.nameStartsWith("org.apache.shardingsphere.agent.")
                         .and(ElementMatchers.not(ElementMatchers.nameStartsWith("org.apache.shardingsphere.agent.core.mock"))))
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoaderTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoaderTest.java
index cf4abfc6e50..6a0e168b100 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoaderTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/plugin/loader/AgentPluginLoaderTest.java
@@ -20,12 +20,13 @@ package org.apache.shardingsphere.agent.core.plugin.loader;
 import net.bytebuddy.description.type.TypeDescription;
 import net.bytebuddy.matcher.ElementMatchers;
 import net.bytebuddy.pool.TypePool;
-import org.apache.shardingsphere.agent.pointcut.PluginPointcuts;
-import org.apache.shardingsphere.agent.core.mock.advice.MockStaticMethodAroundAdvice;
 import org.apache.shardingsphere.agent.core.mock.advice.MockConstructorAdvice;
 import org.apache.shardingsphere.agent.core.mock.advice.MockInstanceMethodAroundAdvice;
+import org.apache.shardingsphere.agent.core.mock.advice.MockStaticMethodAroundAdvice;
 import org.apache.shardingsphere.agent.core.plugin.AdviceInstanceLoader;
 import org.apache.shardingsphere.agent.core.plugin.AgentPluginLoader;
+import org.apache.shardingsphere.agent.pointcut.PluginPointcuts;
+import org.apache.shardingsphere.agent.pointcut.PluginPointcuts.Builder;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -59,7 +60,7 @@ public final class AgentPluginLoaderTest {
         objectPool.put(MockConstructorAdvice.class.getTypeName(), new MockConstructorAdvice());
         objectPool.put(MockInstanceMethodAroundAdvice.class.getTypeName(), new MockInstanceMethodAroundAdvice());
         objectPool.put(MockStaticMethodAroundAdvice.class.getTypeName(), new MockStaticMethodAroundAdvice());
-        PluginPointcuts interceptorPoint = PluginPointcuts.intercept("org.apache.shardingsphere.agent.core.mock.material.Material")
+        PluginPointcuts pluginPointcuts = new Builder("org.apache.shardingsphere.agent.core.mock.material.Material")
                 .aroundInstanceMethod(ElementMatchers.named("mock"))
                 .implement(MockInstanceMethodAroundAdvice.class.getTypeName())
                 .build()
@@ -71,7 +72,7 @@ public final class AgentPluginLoaderTest {
                 .build()
                 .install();
         MemberAccessor accessor = Plugins.getMemberAccessor();
-        accessor.set(PLUGIN_LOADER.getClass().getDeclaredField("interceptorPointMap"), PLUGIN_LOADER, Collections.singletonMap(interceptorPoint.getTargetClassName(), interceptorPoint));
+        accessor.set(PLUGIN_LOADER.getClass().getDeclaredField("pointcuts"), PLUGIN_LOADER, Collections.singletonMap(pluginPointcuts.getTargetClassName(), pluginPointcuts));
     }
     
     @Test