/** * 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 component types of the given type if it is an array or an iterable and logs errors with the given logger otherwise. */ @Pure @LogsErrorWhenReturningNull public static @Nullable List<TypeMirror> getComponentTypes(@Nonnull TypeMirror type, @NonCaptured @Modified @Nonnull ErrorLogger errorLogger) { final @Nonnull List<TypeMirror> list = new ArrayList<>(); if (type.getKind() == TypeKind.ARRAY) { list.add(((ArrayType) type).getComponentType()); return list; } else if (type.getKind() == TypeKind.DECLARED) { @Nullable DeclaredType supertype = getSupertype((DeclaredType) type, Iterable.class); if (supertype == null) { supertype = getSupertype((DeclaredType) type, Map.class); } if (supertype != null) { for (@Nonnull TypeMirror typeArgument : supertype.getTypeArguments()) { list.add(typeArgument); } return list; } else { errorLogger.log("The declared type $ is not iterable.", null, type); } } else { errorLogger.log("The type $ is neither an array nor a declared type.", null, type); } return null; }
addStatement("encoder.encode" + Strings.uppercaseFirstCharacter(CustomType.of(type, field.getAnnotations()).getTypeName().toLowerCase()) + "(" + access + ")"); } else if (ProcessingUtility.isRawSubtype(type, Map.class)) { final @Nullable DeclaredType supertype = ProcessingUtility.getSupertype((DeclaredType) type, Map.class); if (supertype != null) { final boolean nullable = !field.hasAnnotation(NonNullableElements.class);
addStatement("final " + importIfPossible(field.getType()) + " " + field.getName() + " = decoder.decode" + Strings.uppercaseFirstCharacter(CustomType.of(type, field.getAnnotations()).getTypeName().toLowerCase()) + "()"); } else if (ProcessingUtility.isRawSubtype(type, Map.class)) { final @Nullable DeclaredType supertype = ProcessingUtility.getSupertype((DeclaredType) type, Map.class); if (supertype != null) { final boolean nullable = !field.hasAnnotation(NonNullableElements.class);