This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 3d7931dd256 Add AdviceExecutorFactory (#23399)
3d7931dd256 is described below
commit 3d7931dd2560ac2dc85ba4503826c735081fd9d3
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jan 8 08:00:40 2023 +0800
Add AdviceExecutorFactory (#23399)
* Remove useless MethodAdvisor
* Add AdviceExecutorFactory
* Move AdviceFactory
* Move MethodAdvisorBuilder
* Rename MethodAdvisorBuilderDecorator
---
.../agent/core/builder/AgentTransformer.java | 4 +-
.../builder/MethodAdvisorBuilderDecorator.java | 71 ++++++++++++++++++++++
.../executor/AdviceExecutorFactory.java} | 47 ++++----------
.../advisor => plugin/executor}/AdviceFactory.java | 2 +-
4 files changed, 85 insertions(+), 39 deletions(-)
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
index 7099231d96d..1d8d092bb8f 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/AgentTransformer.java
@@ -26,8 +26,6 @@ import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.utility.JavaModule;
import org.apache.shardingsphere.agent.api.PluginConfiguration;
import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
-import org.apache.shardingsphere.agent.core.builder.advisor.AdviceFactory;
-import
org.apache.shardingsphere.agent.core.builder.advisor.MethodAdvisorBuilder;
import org.apache.shardingsphere.agent.core.classloader.ClassLoaderContext;
import org.apache.shardingsphere.agent.core.plugin.PluginJar;
import
org.apache.shardingsphere.agent.core.plugin.PluginLifecycleServiceManager;
@@ -62,6 +60,6 @@ public final class AgentTransformer implements Transformer {
PluginLifecycleServiceManager.init(pluginConfigs, pluginJars,
classLoaderContext.getAgentClassLoader(), isEnhancedForProxy);
Builder<?> targetAdviceObjectBuilder = builder.defineField(EXTRA_DATA,
Object.class, Opcodes.ACC_PRIVATE |
Opcodes.ACC_VOLATILE).implement(TargetAdviceObject.class).intercept(FieldAccessor.ofField(EXTRA_DATA));
- return new MethodAdvisorBuilder(new AdviceFactory(classLoaderContext),
advisorConfigs.get(typeDescription.getTypeName()),
typeDescription).build(targetAdviceObjectBuilder);
+ return new MethodAdvisorBuilderDecorator(typeDescription,
classLoaderContext,
advisorConfigs.get(typeDescription.getTypeName())).decorate(targetAdviceObjectBuilder);
}
}
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/MethodAdvisorBuilderDecorator.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/MethodAdvisorBuilderDecorator.java
new file mode 100644
index 00000000000..7b752f68dc2
--- /dev/null
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/MethodAdvisorBuilderDecorator.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.agent.core.builder;
+
+import net.bytebuddy.description.method.MethodDescription.InDefinedShape;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.dynamic.DynamicType.Builder;
+import org.apache.shardingsphere.agent.core.classloader.ClassLoaderContext;
+import org.apache.shardingsphere.agent.core.log.LoggerFactory;
+import org.apache.shardingsphere.agent.core.log.LoggerFactory.Logger;
+import
org.apache.shardingsphere.agent.core.plugin.advisor.AdvisorConfiguration;
+import org.apache.shardingsphere.agent.core.plugin.executor.AdviceExecutor;
+import
org.apache.shardingsphere.agent.core.plugin.executor.AdviceExecutorFactory;
+
+import java.util.Optional;
+
+/**
+ * Method advisor builder decorator.
+ */
+public final class MethodAdvisorBuilderDecorator {
+
+ private static final Logger LOGGER =
LoggerFactory.getLogger(MethodAdvisorBuilderDecorator.class);
+
+ private final TypeDescription typePointcut;
+
+ private final AdviceExecutorFactory adviceExecutorFactory;
+
+ public MethodAdvisorBuilderDecorator(final TypeDescription typePointcut,
final ClassLoaderContext classLoaderContext, final AdvisorConfiguration
advisorConfig) {
+ this.typePointcut = typePointcut;
+ adviceExecutorFactory = new AdviceExecutorFactory(classLoaderContext,
advisorConfig);
+ }
+
+ /**
+ * Decorate agent builder with method advisor.
+ *
+ * @param builder to be decorated agent builder
+ * @return decorated agent builder
+ */
+ public Builder<?> decorate(final Builder<?> builder) {
+ Builder<?> result = builder;
+ for (InDefinedShape each : typePointcut.getDeclaredMethods()) {
+ Optional<AdviceExecutor> adviceExecutor =
adviceExecutorFactory.findMatchedAdviceExecutor(each);
+ if (!adviceExecutor.isPresent()) {
+ continue;
+ }
+ try {
+ result = adviceExecutor.get().decorateBuilder(result, each);
+ // CHECKSTYLE:OFF
+ } catch (final Throwable ex) {
+ // CHECKSTYLE:ON
+ LOGGER.error("Failed to load advice class: {}.",
typePointcut.getTypeName(), ex);
+ }
+ }
+ return result;
+ }
+}
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/MethodAdvisorBuilder.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceExecutorFactory.java
similarity index 66%
rename from
agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/MethodAdvisorBuilder.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceExecutorFactory.java
index c30e835d118..99f54c92b90 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/MethodAdvisorBuilder.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceExecutorFactory.java
@@ -15,20 +15,15 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.builder.advisor;
+package org.apache.shardingsphere.agent.core.plugin.executor;
-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.api.advice.AgentAdvice;
import org.apache.shardingsphere.agent.api.advice.type.ConstructorAdvice;
import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
import org.apache.shardingsphere.agent.api.advice.type.StaticMethodAdvice;
-import org.apache.shardingsphere.agent.core.log.LoggerFactory;
-import org.apache.shardingsphere.agent.core.log.LoggerFactory.Logger;
+import org.apache.shardingsphere.agent.core.classloader.ClassLoaderContext;
import
org.apache.shardingsphere.agent.core.plugin.advisor.AdvisorConfiguration;
-import org.apache.shardingsphere.agent.core.plugin.executor.AdviceExecutor;
import
org.apache.shardingsphere.agent.core.plugin.executor.type.ConstructorAdviceExecutor;
import
org.apache.shardingsphere.agent.core.plugin.executor.type.InstanceMethodAdviceExecutor;
import
org.apache.shardingsphere.agent.core.plugin.executor.type.StaticMethodAdviceExecutor;
@@ -38,44 +33,26 @@ import java.util.Optional;
import java.util.stream.Collectors;
/**
- * Method advisor builder.
+ * Advice executor factory.
*/
-@RequiredArgsConstructor
-public final class MethodAdvisorBuilder {
-
- private static final Logger LOGGER =
LoggerFactory.getLogger(MethodAdvisorBuilder.class);
+public final class AdviceExecutorFactory {
private final AdviceFactory adviceFactory;
private final AdvisorConfiguration advisorConfig;
- private final TypeDescription typePointcut;
+ public AdviceExecutorFactory(final ClassLoaderContext classLoaderContext,
final AdvisorConfiguration advisorConfig) {
+ adviceFactory = new AdviceFactory(classLoaderContext);
+ this.advisorConfig = advisorConfig;
+ }
/**
- * Build method advisor builder.
+ * Find matched advice executor.
*
- * @param builder original builder
- * @return built builder
+ * @param methodDescription method description
+ * @return found advice executor
*/
- public Builder<?> build(final Builder<?> builder) {
- Builder<?> result = builder;
- for (InDefinedShape each : typePointcut.getDeclaredMethods()) {
- Optional<AdviceExecutor> adviceExecutor =
findMatchedAdviceExecutor(each);
- if (!adviceExecutor.isPresent()) {
- continue;
- }
- try {
- result = adviceExecutor.get().decorateBuilder(result, each);
- // CHECKSTYLE:OFF
- } catch (final Throwable ex) {
- // CHECKSTYLE:ON
- LOGGER.error("Failed to load advice class: {}.",
typePointcut.getTypeName(), ex);
- }
- }
- return result;
- }
-
- private Optional<AdviceExecutor> findMatchedAdviceExecutor(final
InDefinedShape methodDescription) {
+ public Optional<AdviceExecutor> findMatchedAdviceExecutor(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)) {
diff --git
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/AdviceFactory.java
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceFactory.java
similarity index 97%
rename from
agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/AdviceFactory.java
rename to
agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceFactory.java
index 2f109257ffc..8fa45db844f 100644
---
a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/builder/advisor/AdviceFactory.java
+++
b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/plugin/executor/AdviceFactory.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.core.builder.advisor;
+package org.apache.shardingsphere.agent.core.plugin.executor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;