This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new ec198d4428 fix(sdk): handle Object methods on ShenYu client proxies
locally (#7490)
ec198d4428 is described below
commit ec198d442894fd9617947ed73c6c8fe3b206f665
Author: akashchamp <[email protected]>
AuthorDate: Thu Oct 8 07:51:42 2026 +0530
fix(sdk): handle Object methods on ShenYu client proxies locally (#7490)
toString, hashCode and equals on a @ShenyuClient proxy went through
methodHandlerMap and threw ShenyuException, so logging the client or
putting it in a hash-based collection failed. Answer them in the
invocation handler instead of looking up a remote method handler.
Fixes #7466
---
.../proxy/ShenyuClientInvocationHandler.java | 21 +++++-
.../proxy/ShenyuClientObjectMethodsTest.java | 84 ++++++++++++++++++++++
2 files changed, 104 insertions(+), 1 deletion(-)
diff --git
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
index 96daaee7fa..27b33c324d 100644
---
a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
+++
b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
@@ -54,6 +54,8 @@ public class ShenyuClientInvocationHandler implements
InvocationHandler {
private final Map<Method, ShenyuClientMethodHandler> methodHandlerMap =
new ConcurrentHashMap<>();
+ private final Class<?> apiClass;
+
private final ApplicationContext applicationContext;
private final Contract contract;
@@ -64,6 +66,7 @@ public class ShenyuClientInvocationHandler implements
InvocationHandler {
public ShenyuClientInvocationHandler(final Class<?> apiClass, final
ApplicationContext applicationContext,
final ShenyuClientFactoryBean
shenyuClientFactoryBean) {
+ this.apiClass = apiClass;
this.shenyuClientFactoryBean = shenyuClientFactoryBean;
this.applicationContext = applicationContext;
this.contract = applicationContext.getBean(Contract.class);
@@ -75,7 +78,9 @@ public class ShenyuClientInvocationHandler implements
InvocationHandler {
@Override
public Object invoke(final Object proxy, final Method method, final
Object[] args) throws Throwable {
-
+ if (method.getDeclaringClass() == Object.class) {
+ return invokeObjectMethod(proxy, method, args);
+ }
ShenyuClientMethodHandler handler = methodHandlerMap.get(method);
if (ObjectUtils.isEmpty(handler)) {
throw new ShenyuException(String.format("the method cannot be
called, please check the annotation and configuration, method %s",
method.getName()));
@@ -94,6 +99,20 @@ public class ShenyuClientInvocationHandler implements
InvocationHandler {
return result;
}
+ private Object invokeObjectMethod(final Object proxy, final Method method,
final Object[] args) {
+ switch (method.getName()) {
+ case "equals":
+ return proxy == args[0];
+ case "hashCode":
+ return System.identityHashCode(proxy);
+ case "toString":
+ return String.format("%s(name=%s, url=%s)", apiClass.getName(),
+ shenyuClientFactoryBean.getName(),
shenyuClientFactoryBean.getUrl());
+ default:
+ throw new UnsupportedOperationException(method.toString());
+ }
+ }
+
private void buildMethodHandlerMap(final Class<?> apiClass, final
ShenyuClient shenyuClient) {
// parseAndValidate RequestTemplate
final List<RequestTemplate> requestTemplates =
contract.parseAndValidateRequestTemplate(apiClass, shenyuClientFactoryBean);
diff --git
a/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientObjectMethodsTest.java
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientObjectMethodsTest.java
new file mode 100644
index 0000000000..65bcc3a57f
--- /dev/null
+++
b/shenyu-sdk/shenyu-sdk-spring/src/test/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientObjectMethodsTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.shenyu.sdk.spring.proxy;
+
+import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
+import org.apache.shenyu.sdk.spring.support.SpringMvcContract;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Object methods called on a ShenYu client proxy.
+ */
+public class ShenyuClientObjectMethodsTest {
+
+ private AnnotationConfigApplicationContext context;
+
+ private ShenyuSdkClient sdkClient;
+
+ private AbstractProxyTest.InvocationClient client;
+
+ @BeforeEach
+ public void setUp() {
+ sdkClient = mock(ShenyuSdkClient.class);
+ context = new AnnotationConfigApplicationContext();
+ context.register(AbstractProxyTest.TestConfig.class);
+ context.register(SpringMvcContract.class);
+ context.registerBean("shenyuSdkClient", ShenyuSdkClient.class, () ->
sdkClient);
+ context.refresh();
+ client = context.getBean(AbstractProxyTest.InvocationClient.class);
+ }
+
+ @AfterEach
+ public void tearDown() {
+ context.close();
+ }
+
+ @Test
+ public void testToString() throws IOException {
+ final String value = client.toString();
+
assertTrue(value.contains(AbstractProxyTest.InvocationClient.class.getName()),
value);
+ verify(sdkClient, never()).execute(any());
+ }
+
+ @Test
+ public void testHashCodeAndEquals() throws IOException {
+ assertEquals(client.hashCode(), client.hashCode());
+ assertEquals(client, client);
+ assertNotEquals(client, null);
+ assertNotEquals(client, new Object());
+ final Set<Object> set = new HashSet<>();
+ set.add(client);
+ assertTrue(set.contains(client));
+ verify(sdkClient, never()).execute(any());
+ }
+}