Pārlūkot izejas kodu

修复多线程调用同一实例list.sort()的bug

kangjie 5 gadi atpakaļ
vecāks
revīzija
9d913e097d

+ 8 - 5
src/main/java/org/ssssssss/magicapi/expression/interpreter/JavaReflection.java

@@ -83,6 +83,14 @@ public class JavaReflection extends AbstractReflection {
 					cachedList.add(method);
 				}
 			}
+			Collection<List<Method>> methodsValues = cachedMethodMap.values();
+			for (List<Method> methodList : methodsValues) {
+				methodList.sort((m1, m2) -> {
+					int sum1 = Arrays.stream(m1.getParameterTypes()).mapToInt(JavaReflection::calcToObjectDistance).sum();
+					int sum2 = Arrays.stream(m2.getParameterTypes()).mapToInt(JavaReflection::calcToObjectDistance).sum();
+					return sum2 - sum1;
+				});
+			}
 		}
 	}
 
@@ -222,11 +230,6 @@ public class JavaReflection extends AbstractReflection {
 	private static Method findMethod (List<Method> methods, Class<?>[] parameterTypes) {
 		Method foundMethod = null;
 		int foundScore = 0;
-		methods.sort((m1, m2) -> {
-			int sum1 = Arrays.stream(m1.getParameterTypes()).mapToInt(JavaReflection::calcToObjectDistance).sum();
-			int sum2 = Arrays.stream(m2.getParameterTypes()).mapToInt(JavaReflection::calcToObjectDistance).sum();
-			return sum2 - sum1;
-		});
 		for (Method method : methods) {
 			// Check if the types match.
 			Class<?>[] otherTypes = method.getParameterTypes();