Skip to content

Commit 93e7a0a

Browse files
committed
Merge pull request #1452 from diguage
* pr/1452: Polish "Refactor duplicate code" Refactor duplicate code
2 parents 5a8b8b1 + b83ceab commit 93e7a0a

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.beans.PropertyDescriptor;
2020
import java.io.Serializable;
2121
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.Executable;
2223
import java.lang.reflect.InvocationHandler;
2324
import java.lang.reflect.InvocationTargetException;
2425
import java.lang.reflect.Method;
@@ -28,6 +29,7 @@
2829
import java.lang.reflect.Type;
2930
import java.lang.reflect.TypeVariable;
3031
import java.util.Arrays;
32+
import java.util.Comparator;
3133
import java.util.Set;
3234

3335
import org.springframework.beans.BeanMetadataElement;
@@ -49,6 +51,17 @@
4951
*/
5052
abstract class AutowireUtils {
5153

54+
private static final Comparator<Executable> EXECUTABLE_COMPARATOR = (e1, e2) -> {
55+
boolean p1 = Modifier.isPublic(e1.getModifiers());
56+
boolean p2 = Modifier.isPublic(e2.getModifiers());
57+
if (p1 != p2) {
58+
return (p1 ? -1 : 1);
59+
}
60+
int c1pl = e1.getParameterCount();
61+
int c2pl = e2.getParameterCount();
62+
return Integer.compare(c2pl, c1pl);
63+
};
64+
5265
/**
5366
* Sort the given constructors, preferring public constructors and "greedy" ones with
5467
* a maximum number of arguments. The result will contain public constructors first,
@@ -57,16 +70,7 @@ abstract class AutowireUtils {
5770
* @param constructors the constructor array to sort
5871
*/
5972
public static void sortConstructors(Constructor<?>[] constructors) {
60-
Arrays.sort(constructors, (c1, c2) -> {
61-
boolean p1 = Modifier.isPublic(c1.getModifiers());
62-
boolean p2 = Modifier.isPublic(c2.getModifiers());
63-
if (p1 != p2) {
64-
return (p1 ? -1 : 1);
65-
}
66-
int c1pl = c1.getParameterCount();
67-
int c2pl = c2.getParameterCount();
68-
return (c1pl < c2pl ? 1 : (c1pl > c2pl ? -1 : 0));
69-
});
73+
Arrays.sort(constructors, EXECUTABLE_COMPARATOR);
7074
}
7175

7276
/**
@@ -77,16 +81,7 @@ public static void sortConstructors(Constructor<?>[] constructors) {
7781
* @param factoryMethods the factory method array to sort
7882
*/
7983
public static void sortFactoryMethods(Method[] factoryMethods) {
80-
Arrays.sort(factoryMethods, (fm1, fm2) -> {
81-
boolean p1 = Modifier.isPublic(fm1.getModifiers());
82-
boolean p2 = Modifier.isPublic(fm2.getModifiers());
83-
if (p1 != p2) {
84-
return (p1 ? -1 : 1);
85-
}
86-
int c1pl = fm1.getParameterCount();
87-
int c2pl = fm2.getParameterCount();
88-
return (c1pl < c2pl ? 1 : (c1pl > c2pl ? -1 : 0));
89-
});
84+
Arrays.sort(factoryMethods, EXECUTABLE_COMPARATOR);
9085
}
9186

9287
/**

0 commit comments

Comments
 (0)