/** * Returns the given annotation value as a class object or propagates null. */ @Pure public static @Nullable Class<?> getClass(@Nullable AnnotationValue annotationValue) { if (annotationValue != null) { final @Nonnull Object object = annotationValue.getValue(); if (object instanceof TypeMirror) { return getClass((TypeMirror) object); } else { ProcessingLog.error("The annotation value is not a class: $.", object); } } return null; }
/** * Returns the given annotation value as a new instance of the given type or propagates null. */ @Pure @SuppressWarnings("unchecked") public static <T> @Nullable T getInstance(@Nullable AnnotationValue annotationValue, @Nonnull Class<T> desiredType) { final @Nullable Class<?> declaredType = getClass(annotationValue); if (declaredType != null) { try { final @Nonnull Object object = declaredType.newInstance(); if (desiredType.isInstance(object)) { return (T) object; } else { ProcessingLog.error("$ is not an instance of $.", object, desiredType.getCanonicalName()); } } catch (@Nonnull InstantiationException | IllegalAccessException exception) { ProcessingLog.error("Could not instantiate the class $.", declaredType.getCanonicalName()); } } return null; }
@Pure @Override public void checkUsage(@Nonnull Element element, @Nonnull AnnotationMirror annotationMirror, @NonCaptured @Modified @Nonnull ErrorLogger errorLogger) { final @Nonnull TypeMirror declaredType = ProcessingUtility.getType(element); final @Nullable Class<?> desiredType = ProcessingUtility.getClass(ProcessingUtility.getAnnotationValue(annotationMirror)); if (desiredType != null && ProcessingUtility.isRawSubtype(declaredType, desiredType)) { errorLogger.log("The type $ is a subtype of $:", SourcePosition.of(element), ProcessingUtility.getSimpleName(declaredType), desiredType.getSimpleName()); } }
@Pure @Override public void checkUsage(@Nonnull Element element, @Nonnull AnnotationMirror annotationMirror, @NonCaptured @Modified @Nonnull ErrorLogger errorLogger) { final @Nonnull TypeMirror declaredType = ProcessingUtility.getType(element); final @Nullable Class<?> desiredType = ProcessingUtility.getClass(ProcessingUtility.getAnnotationValue(annotationMirror)); if (desiredType != null && !ProcessingUtility.isRawSubtype(declaredType, desiredType)) { errorLogger.log("The type $ is not a subtype of $:", SourcePosition.of(element), ProcessingUtility.getSimpleName(declaredType), desiredType.getSimpleName()); } }
@Pure @Override public void checkUsage(@Nonnull Element element, @Nonnull AnnotationMirror annotationMirror, @NonCaptured @Modified @Nonnull ErrorLogger errorLogger) { final @Nonnull TypeMirror declaredType = ProcessingUtility.getType(element); final @Nullable Class<?> desiredType = ProcessingUtility.getClass(ProcessingUtility.getAnnotationValue(annotationMirror)); if (desiredType != null && ProcessingUtility.isRawlyAssignable(declaredType, desiredType)) { errorLogger.log("The value of type $ is rawly assignable to $:", SourcePosition.of(element), ProcessingUtility.getSimpleName(declaredType), desiredType.getSimpleName()); } }
@Pure @Override public void checkUsage(@Nonnull Element element, @Nonnull AnnotationMirror annotationMirror, @NonCaptured @Modified @Nonnull ErrorLogger errorLogger) { final @Nonnull TypeMirror declaredType = ProcessingUtility.getType(element); final @Nullable Class<?> desiredType = ProcessingUtility.getClass(ProcessingUtility.getAnnotationValue(annotationMirror)); if (desiredType != null && !ProcessingUtility.isRawlyAssignable(declaredType, desiredType)) { errorLogger.log("The value of type $ is not rawly assignable to $:", SourcePosition.of(element), ProcessingUtility.getSimpleName(declaredType), desiredType.getSimpleName()); } }
final @Nullable Class<?> componentType = getClass(((ArrayType) typeMirror).getComponentType()); return componentType != null ? Array.newInstance(componentType, 0).getClass() : null; case BOOLEAN: return boolean.class;
final @Nullable Class<@Nonnull ?> annotationClass = ProcessingUtility.getClass(annotation.getAnnotationType()); Require.that(annotationClass != null).orThrow("The annotation class {} should not resolve into a null type."); if (alreadyProcessedAnnotations.add(annotationClass)) {
@Pure @Override public void generateFieldsRequiredByMethod(@Nonnull JavaFileGenerator javaFileGenerator, @Nonnull MethodInformation method, @Nonnull TypeInformation typeInformation) { final @Nonnull FiniteIterable<@Nonnull MethodParameterInformation> parameters = method.getParameters(); @Nullable Class<?> keyType = Tuple.getTupleType(parameters.size()); @Nonnull String keyTypeAsString; if (keyType == null) { if (parameters.size() == 0) { keyType = Object.class; } else if (parameters.size() == 1) { final @Nonnull TypeMirror typeMirror = ProcessingUtility.getBoxedType(parameters.getFirst().getType()); keyType = ProcessingUtility.getClass(typeMirror); } else { ProcessingLog.error("Cannot create a cache for more than 8 parameters (method: $).", method.getName()); } keyTypeAsString = javaFileGenerator.importIfPossible(keyType); } else { keyTypeAsString = javaFileGenerator.importIfPossible(keyType) + Brackets.inPointy(getListOfParameterTypes(javaFileGenerator, parameters)); } @Nonnull final TypeMirror valueType = ProcessingUtility.getBoxedType(method.getReturnType()); javaFileGenerator.addField("private final @" + javaFileGenerator.importIfPossible(Nonnull.class) + " " + javaFileGenerator.importIfPossible(HashMap.class) + Brackets.inPointy(keyTypeAsString + ", " + javaFileGenerator.importIfPossible(valueType)) + " " + getCacheName(javaFileGenerator, method) + " = new HashMap<>()"); }