/** * Issue a compilation error and abandon the processing of this class. This does not prevent * the processing of other classes. * * @param msg The error message. * @param e The element at which the error occurred. */ private void abortWithError(String msg, Element e) { reportError(msg, e); throw new AbortProcessingException(); }
void writeMustBeTrue() { // Args: value, validationContext BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_MUST_BE_TRUE_METHOD_NAME, false); baseWriter.addArg(LITERAL, RaveWriter.VALIDATION_CONTEXT_ARG_NAME, true); buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
/** * Process only the {@link Validated} annotations. * * @param raveIR the IR that will be filled in with required information collected during processing. * @param allTypes the elements annotated with the {@link Validated} annotation. */ private void process(RaveIR raveIR, List<TypeElement> allTypes) { for (TypeElement typeElement : allTypes) { raveIR.addClassIR(extractClassInfo(typeElement, raveIR.getMode())); } }
/** * Generate the {@link BaseValidator} class. * * @param raveIR the {@link RaveIR} to generate the java. * @throws IOException if somethings fails when writing the file. */ private void generateSource(RaveIR raveIR) throws IOException { Filer filer = processingEnv.getFiler(); RaveWriter raveWriter = new RaveWriter(filer, typesUtils, processingEnv.getElementUtils()); raveWriter.generateJava(raveIR); }
private MethodSpec generateConstructor(RaveIR raveIR) { MethodSpec.Builder builder = MethodSpec.constructorBuilder(); for (ClassIR classIR : raveIR.getClassIRs()) { builder.addStatement("$L($T.class)", ADD_SUPPORTED_CLASS_METHOD_NAME, classIR.getTypeMirror()); } builder.addStatement("registerSelf()"); return builder.build(); }
MyFactory_Generated_Validator() { addSupportedClass(InheritFrom.class); addSupportedClass(ValidateByInterface.class); addSupportedClass(MultiMethodSampleModel.class); addSupportedClass(SingleMethodSampleModel.class); addSupportedClass(ArrayNotNull.class); addSupportedClass(AbstractAnnotated.class); addSupportedClass(IntDefModel.class); addSupportedClass(LongDefModel.class); addSupportedClass(IntRangeTestModel.class); addSupportedClass(FloatRangeTestModel.class); registerSelf(); }
@Test public void areConflicting_whenTrueAndFalseConflict_shouldReturnTrue() { assertTrue(CompilerUtils.areConflicting("com.uber.rave.annotation.MustBeFalse", "com.uber.rave.annotation.MustBeTrue")); assertTrue(CompilerUtils.areConflicting("com.uber.rave.annotation.MustBeTrue", "com.uber.rave.annotation.MustBeFalse")); } }
/** * The basic constructor. This gives you the first few args for the checks. * @param checkMethodName the name of the checker method. */ private BaseAnnotationWriter(String checkMethodName) { addArg(LITERAL + " = ", RaveWriter.RAVE_ERROR_ARG_NAME, false); addArg(LITERAL + "(", MERGE_ERROR_METHOD_NAME, false); addArg(LITERAL, RaveWriter.RAVE_ERROR_ARG_NAME, false); addArg(LITERAL + "(", checkMethodName, true); }
/** * Add the types that are inherited by this class. * @param typeMirror The type mirror to add. * @param typesUtils The {@link Types} utility needed to add the type mirror here. */ void addInheritedTypes(TypeMirror typeMirror, Types typesUtils) { if (!CompilerUtils.typeMirrorInCollection(inheritedTypes, typeMirror, typesUtils)) { inheritedTypes.add(typeMirror); } }
/** * @param annotationName the annotation, by fully qualified name, to check. * @return true if the annotation is an supported by RAVE, false otherwise. */ static boolean isSupportedAnnotation(String annotationName) { return annotationName.toLowerCase().contains("nullable") || annotationName.toLowerCase().contains("nonnull") || isSupportAnnotation(annotationName); }
@NonNull @Override public BaseValidator generateValidator() { return new com.uber.rave.compiler.MyFactory_Generated_Validator(); } }
void writeNullable() { // Args: value, isNullable, validationContext BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_NULL_METHOD_NAME, false); baseWriter.addArg(LITERAL, isNullable, true); baseWriter.addArg(LITERAL, RaveWriter.VALIDATION_CONTEXT_ARG_NAME, true); buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
/** * Add the getter arg to the string format. * @param getter the {@link MethodSpec} of the getter. * @param format the format of the item returned from the getter. */ private void addGetterCall(MethodSpec getter, String format) { addArg(format + ".", RaveWriter.VALIDATE_METHOD_ARG_NAME, true); addArg(NAMES + "()", getter, false); }
void writeMustBeFalse() { // Args: value, validationContext BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_MUST_BE_FALSE_METHOD_NAME, false); baseWriter.addArg(LITERAL, RaveWriter.VALIDATION_CONTEXT_ARG_NAME, true); buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
/** * Use this constructor if you want to auto generate the getter function call as one of the first params. If the * boolean parameter is set to true then the {@link BaseValidator.ValidationContext} object * is inserted first otherwise it is not. This is a convenience method for the * {@link BaseValidator} methods that start with either the getter method or the context. * @param getter the {@link MethodSpec} for the getter. * @param checkMethodName the name of the checker method being called. * @param contextFirst if true this will insert the {@link BaseValidator.ValidationContext} * parameter as the first parameter otherwise it won't be inserted at all. */ private BaseAnnotationWriter(MethodSpec getter, String checkMethodName, boolean contextFirst) { addArg(LITERAL + " = ", RaveWriter.RAVE_ERROR_ARG_NAME, false); addArg(LITERAL + "(", MERGE_ERROR_METHOD_NAME, false); addArg(LITERAL, RaveWriter.RAVE_ERROR_ARG_NAME, false); addArg(LITERAL + "(", checkMethodName, true); if (contextFirst) { addArg(LITERAL, RaveWriter.VALIDATION_CONTEXT_ARG_NAME, false); } addArg(LITERAL + ".", RaveWriter.VALIDATE_METHOD_ARG_NAME, contextFirst); addArg(NAMES + "()", getter, false); }
void write(@Nullable StringDef stringDef) { checkAnnotationNotNull(stringDef); boolean isStringDefNullable = hasNonNullOrNullable && isNullable; String[] acceptableStrings = stringDef.value(); //Args: isNullable, validationContext, value(s), acceptableValues BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(CHECK_MUST_BE_STRING_DEF_VALUE); baseWriter.addArg(LITERAL, isStringDefNullable, false); baseWriter.addArg(LITERAL, RaveWriter.VALIDATION_CONTEXT_ARG_NAME, true); baseWriter.addGetterCall(getter, LITERAL); for (String string : acceptableStrings) { baseWriter.addArg(STRING_LITERAL, string, true); } buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
void write(@Nullable IntDef intDef) { checkAnnotationNotNull(intDef); // Args: validationContext, value, flag, acceptableValues BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_MUST_BE_INT_DEF_VALUE, true); baseWriter.addArg(LITERAL, intDef.flag(), true); int[] acceptableInts = intDef.value(); for (int intVal : acceptableInts) { baseWriter.addArg(LITERAL, intVal, true); } buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
void write(@Nullable LongDef longDef) { checkAnnotationNotNull(longDef); // Args: validationContext, value, flag, acceptableValues BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_MUST_BE_LONG_DEF_VALUE, true); baseWriter.addArg(LITERAL, longDef.flag(), true); long[] acceptableInts = longDef.value(); for (long longVal : acceptableInts) { baseWriter.addArg(LITERAL_LONG, longVal, true); } buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
void write(@Nullable IntRange intRange) { checkAnnotationNotNull(intRange); // Args: validationContext, value, from, to BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_INT_RANGE_METHOD_NAME, true); baseWriter.addArg(LITERAL_LONG, intRange.from(), true); baseWriter.addArg(LITERAL_LONG, intRange.to(), true); buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }
void write(@Nullable FloatRange floatRange) { checkAnnotationNotNull(floatRange); // Args: validationContext, value, from, to BaseAnnotationWriter baseWriter = new BaseAnnotationWriter(getter, CHECK_FLOAT_RANGE_METHOD_NAME, true); if (Double.isInfinite(floatRange.from())) { baseWriter.addArg("Double.NEGATIVE_INFINITY", null, true); } else { baseWriter.addArg(LITERAL_DOUBLE, floatRange.from(), true); } if (Double.isInfinite(floatRange.to())) { baseWriter.addArg("Double.POSITIVE_INFINITY", null, true); } else { baseWriter.addArg(LITERAL_DOUBLE, floatRange.to(), true); } buildStatements(baseWriter.getFormattedString(), baseWriter.getArgs()); }