protected DirectlyAccessibleDeclaredFieldInformation(@Nonnull VariableElement field, @Nonnull DeclaredType containingType) { super(field, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, field), containingType, field); }
protected NonDirectlyAccessibleDeclaredFieldInformation(@Nonnull VariableElement field, @Nonnull DeclaredType containingType, @Nonnull MethodInformation getter, @Nullable MethodInformation setter) { super(field, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, field), containingType, getter, setter); }
public static @Nonnull NonAccessibleDeclaredFieldInformation of(@Nonnull Element element, @Nonnull DeclaredType containingType) { return new NonAccessibleDeclaredFieldInformation(element, StaticProcessingEnvironment.getTypeUtils().asMemberOf(containingType, element), containingType); }
/** * 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; }
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); }
/** * 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 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; } }
public @Nonnull FiniteIterable<@Nonnull TypeMirror> getThrownTypes() { final @Nonnull ExecutableType executableType = (ExecutableType) StaticProcessingEnvironment.getTypeUtils().asMemberOf(getContainingType(), getElement()); return FiniteIterable.of(executableType.getThrownTypes()); }
/** * Returns all return type annotations of the method as a space-separated string. */ public @Nonnull String getReturnTypeAnnotations(@Nonnull TypeImporter typeImporter) { final @Nonnull TypeMirror typeMirror = StaticProcessingEnvironment.getTypeUtils().asMemberOf(getContainingType(), getElement()); final @Nonnull ExecutableType executableType = (ExecutableType) typeMirror; final @Nonnull TypeMirror returnType = executableType.getReturnType(); final @Nonnull String returnTypeAnnotationsAsString; if (returnType instanceof Type.AnnotatedType) { final Type.@Nonnull AnnotatedType annotatedType = (Type.AnnotatedType) returnType; returnTypeAnnotationsAsString = ProcessingUtility.getAnnotationsAsString(FiniteIterable.of(annotatedType.getAnnotationMirrors()), typeImporter); } else { returnTypeAnnotationsAsString = ""; } return returnTypeAnnotationsAsString; }
/** * Returns true iff a getter method was found in a list of methods of a type for a given field. */ private static boolean hasGetter(@Nonnull String fieldName, @Nonnull TypeMirror fieldType, @Nonnull FiniteIterable<@Nonnull MethodInformation> methodsOfType) { final @Nonnull String nameRegex = "(get|has|is)" + Strings.capitalizeFirstLetters(fieldName); final @Nullable MethodInformation methodInformation = methodsOfType.findFirst(MethodSignatureMatcher.of(nameRegex)); if (methodInformation != null) { @Nullable TypeMirror returnType = methodInformation.getReturnType(); if (StaticProcessingEnvironment.getTypeUtils().isAssignable(returnType, fieldType)) { ProcessingLog.debugging("Found getter for field $: $", fieldName, methodInformation); return true; } else { ProcessingLog.debugging("Found method that looks like a getter, but return type of method is: $, type of field: $ ", returnType, fieldType); return false; } } return methodInformation != null; }
/** * 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)); } }
public @Nullable TypeMirror getReturnType() { if (!hasReturnType()) { return null; } final @Nonnull TypeMirror typeMirror = StaticProcessingEnvironment.getTypeUtils().asMemberOf(getContainingType(), getElement()); Require.that(typeMirror instanceof ExecutableType).orThrow("Expected ExecutableType, but got $", getContainingType()); final @Nonnull ExecutableType executableType = (ExecutableType) typeMirror; return executableType.getReturnType(); }
/** * Returns a matching field for the method parameter, if one exists. This method may only be called once type information is fully initialized. Otherwise, it may accidentally return null even though a field with the same name might exist. */ @Pure public @Nullable FieldInformation getMatchingField() { Require.that(typeInformation.isInitialized()).orThrow("getMatchingField() called before the type information object was fully initialized."); return typeInformation.getFieldInformation().findFirst(field -> field.getName().equals(getName()) && StaticProcessingEnvironment.getTypeUtils().isAssignable(field.getType(), getType())); }
@Impure private void generateGetType() { addAnnotation(Pure.class); addAnnotation(Override.class); beginMethod("public @" + importIfPossible(Nonnull.class) + " Class<" + importIfPossible(StaticProcessingEnvironment.getTypeUtils().erasure(typeInformation.getType())) + "> getType()"); addStatement("return " + importIfPossible(StaticProcessingEnvironment.getTypeUtils().erasure(typeInformation.getType())) + ".class"); endMethod(); }
/** * Returns a matching getter for the method parameter, if one exists. This method may only be called once type information is fully initialized. Otherwise, it may accidentally return null even though a getter with the matching name might exist. */ @Pure public @Nullable MethodInformation getMatchingGetter() { Require.that(typeInformation.isInitialized()).orThrow("getMatchingGetter() called before the type information object was fully initialized."); return typeInformation.getOverriddenMethods().findFirst(method -> method.isGetter() && method.getName().equals("get" + Strings.capitalizeFirstLetters(getName())) && StaticProcessingEnvironment.getTypeUtils().isAssignable(method.getReturnType(), getType())); }
@Impure private void generateGetSupertypeConverterMethod() { addAnnotation(Pure.class); addAnnotation(Override.class); beginMethod("public @" + importIfPossible(Nullable.class) + " " + importIfPossible(Converter.class) + Brackets.inPointy("? super " + importIfPossible(StaticProcessingEnvironment.getTypeUtils().erasure(typeInformation.getType())) + ", " + getExternallyProvidedParameterDeclarationsAsString("")) + " getSupertypeConverter()"); addStatement("return null"); endMethod(); }
@Impure private void generateGetSubtypeConvertersMethod() { addAnnotation(Pure.class); addAnnotation(Override.class); beginMethod("public @" + importIfPossible(Nullable.class) + " " + importIfPossible(ImmutableList.class) + Brackets.inPointy(importIfPossible(Converter.class) + Brackets.inPointy("? extends " + importIfPossible(StaticProcessingEnvironment.getTypeUtils().erasure(typeInformation.getType())) + ", " + getExternallyProvidedParameterDeclarationsAsString(""))) + " getSubtypeConverters()"); addStatement("return null"); endMethod(); }