/** * Returns whether the type of the given element corresponds to the given type. */ @Pure public static boolean correspond(@Nonnull Element element, @Nonnull Class<?> type) { return correspond(getType(element), type); }
/** * Returns a list of all the fields with the given type in the given type element. */ @Pure public static @Nonnull FiniteIterable<@Nonnull VariableElement> getFieldsOfType(@Nonnull TypeElement typeElement, @Nonnull Class<?> fieldType) { return getFields(typeElement).filter(field -> correspond(field, fieldType)); }
/** * Returns the supertype of the given source type that corresponds to the given target type or null (without logged errors) if not found. */ @Pure public static @Nullable DeclaredType getSupertype(@Nonnull DeclaredType sourceType, @Nonnull Class<?> targetType) { if (correspond(sourceType, targetType)) { return sourceType; } else { for (@Nonnull TypeMirror supertype : StaticProcessingEnvironment.getTypeUtils().directSupertypes(sourceType)) { final @Nullable DeclaredType result = getSupertype((DeclaredType) supertype, targetType); if (result != null) { return result; } } return null; } }
/** * Returns the non-private method with the given name, return type, parameter types and no thrown types in the given type element or null if no such method is found. */ @Pure public static @Nullable ExecutableElement getNonPrivateMethod(@Nonnull TypeElement typeElement, @Nonnull String methodName, @Nonnull Class<?> returnType, @Nonnull @NonNullableElements Class<?>... parameterTypes) { final @Nonnull DeclaredType surroundingType = (DeclaredType) typeElement.asType(); for (@Nonnull ExecutableElement inheritedMethod : getAllMethods(typeElement)) { if (!inheritedMethod.getModifiers().contains(Modifier.PRIVATE) && inheritedMethod.getSimpleName().contentEquals(methodName) && inheritedMethod.getThrownTypes().isEmpty()) { final @Nonnull ExecutableType methodType = (ExecutableType) StaticProcessingEnvironment.getTypeUtils().asMemberOf(surroundingType, inheritedMethod); if (isRawSubtype(methodType.getReturnType(), returnType)) { if (methodType.getParameterTypes().size() == parameterTypes.length) { boolean isAssignable = true; for (int i = 0; i < parameterTypes.length; i++) { final @Nonnull TypeMirror parameterType = methodType.getParameterTypes().get(i); if (parameterType.getKind() == TypeKind.TYPEVAR || !correspond(parameterType, parameterTypes[i])) { isAssignable = false; } } if (isAssignable) { return inheritedMethod; } } } } } return null; }