Skip to content
This repository was archived by the owner on Jul 29, 2026. It is now read-only.

Commit f33474b

Browse files
committed
bug fix for MethodInheritanceComparator
1 parent b1b7a68 commit f33474b

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/main/java/com/alibaba/fastjson/util/TypeUtils.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,12 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
19161916
String[] paramNames = null;
19171917
short[] paramNameMapping = null;
19181918
Method[] methods = clazz.getMethods();
1919+
try {
1920+
Arrays.sort(methods, new MethodInheritanceComparator());
1921+
} catch (Throwable ignored) {
1922+
1923+
}
1924+
19191925
for (Method method : methods) {
19201926
String methodName = method.getName();
19211927
int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0;
@@ -3424,4 +3430,29 @@ public static Object optionalEmpty(Type type) {
34243430
}
34253431
return null;
34263432
}
3433+
3434+
public static class MethodInheritanceComparator implements Comparator<Method> {
3435+
public int compare(Method m1, Method m2) {
3436+
int cmp = m1.getName().compareTo(m2.getName());
3437+
if (cmp != 0) {
3438+
return cmp;
3439+
}
3440+
3441+
Class<?> class1 = m1.getReturnType();
3442+
Class<?> class2 = m2.getReturnType();
3443+
3444+
if (class1.equals(class2)) {
3445+
return 0;
3446+
}
3447+
3448+
if (class1.isAssignableFrom(class2)) {
3449+
return -1;
3450+
}
3451+
3452+
if (class2.isAssignableFrom(class1)) {
3453+
return 1;
3454+
}
3455+
return 0;
3456+
}
3457+
}
34273458
}

0 commit comments

Comments
 (0)