-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: deduplicate methods in JavassistProxy to prevent DuplicateMemberException #1554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,8 +42,10 @@ | |
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Modifier; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
|
|
@@ -154,12 +156,22 @@ private void createMethod(Class<?> interfaceClass, List<String> fieldList, List< | |
| Method[] methodAry = interfaceClass.getMethods(); | ||
| StringBuilder sb = new StringBuilder(512); | ||
| int mi = 0; | ||
| // Track method signatures to skip duplicate methods from multiple parent interfaces. | ||
| // When an interface extends multiple parent interfaces that declare the same method, | ||
| // interfaceClass.getMethods() returns duplicates, causing Javassist to throw | ||
| // DuplicateMemberException when adding the same method twice. | ||
| // See: https://github.com/sofastack/sofa-rpc/issues/1384 | ||
| Set<String> addedMethodSignatures = new HashSet<String>(); | ||
| for (Method m : methodAry) { | ||
| mi++; | ||
| if (Modifier.isNative(m.getModifiers()) || Modifier.isFinal(m.getModifiers()) || | ||
| Modifier.isStatic(m.getModifiers())) { | ||
| continue; | ||
| } | ||
| // Skip duplicate methods inherited from multiple parent interfaces | ||
| if (!addedMethodSignatures.add(buildMethodSignature(m))) { | ||
| continue; | ||
| } | ||
| mi++; | ||
| Class<?>[] mType = m.getParameterTypes(); | ||
| Class<?> returnType = m.getReturnType(); | ||
|
|
||
|
|
@@ -285,6 +297,28 @@ public Invoker getInvoker(Object proxyObject) { | |
| return parseInvoker(proxyObject); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a unique method signature string in the format: methodName(paramType1,paramType2,...) | ||
| * Used to deduplicate methods inherited from multiple parent interfaces that declare the same method. | ||
| * Without deduplication, Javassist throws DuplicateMemberException when the same method is added twice. | ||
| * | ||
| * @param method the method to build a signature for | ||
| * @return the method signature string | ||
| */ | ||
| private String buildMethodSignature(Method method) { | ||
| StringBuilder signature = new StringBuilder(method.getName()); | ||
| signature.append('('); | ||
| Class<?>[] parameterTypes = method.getParameterTypes(); | ||
| for (int i = 0; i < parameterTypes.length; i++) { | ||
| if (i > 0) { | ||
| signature.append(','); | ||
| } | ||
| signature.append(parameterTypes[i].getName()); | ||
| } | ||
|
Comment on lines
+308
to
+317
|
||
| signature.append(')'); | ||
| return signature.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Parse proxy invoker from proxy object | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change adds new behavior (skipping duplicate inherited methods) but there’s no test exercising the failure mode from #1384 (diamond interface inheritance with duplicate method signatures). Since this module already has
JavassistProxyTest, consider adding a test interface that extends two parents declaring the same method and assertgetProxy()succeeds (and the method invocation is routable) to prevent regressions.