protected DirectlyAccessibleDeclaredFieldInformation(@Nonnull VariableElement field, @Nonnull DeclaredType containingType) { super(field, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, field), containingType, field); }
/** * Returns the given class object as a type mirror. */ @Pure @LogsErrorWhenReturningNull public static @Nullable TypeMirror getTypeMirror(@Nonnull Class<?> type) { if (type.isArray()) { final @Nullable TypeMirror componentType = getTypeMirror(type.getComponentType()); if (componentType == null) { ProcessingLog.error("Could not retrieve a type mirror for the component type of the array $.", type.getCanonicalName()); } else { return StaticProcessingEnvironment.getTypeUtils().getArrayType(componentType); } } else if (type.isPrimitive()) { final @Nullable TypeKind typeKind = primitiveTypes.get(type); if (typeKind == null) { ProcessingLog.error("There is no mapping for the primitive type $.", type.getName()); } else { return StaticProcessingEnvironment.getTypeUtils().getPrimitiveType(typeKind); } } else if (type.isLocalClass()) { ProcessingLog.error("Cannot retrieve a type mirror for the local class $.", type.getName()); } else if (type.isAnonymousClass()) { ProcessingLog.error("Cannot retrieve a type mirror for the anonymous class $.", type.getName()); } else { final @Nullable TypeElement desiredTypeElement = StaticProcessingEnvironment.getElementUtils().getTypeElement(type.getCanonicalName()); if (desiredTypeElement == null) { ProcessingLog.error("Could not retrieve the element for the type $.", type.getCanonicalName()); } else { return desiredTypeElement.asType(); } } return null; }
protected ServiceFileGenerator(@Nonnull Class<?> service) { this.service = service; this.serviceMirror = StaticProcessingEnvironment.getElementUtils().getTypeElement(service.getCanonicalName()).asType(); ProcessingLog.verbose("Generating the service loader file for the service $.", service.getName()); }
/** * Returns the qualified name of the package in which the given element is declared. */ @Pure public static @Nonnull String getQualifiedPackageName(@Nonnull Element element) { return StaticProcessingEnvironment.getElementUtils().getPackageOf(element).getQualifiedName().toString(); }
protected NonDirectlyAccessibleDeclaredFieldInformation(@Nonnull VariableElement field, @Nonnull DeclaredType containingType, @Nonnull MethodInformation getter, @Nullable MethodInformation setter) { super(field, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, field), containingType, getter, setter); }
/** * Adds the given element as a provider for the specified service after performing numerous checks. */ @Impure @NonWrittenRecipient public void addProvider(@Nonnull Element providerElement) { @Nullable String errorMessage = null; if (providerElement.getKind() != ElementKind.CLASS) { errorMessage = "Only a class can implement a service:"; } else if (providerElement.getModifiers().contains(Modifier.ABSTRACT)) { errorMessage = "Only a non-abstract class can implement a service:"; } else if (!ProcessingUtility.hasPublicDefaultConstructor((TypeElement) providerElement)) { errorMessage = "The annotated class does not have a public default constructor:"; } else if (!StaticProcessingEnvironment.getTypeUtils().isSubtype(providerElement.asType(), serviceMirror)) { errorMessage = "The annotated class does not implement the specified service:"; } if (errorMessage == null) { addProvider(StaticProcessingEnvironment.getElementUtils().getBinaryName((TypeElement) providerElement).toString()); } else { ProcessingLog.error(errorMessage, SourcePosition.of(providerElement)); } }
/** * Returns the annotation values mapped from their name for all methods of the given annotation mirror. */ @Pure public static @Nonnull Map<@Nonnull String, @Nonnull AnnotationValue> getAnnotationValues(@Nonnull AnnotationMirror annotationMirror) { final @Nonnull Map<@Nonnull String, @Nonnull AnnotationValue> result = new HashMap<>(); for (Map.@Nonnull Entry<@Nonnull ? extends ExecutableElement, @Nonnull ? extends AnnotationValue> annotationEntry : StaticProcessingEnvironment.getElementUtils().getElementValuesWithDefaults(annotationMirror).entrySet()) { result.put(annotationEntry.getKey().getSimpleName().toString(), annotationEntry.getValue()); } return result; }
public static @Nonnull NonAccessibleDeclaredFieldInformation of(@Nonnull Element element, @Nonnull DeclaredType containingType) { return new NonAccessibleDeclaredFieldInformation(element, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, element), containingType); }
/** * Returns the directly declared and inherited members of the given type element. */ @Pure public static @Nonnull FiniteIterable<@Nonnull Element> getAllMembers(@Nonnull TypeElement typeElement) { return FiniteIterable.of(StaticProcessingEnvironment.getElementUtils().getAllMembers(typeElement)); }
/** * Returns whether the given declared type is a raw subtype of the given desired type. */ @Pure public static boolean isRawSubtype(@Nonnull TypeMirror declaredType, @Nonnull Class<?> desiredType) { final @Nullable TypeMirror typeMirror = getErasedTypeMirror(desiredType); return typeMirror != null ? StaticProcessingEnvironment.getTypeUtils().isSubtype(declaredType, typeMirror) : false; }
final List<? extends Element> allMembers = StaticProcessingEnvironment.getElementUtils().getAllMembers(typeElement); final @Nonnull HashMap<String, Integer> abstractMethodEntries = new HashMap<>(); final @Nonnull Set<String> nonAbstractMethodEntries = new HashSet<>();
protected ExecutableInformation(@Nonnull Element element, @Nonnull DeclaredType containingType, @Nonnull TypeInformation typeInformation) { super(element, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, element), containingType); final @Nonnull List<MethodParameterInformation> parameters = new ArrayList<>(getElement().getParameters().size()); for (@Nonnull VariableElement variableElement : getElement().getParameters()) { parameters.add(new MethodParameterInformation(variableElement, getContainingType(), typeInformation)); } this.parameters = FiniteIterable.of(parameters); }
case DECLARED: final @Nonnull TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement(); final @Nonnull String binaryName = StaticProcessingEnvironment.getElementUtils().getBinaryName(typeElement).toString(); try { return Class.forName(binaryName);
/** * Returns whether the given declared type is rawly assignable to the given desired type. */ @Pure public static boolean isRawlyAssignable(@Nonnull TypeMirror declaredType, @Nonnull Class<?> desiredType) { final @Nullable TypeMirror typeMirror = getErasedTypeMirror(desiredType); return typeMirror != null ? StaticProcessingEnvironment.getTypeUtils().isAssignable(declaredType, typeMirror) : false; }
/** * Returns whether the given surrounding type has a (static) method (with the given suffix) to check the validity of an instance of the given element type. */ @Pure protected static boolean hasMethodToCheckValidity(@Nonnull DeclaredType declaredType, @Nonnull TypeMirror elementType, boolean onlyStatic, @Nonnull String suffix) { for (@Nonnull ExecutableElement inheritedMethod : ProcessingUtility.getAllMethods((TypeElement) declaredType.asElement())) { if (inheritedMethod.getThrownTypes().isEmpty() && inheritedMethod.getSimpleName().contentEquals("isValid" + suffix) && !inheritedMethod.getModifiers().contains(Modifier.PRIVATE) && (!onlyStatic || inheritedMethod.getModifiers().contains(Modifier.STATIC)) && inheritedMethod.getReturnType().getKind() == TypeKind.BOOLEAN) { final @Nonnull ExecutableType methodType = (ExecutableType) StaticProcessingEnvironment.getTypeUtils().asMemberOf(declaredType, inheritedMethod); if (methodType.getParameterTypes().size() == 1 && StaticProcessingEnvironment.getTypeUtils().isAssignable(elementType, methodType.getParameterTypes().get(0))) { return true; } } } return false; }
/** * Returns the given class object as an erased type mirror. */ @Pure @LogsErrorWhenReturningNull public static @Nullable TypeMirror getErasedTypeMirror(@Nonnull Class<?> type) { final @Nullable TypeMirror typeMirror = getTypeMirror(type); return typeMirror != null ? StaticProcessingEnvironment.getTypeUtils().erasure(typeMirror) : null; }
/** * 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; } }
protected GeneratedFieldInformation(@Nonnull DeclaredType containingType, @Nonnull MethodInformation getter, @Nullable MethodInformation setter) { super(getter.getElement(), ((ExecutableType) StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, getter.getElement())).getReturnType(), containingType, getter, setter); }
/** * Returns the boxed type of the given type mirror or its component type in case of arrays. */ @Pure public static @Nonnull TypeMirror getBoxedType(@Nonnull TypeMirror typeMirror) { if (isPrimitive(typeMirror)) { return getType(StaticProcessingEnvironment.getTypeUtils().boxedClass((PrimitiveType) typeMirror)); } else { return typeMirror; } }
/** * 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; }