1919import org .apache .dubbo .common .extension .Activate ;
2020import org .apache .dubbo .common .extension .ExtensionLoader ;
2121import org .apache .dubbo .common .extension .SPI ;
22+ import org .apache .dubbo .common .utils .ArrayUtils ;
2223
24+ import java .util .Arrays ;
2325import java .util .Comparator ;
2426
2527/**
@@ -44,75 +46,92 @@ public int compare(Object o1, Object o2) {
4446 return 0 ;
4547 }
4648
47- // to support com.alibab.dubbo.common.extension.Activate
48- String [] a1Before , a2Before , a1After , a2After ;
49- int a1Order , a2Order ;
50- Class <?> inf = null ;
51- if (o1 .getClass ().getInterfaces ().length > 0 ) {
52- inf = o1 .getClass ().getInterfaces ()[0 ];
49+ Class <?> inf = findSpi (o1 .getClass ());
5350
54- if (inf .getInterfaces ().length > 0 ) {
55- inf = inf .getInterfaces ()[0 ];
56- }
57- }
51+ ActivateInfo a1 = parseActivate (o1 .getClass ());
52+ ActivateInfo a2 = parseActivate (o2 .getClass ());
5853
59- Activate a1 = o1 .getClass ().getAnnotation (Activate .class );
60- if (a1 != null ) {
61- a1Before = a1 .before ();
62- a1After = a1 .after ();
63- a1Order = a1 .order ();
64- } else {
65- com .alibaba .dubbo .common .extension .Activate oa1 = o1 .getClass ().getAnnotation (com .alibaba .dubbo .common .extension .Activate .class );
66- a1Before = oa1 .before ();
67- a1After = oa1 .after ();
68- a1Order = oa1 .order ();
69- }
70- Activate a2 = o2 .getClass ().getAnnotation (Activate .class );
71- if (a2 != null ) {
72- a2Before = a2 .before ();
73- a2After = a2 .after ();
74- a2Order = a2 .order ();
75- } else {
76- com .alibaba .dubbo .common .extension .Activate oa2 = o2 .getClass ().getAnnotation (com .alibaba .dubbo .common .extension .Activate .class );
77- a2Before = oa2 .before ();
78- a2After = oa2 .after ();
79- a2Order = oa2 .order ();
80- }
81- if ((a1Before .length > 0 || a1After .length > 0
82- || a2Before .length > 0 || a2After .length > 0 )
83- && inf != null && inf .isAnnotationPresent (SPI .class )) {
54+ if ((a1 .applicableToCompare () || a2 .applicableToCompare ()) && inf != null ) {
8455 ExtensionLoader <?> extensionLoader = ExtensionLoader .getExtensionLoader (inf );
85- if (a1Before . length > 0 || a1After . length > 0 ) {
56+ if (a1 . applicableToCompare () ) {
8657 String n2 = extensionLoader .getExtensionName (o2 .getClass ());
87- for (String before : a1Before ) {
88- if (before .equals (n2 )) {
89- return -1 ;
90- }
58+ if (a1 .isLess (n2 )) {
59+ return -1 ;
9160 }
92- for (String after : a1After ) {
93- if (after .equals (n2 )) {
94- return 1 ;
95- }
61+
62+ if (a1 .isMore (n2 )) {
63+ return 1 ;
9664 }
9765 }
98- if (a2Before .length > 0 || a2After .length > 0 ) {
66+
67+ if (a2 .applicableToCompare ()) {
9968 String n1 = extensionLoader .getExtensionName (o1 .getClass ());
100- for (String before : a2Before ) {
101- if (before .equals (n1 )) {
102- return 1 ;
103- }
69+ if (a2 .isLess (n1 )) {
70+ return 1 ;
10471 }
105- for (String after : a2After ) {
106- if (after .equals (n1 )) {
107- return -1 ;
108- }
72+
73+ if (a2 .isMore (n1 )) {
74+ return -1 ;
10975 }
11076 }
11177 }
112- int n1 = a1 == null ? 0 : a1Order ;
113- int n2 = a2 == null ? 0 : a2Order ;
78+ int n1 = a1 == null ? 0 : a1 . order ;
79+ int n2 = a2 == null ? 0 : a2 . order ;
11480 // never return 0 even if n1 equals n2, otherwise, o1 and o2 will override each other in collection like HashSet
11581 return n1 > n2 ? 1 : -1 ;
11682 }
11783
84+ private Class <?> findSpi (Class clazz ) {
85+ if (clazz .getInterfaces ().length <= 0 ) {
86+ return null ;
87+ }
88+
89+ for (Class <?> intf : clazz .getInterfaces ()) {
90+ if (intf .isAnnotationPresent (SPI .class )) {
91+ return intf ;
92+ } else {
93+ Class result = findSpi (intf );
94+ if (result != null ) {
95+ return result ;
96+ }
97+ }
98+ }
99+
100+ return null ;
101+ }
102+
103+ private ActivateInfo parseActivate (Class <?> clazz ) {
104+ ActivateInfo info = new ActivateInfo ();
105+ if (clazz .isAnnotationPresent (Activate .class )) {
106+ Activate activate = clazz .getAnnotation (Activate .class );
107+ info .before = activate .before ();
108+ info .after = activate .after ();
109+ info .order = activate .order ();
110+ } else {
111+ com .alibaba .dubbo .common .extension .Activate activate = clazz .getAnnotation (
112+ com .alibaba .dubbo .common .extension .Activate .class );
113+ info .before = activate .before ();
114+ info .after = activate .after ();
115+ info .order = activate .order ();
116+ }
117+ return info ;
118+ }
119+
120+ private static class ActivateInfo {
121+ private String [] before ;
122+ private String [] after ;
123+ private int order ;
124+
125+ private boolean applicableToCompare () {
126+ return ArrayUtils .isNotEmpty (before ) || ArrayUtils .isNotEmpty (after );
127+ }
128+
129+ private boolean isLess (String name ) {
130+ return Arrays .asList (before ).contains (name );
131+ }
132+
133+ private boolean isMore (String name ) {
134+ return Arrays .asList (after ).contains (name );
135+ }
136+ }
118137}
0 commit comments