@@ -74,59 +74,71 @@ private List<DeclaredType> findChain(Types typeUtils, DeclaredType declaredType)
74
74
75
75
final var result = new ArrayList <DeclaredType >();
76
76
result .add (declaredType );
77
- var superElement = ((TypeElement ) (( DeclaredType ) declaredType ) .asElement ());
77
+ var superElement = ((TypeElement ) declaredType .asElement ());
78
78
var superclass = (DeclaredType ) superElement .getSuperclass ();
79
- boolean interfaceFound = false ;
80
- final var matchingInterfaces =
81
- superElement .getInterfaces ().stream ()
82
- .filter (intface -> typeUtils .isAssignable (intface , interestedClass ))
83
- .map (i -> (DeclaredType ) i )
84
- .collect (Collectors .toList ());
85
- if (!matchingInterfaces .isEmpty ()) {
79
+
80
+ final var matchingInterfaces = getMatchingInterfaces (typeUtils , superElement );
81
+ if (matchingInterfaces .size () > 0 ) {
86
82
result .addAll (matchingInterfaces );
87
- interfaceFound = true ;
83
+ return result ;
88
84
}
89
85
90
86
while (superclass .getKind () != TypeKind .NONE ) {
91
- if (interfaceFound ) {
92
- final var lastFoundInterface = result .get (result .size () - 1 );
93
- final var marchingInterfaces =
94
- ((TypeElement ) lastFoundInterface .asElement ())
95
- .getInterfaces ().stream ()
96
- .filter (intface -> typeUtils .isAssignable (intface , interestedClass ))
97
- .map (i -> (DeclaredType ) i )
98
- .collect (Collectors .toList ());
99
-
100
- if (marchingInterfaces .size () > 0 ) {
101
- result .addAll (marchingInterfaces );
102
- continue ;
103
- } else {
104
- break ;
105
- }
106
- }
107
87
108
88
if (typeUtils .isAssignable (superclass , interestedClass )) {
109
89
result .add (superclass );
110
90
}
111
91
112
92
superElement = (TypeElement ) superclass .asElement ();
113
- final var matchedInterfaces =
114
- superElement .getInterfaces ().stream ()
115
- .filter (intface -> typeUtils .isAssignable (intface , interestedClass ))
116
- .map (i -> (DeclaredType ) i )
117
- .collect (Collectors .toList ());
118
- if (matchedInterfaces .size () > 0 ) {
119
- result .addAll (matchedInterfaces );
120
- interfaceFound = true ;
121
- continue ;
93
+ ArrayList <DeclaredType > ifs = getMatchingInterfaces (typeUtils , superElement );
94
+ if (ifs .size () > 0 ) {
95
+ result .addAll (ifs );
96
+ return result ;
122
97
}
123
98
124
99
if (superElement .getSuperclass ().getKind () == TypeKind .NONE ) {
125
100
break ;
126
101
}
127
102
superclass = (DeclaredType ) superElement .getSuperclass ();
128
103
}
104
+ return result ;
105
+ }
129
106
107
+ private ArrayList <DeclaredType > getMatchingInterfaces (Types typeUtils , TypeElement superElement ) {
108
+ final var result = new ArrayList <DeclaredType >();
109
+
110
+ final var matchedInterfaces =
111
+ superElement .getInterfaces ().stream ()
112
+ .filter (intface -> typeUtils .isAssignable (intface , interestedClass ))
113
+ .map (i -> (DeclaredType ) i )
114
+ .collect (Collectors .toList ());
115
+ if (matchedInterfaces .size () > 0 ) {
116
+ result .addAll (matchedInterfaces );
117
+ final var lastFoundInterface = result .get (result .size () - 1 );
118
+ final var marchingInterfaces = findChainOfInterfaces (typeUtils , lastFoundInterface );
119
+ result .addAll (marchingInterfaces );
120
+ }
121
+ return result ;
122
+ }
123
+
124
+ private List <DeclaredType > findChainOfInterfaces (Types typeUtils , DeclaredType parentInterface ) {
125
+ final var result = new ArrayList <DeclaredType >();
126
+ var matchingInterfaces =
127
+ ((TypeElement ) parentInterface .asElement ())
128
+ .getInterfaces ().stream ()
129
+ .filter (i -> typeUtils .isAssignable (i , interestedClass ))
130
+ .map (i -> (DeclaredType ) i )
131
+ .collect (Collectors .toList ());
132
+ while (matchingInterfaces .size () > 0 ) {
133
+ result .addAll (matchingInterfaces );
134
+ final var lastFoundInterface = matchingInterfaces .get (matchingInterfaces .size () - 1 );
135
+ matchingInterfaces =
136
+ ((TypeElement ) lastFoundInterface .asElement ())
137
+ .getInterfaces ().stream ()
138
+ .filter (i -> typeUtils .isAssignable (i , interestedClass ))
139
+ .map (i -> (DeclaredType ) i )
140
+ .collect (Collectors .toList ());
141
+ }
130
142
return result ;
131
143
}
132
144
}
0 commit comments