Skip to content

Commit 4982038

Browse files
author
Soroosh Sarabadani
committed
Simplify interface chain selection
1 parent 7767f32 commit 4982038

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

operator-framework/src/main/java/io/javaoperatorsdk/operator/processing/annotation/TypeParameterResolver.java

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,59 +74,71 @@ private List<DeclaredType> findChain(Types typeUtils, DeclaredType declaredType)
7474

7575
final var result = new ArrayList<DeclaredType>();
7676
result.add(declaredType);
77-
var superElement = ((TypeElement) ((DeclaredType) declaredType).asElement());
77+
var superElement = ((TypeElement) declaredType.asElement());
7878
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) {
8682
result.addAll(matchingInterfaces);
87-
interfaceFound = true;
83+
return result;
8884
}
8985

9086
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-
}
10787

10888
if (typeUtils.isAssignable(superclass, interestedClass)) {
10989
result.add(superclass);
11090
}
11191

11292
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;
12297
}
12398

12499
if (superElement.getSuperclass().getKind() == TypeKind.NONE) {
125100
break;
126101
}
127102
superclass = (DeclaredType) superElement.getSuperclass();
128103
}
104+
return result;
105+
}
129106

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+
}
130142
return result;
131143
}
132144
}

0 commit comments

Comments
 (0)