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

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 1fdd7fb  fix shenyu agent InstanceMethodInterceptor and 
StaticMethodInterceptor intercept error #2958 (#2961)
1fdd7fb is described below

commit 1fdd7fb9df7a279c303e37ef78041271db48e319
Author: hutaishi <[email protected]>
AuthorDate: Wed Mar 2 13:58:01 2022 +0800

    fix shenyu agent InstanceMethodInterceptor and StaticMethodInterceptor 
intercept error #2958 (#2961)
---
 .../agent/api/handler/StaticMethodHandler.java     |  8 ++--
 .../interceptor/InstanceMethodInterceptor.java     | 55 +++++++++++-----------
 .../interceptor/StaticMethodInterceptor.java       | 50 ++++++++++----------
 3 files changed, 59 insertions(+), 54 deletions(-)

diff --git 
a/shenyu-agent/shenyu-agent-api/src/main/java/org/apache/shenyu/agent/api/handler/StaticMethodHandler.java
 
b/shenyu-agent/shenyu-agent-api/src/main/java/org/apache/shenyu/agent/api/handler/StaticMethodHandler.java
index bde61d7..bf7de70 100644
--- 
a/shenyu-agent/shenyu-agent-api/src/main/java/org/apache/shenyu/agent/api/handler/StaticMethodHandler.java
+++ 
b/shenyu-agent/shenyu-agent-api/src/main/java/org/apache/shenyu/agent/api/handler/StaticMethodHandler.java
@@ -17,10 +17,10 @@
 
 package org.apache.shenyu.agent.api.handler;
 
-import org.apache.shenyu.agent.api.entity.MethodResult;
-
 import java.lang.reflect.Method;
 
+import org.apache.shenyu.agent.api.entity.MethodResult;
+
 /**
  * The interface Class static method handler.
  */
@@ -44,8 +44,10 @@ public interface StaticMethodHandler {
      * @param method the method
      * @param args the args
      * @param result the result
+     * @return method result
      */
-    default void after(final Class<?> clazz, final Method method, final 
Object[] args, final MethodResult result) {
+    default Object after(final Class<?> clazz, final Method method, final 
Object[] args, final MethodResult result) {
+        return result.getResult();
     }
     
     /**
diff --git 
a/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/InstanceMethodInterceptor.java
 
b/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/InstanceMethodInterceptor.java
index deb05a5..2daf8f1 100644
--- 
a/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/InstanceMethodInterceptor.java
+++ 
b/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/InstanceMethodInterceptor.java
@@ -18,20 +18,21 @@
 
 package org.apache.shenyu.agent.core.bytebuddy.interceptor;
 
-import net.bytebuddy.implementation.bind.annotation.AllArguments;
-import net.bytebuddy.implementation.bind.annotation.Origin;
-import net.bytebuddy.implementation.bind.annotation.RuntimeType;
-import net.bytebuddy.implementation.bind.annotation.SuperCall;
-import net.bytebuddy.implementation.bind.annotation.This;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.concurrent.Callable;
+
 import org.apache.shenyu.agent.api.entity.MethodResult;
 import org.apache.shenyu.agent.api.entity.TargetObject;
 import org.apache.shenyu.agent.api.handler.InstanceMethodHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.concurrent.Callable;
+import net.bytebuddy.implementation.bind.annotation.AllArguments;
+import net.bytebuddy.implementation.bind.annotation.Origin;
+import net.bytebuddy.implementation.bind.annotation.RuntimeType;
+import net.bytebuddy.implementation.bind.annotation.SuperCall;
+import net.bytebuddy.implementation.bind.annotation.This;
 
 /**
  * The type Instance method interceptor.
@@ -63,38 +64,38 @@ public class InstanceMethodInterceptor {
      */
     @RuntimeType
     public Object intercept(@This final Object target, @Origin final Method 
method, @AllArguments final Object[] args, @SuperCall final Callable<?> 
callable) throws Exception {
-        Object result = null;
+
         TargetObject instance = (TargetObject) target;
+        MethodResult methodResult = new MethodResult();
         for (InstanceMethodHandler handler : handlerList) {
-            MethodResult methodResult = new MethodResult();
             try {
                 handler.before(instance, method, args, methodResult);
-                // CHECKSTYLE:OFF
             } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
                 LOG.error("Failed to execute the before method of method {} in 
class {}", method.getName(), target.getClass(), ex);
             }
-            try {
-                if (!methodResult.isReset()) {
-                    methodResult.reset(callable.call());
-                }
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
+        }
+        Object result;
+        try {
+            if (!methodResult.isReset()) {
+                result = callable.call();
+                methodResult.reset(result);
+            } else {
+                result = methodResult.getResult();
+            }
+        } catch (final Throwable ex) {
+            for (InstanceMethodHandler handler : handlerList) {
                 try {
                     handler.onThrowing(instance, method, args, ex);
-                    // CHECKSTYLE:OFF
-                } catch (final Throwable ignored) {
-                    // CHECKSTYLE:ON
-                    LOG.error("Failed to execute the error handler of method 
{} in class {}", method.getName(), target.getClass(), ex);
-                    throw ex;
+                } catch (final Throwable handlerEx) {
+                    LOG.error("Failed to execute the error handler of method 
{} in class {}", method.getName(), target.getClass(), handlerEx);
                 }
-            } finally {
+            }
+            throw ex;
+        } finally {
+            for (InstanceMethodHandler handler : handlerList) {
                 try {
                     result = handler.after(instance, method, args, 
methodResult);
-                    // CHECKSTYLE:OFF
                 } catch (final Throwable ex) {
-                    // CHECKSTYLE:ON
                     LOG.error("Failed to execute the after method of method {} 
in class {}", method.getName(), target.getClass(), ex);
                 }
             }
diff --git 
a/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/StaticMethodInterceptor.java
 
b/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/StaticMethodInterceptor.java
index 1b2ecd4..a9a3836 100644
--- 
a/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/StaticMethodInterceptor.java
+++ 
b/shenyu-agent/shenyu-agent-core/src/main/java/org/apache/shenyu/agent/core/bytebuddy/interceptor/StaticMethodInterceptor.java
@@ -18,18 +18,19 @@
 
 package org.apache.shenyu.agent.core.bytebuddy.interceptor;
 
-import net.bytebuddy.implementation.bind.annotation.AllArguments;
-import net.bytebuddy.implementation.bind.annotation.Origin;
-import net.bytebuddy.implementation.bind.annotation.RuntimeType;
-import net.bytebuddy.implementation.bind.annotation.SuperCall;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.concurrent.Callable;
+
 import org.apache.shenyu.agent.api.entity.MethodResult;
 import org.apache.shenyu.agent.api.handler.StaticMethodHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.concurrent.Callable;
+import net.bytebuddy.implementation.bind.annotation.AllArguments;
+import net.bytebuddy.implementation.bind.annotation.Origin;
+import net.bytebuddy.implementation.bind.annotation.RuntimeType;
+import net.bytebuddy.implementation.bind.annotation.SuperCall;
 
 /**
  * The type Static method interceptor.
@@ -61,35 +62,36 @@ public class StaticMethodInterceptor {
      */
     @RuntimeType
     public Object intercept(@Origin final Class<?> klass, @Origin final Method 
method, @AllArguments final Object[] args, @SuperCall final Callable<?> 
callable) throws Exception {
-        Object result = null;
+        MethodResult methodResult = new MethodResult();
         for (StaticMethodHandler handler : handlerList) {
-            MethodResult methodResult = new MethodResult();
             try {
                 handler.before(klass, method, args, methodResult);
-                // CHECKSTYLE:OFF
             } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
                 LOG.error("Failed to execute the before method of method {} in 
class {}", method.getName(), klass, ex);
             }
-            try {
+        }
+        Object result;
+        try {
+            if (!methodResult.isReset()) {
                 result = callable.call();
-                // CHECKSTYLE:OFF
-            } catch (final Throwable ex) {
-                // CHECKSTYLE:ON
+                methodResult.reset(result);
+            } else {
+                result = methodResult.getResult();
+            }
+        } catch (final Throwable ex) {
+            for (StaticMethodHandler handler : handlerList) {
                 try {
                     handler.onThrowing(klass, method, args, ex);
-                    // CHECKSTYLE:OFF
-                } catch (final Throwable ignored) {
-                    // CHECKSTYLE:ON
-                    LOG.error("Failed to execute the error handler of method 
{} in class {}", method.getName(), klass, ex);
-                    throw ex;
+                } catch (final Throwable handlerEx) {
+                    LOG.error("Failed to execute the error handler of method 
{} in class {}", method.getName(), klass, handlerEx);
                 }
-            } finally {
+            }
+            throw ex;
+        } finally {
+            for (StaticMethodHandler handler : handlerList) {
                 try {
-                    handler.after(klass, method, args, methodResult);
-                    // CHECKSTYLE:OFF
+                    result = handler.after(klass, method, args, methodResult);
                 } catch (final Throwable ex) {
-                    // CHECKSTYLE:ON
                     LOG.error("Failed to execute the after method of method {} 
in class {}", method.getName(), klass, ex);
                 }
             }

Reply via email to