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(); }
/** * 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; }
/** * 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)); }
final List<? extends Element> allMembers = StaticProcessingEnvironment.getElementUtils().getAllMembers(typeElement); final @Nonnull HashMap<String, Integer> abstractMethodEntries = new HashMap<>(); final @Nonnull Set<String> nonAbstractMethodEntries = new HashSet<>();
/** * 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; }
case DECLARED: final @Nonnull TypeElement typeElement = (TypeElement) ((DeclaredType) typeMirror).asElement(); final @Nonnull String binaryName = StaticProcessingEnvironment.getElementUtils().getBinaryName(typeElement).toString(); try { return Class.forName(binaryName);
/** * 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)); } }