|
| 1 | +package io.javaoperatorsdk.operator.processing.annotation; |
| 2 | + |
| 3 | +import static javax.lang.model.type.TypeKind.DECLARED; |
| 4 | +import static javax.lang.model.type.TypeKind.TYPEVAR; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.stream.Collectors; |
| 9 | +import java.util.stream.IntStream; |
| 10 | +import javax.lang.model.element.TypeElement; |
| 11 | +import javax.lang.model.element.TypeParameterElement; |
| 12 | +import javax.lang.model.type.DeclaredType; |
| 13 | +import javax.lang.model.type.TypeKind; |
| 14 | +import javax.lang.model.type.TypeMirror; |
| 15 | +import javax.lang.model.type.TypeVariable; |
| 16 | +import javax.lang.model.util.Types; |
| 17 | + |
| 18 | +/** This class can resolve a type parameter in the given index to the actual type defined. */ |
| 19 | +class TypeParameterResolver { |
| 20 | + |
| 21 | + private final DeclaredType interestedClass; |
| 22 | + private final int interestedTypeArgumentIndex; |
| 23 | + |
| 24 | + public TypeParameterResolver(DeclaredType interestedClass, int interestedTypeArgumentIndex) { |
| 25 | + |
| 26 | + this.interestedClass = interestedClass; |
| 27 | + this.interestedTypeArgumentIndex = interestedTypeArgumentIndex; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param typeUtils Type utilities, During the annotation processing processingEnv.getTypeUtils() |
| 32 | + * can be passed. |
| 33 | + * @param declaredType Class or Interface which extends or implements the interestedClass, and the |
| 34 | + * interest is getting the actual declared type is used. |
| 35 | + * @return the type of the parameter if it can be resolved from the the given declareType, |
| 36 | + * otherwise it returns null |
| 37 | + */ |
| 38 | + public TypeMirror resolve(Types typeUtils, DeclaredType declaredType) { |
| 39 | + final var chain = findChain(typeUtils, declaredType); |
| 40 | + var lastIndex = chain.size() - 1; |
| 41 | + String typeName = ""; |
| 42 | + final List<? extends TypeMirror> typeArguments = (chain.get(lastIndex)).getTypeArguments(); |
| 43 | + if (typeArguments.isEmpty()) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + if (typeArguments.get(interestedTypeArgumentIndex).getKind() == TYPEVAR) { |
| 47 | + typeName = |
| 48 | + ((TypeVariable) typeArguments.get(interestedTypeArgumentIndex)) |
| 49 | + .asElement() |
| 50 | + .getSimpleName() |
| 51 | + .toString(); |
| 52 | + } else if (typeArguments.get(interestedTypeArgumentIndex).getKind() == DECLARED) { |
| 53 | + return typeArguments.get(0); |
| 54 | + } |
| 55 | + |
| 56 | + while (lastIndex > 0) { |
| 57 | + lastIndex -= 1; |
| 58 | + final List<? extends TypeMirror> tArguments = (chain.get(lastIndex)).getTypeArguments(); |
| 59 | + final List<? extends TypeParameterElement> typeParameters = |
| 60 | + ((TypeElement) ((chain.get(lastIndex)).asElement())).getTypeParameters(); |
| 61 | + |
| 62 | + final var typeIndex = getTypeIndexWithName(typeName, typeParameters); |
| 63 | + |
| 64 | + final TypeMirror matchedType = tArguments.get(typeIndex); |
| 65 | + if (matchedType.getKind() == TYPEVAR) { |
| 66 | + typeName = ((TypeVariable) matchedType).asElement().getSimpleName().toString(); |
| 67 | + } else if (matchedType.getKind() == DECLARED) { |
| 68 | + return matchedType; |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + private int getTypeIndexWithName( |
| 75 | + String typeName, List<? extends TypeParameterElement> typeParameters) { |
| 76 | + return IntStream.range(0, typeParameters.size()) |
| 77 | + .filter(i -> typeParameters.get(i).getSimpleName().toString().equals(typeName)) |
| 78 | + .findFirst() |
| 79 | + .getAsInt(); |
| 80 | + } |
| 81 | + |
| 82 | + private List<DeclaredType> findChain(Types typeUtils, DeclaredType declaredType) { |
| 83 | + |
| 84 | + final var result = new ArrayList<DeclaredType>(); |
| 85 | + result.add(declaredType); |
| 86 | + var superElement = ((TypeElement) declaredType.asElement()); |
| 87 | + var superclass = (DeclaredType) superElement.getSuperclass(); |
| 88 | + |
| 89 | + final var matchingInterfaces = getMatchingInterfaces(typeUtils, superElement); |
| 90 | + // if chain of interfaces is not empty, there is no reason to continue the lookup |
| 91 | + // as interfaces do not extend the classes |
| 92 | + if (matchingInterfaces.size() > 0) { |
| 93 | + result.addAll(matchingInterfaces); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + while (superclass.getKind() != TypeKind.NONE) { |
| 98 | + |
| 99 | + if (typeUtils.isAssignable(superclass, interestedClass)) { |
| 100 | + result.add(superclass); |
| 101 | + } |
| 102 | + |
| 103 | + superElement = (TypeElement) superclass.asElement(); |
| 104 | + ArrayList<DeclaredType> ifs = getMatchingInterfaces(typeUtils, superElement); |
| 105 | + if (ifs.size() > 0) { |
| 106 | + result.addAll(ifs); |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + if (superElement.getSuperclass().getKind() == TypeKind.NONE) { |
| 111 | + break; |
| 112 | + } |
| 113 | + superclass = (DeclaredType) superElement.getSuperclass(); |
| 114 | + } |
| 115 | + return result; |
| 116 | + } |
| 117 | + |
| 118 | + private ArrayList<DeclaredType> getMatchingInterfaces(Types typeUtils, TypeElement superElement) { |
| 119 | + final var result = new ArrayList<DeclaredType>(); |
| 120 | + |
| 121 | + final var matchedInterfaces = |
| 122 | + superElement.getInterfaces().stream() |
| 123 | + .filter(intface -> typeUtils.isAssignable(intface, interestedClass)) |
| 124 | + .map(i -> (DeclaredType) i) |
| 125 | + .collect(Collectors.toList()); |
| 126 | + if (matchedInterfaces.size() > 0) { |
| 127 | + result.addAll(matchedInterfaces); |
| 128 | + final var lastFoundInterface = result.get(result.size() - 1); |
| 129 | + final var marchingInterfaces = findChainOfInterfaces(typeUtils, lastFoundInterface); |
| 130 | + result.addAll(marchingInterfaces); |
| 131 | + } |
| 132 | + return result; |
| 133 | + } |
| 134 | + |
| 135 | + private List<DeclaredType> findChainOfInterfaces(Types typeUtils, DeclaredType parentInterface) { |
| 136 | + final var result = new ArrayList<DeclaredType>(); |
| 137 | + var matchingInterfaces = |
| 138 | + ((TypeElement) parentInterface.asElement()) |
| 139 | + .getInterfaces().stream() |
| 140 | + .filter(i -> typeUtils.isAssignable(i, interestedClass)) |
| 141 | + .map(i -> (DeclaredType) i) |
| 142 | + .collect(Collectors.toList()); |
| 143 | + while (matchingInterfaces.size() > 0) { |
| 144 | + result.addAll(matchingInterfaces); |
| 145 | + final var lastFoundInterface = matchingInterfaces.get(matchingInterfaces.size() - 1); |
| 146 | + matchingInterfaces = |
| 147 | + ((TypeElement) lastFoundInterface.asElement()) |
| 148 | + .getInterfaces().stream() |
| 149 | + .filter(i -> typeUtils.isAssignable(i, interestedClass)) |
| 150 | + .map(i -> (DeclaredType) i) |
| 151 | + .collect(Collectors.toList()); |
| 152 | + } |
| 153 | + return result; |
| 154 | + } |
| 155 | +} |
0 commit comments