diff --git a/convalida-compiler/src/main/java/convalida/compiler/ConvalidaProcessor.java b/convalida-compiler/src/main/java/convalida/compiler/ConvalidaProcessor.java index 009ce8f..67c15e2 100644 --- a/convalida-compiler/src/main/java/convalida/compiler/ConvalidaProcessor.java +++ b/convalida-compiler/src/main/java/convalida/compiler/ConvalidaProcessor.java @@ -11,9 +11,11 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.lang.annotation.Annotation; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -41,10 +43,10 @@ import convalida.annotations.OnlyNumberValidation; import convalida.annotations.PasswordValidation; import convalida.annotations.PatternValidation; -import convalida.compiler.internal.FieldValidationInfo; import convalida.compiler.internal.Id; import convalida.compiler.internal.QualifiedId; -import convalida.compiler.internal.TargetClassInfo; +import convalida.compiler.internal.ValidationClass; +import convalida.compiler.internal.ValidationField; import convalida.compiler.internal.scanners.IdScanner; import convalida.compiler.internal.scanners.RClassScanner; @@ -120,28 +122,25 @@ private Set> getSupportedAnnotations() { @Override public boolean process(Set typeElements, RoundEnvironment env) { - Map> validationsMap = findAndParseValidations(env); - - for (Map.Entry> entry : validationsMap.entrySet()) { - TargetClassInfo targetClassInfo = entry.getKey(); - TypeElement typeElement = targetClassInfo.getTypeElement(); - Set fields = entry.getValue(); + List validationClasses = findAndParseValidations(env); + // Generate validation classes source code + for (ValidationClass validationClass : validationClasses) { try { - JavaFile javaFile = JavaFiler.cookJava(targetClassInfo, fields); + JavaFile javaFile = JavaFiler.cookJava(validationClass); javaFile.writeTo(this.filer); } catch (IOException e) { - error(typeElement, "Unable to write validation for type %s: %s", typeElement, e.getMessage()); + error(validationClass.getTypeElement(), "Unable to write validation for type %s: %s", validationClass.getTypeElement(), e.getMessage()); } } return false; } - private Map> findAndParseValidations(RoundEnvironment env) { - Map> map = new LinkedHashMap<>(); - Set targetClassInfos = new LinkedHashSet<>(); - Set fieldValidationInfos = new LinkedHashSet<>(); + private List findAndParseValidations(RoundEnvironment env) { + Set parents = new HashSet<>(); + List validationFields = new ArrayList<>(); + List validationClasses = new ArrayList<>(); scanForRClasses(env); @@ -149,18 +148,17 @@ private Map> findAndParseValidations(R for (Element element : env.getElementsAnnotatedWith(NotEmptyValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parseNotEmptyValidation(element, targetClassInfos, fieldValidationInfos); + parseNotEmptyValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, NotEmptyValidation.class, e); } } - - // Process each @EmailValidation element. + // Process each @EmailValidation element for (Element element : env.getElementsAnnotatedWith(EmailValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parseEmailValidation(element, targetClassInfos, fieldValidationInfos); + parseEmailValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, EmailValidation.class, e); } @@ -170,7 +168,7 @@ private Map> findAndParseValidations(R for (Element element : env.getElementsAnnotatedWith(PatternValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parsePatternValidation(element, targetClassInfos, fieldValidationInfos); + parsePatternValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, PatternValidation.class, e); } @@ -180,7 +178,7 @@ private Map> findAndParseValidations(R for (Element element : env.getElementsAnnotatedWith(LengthValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parseLengthValidation(element, targetClassInfos, fieldValidationInfos); + parseLengthValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, LengthValidation.class, e); } @@ -190,86 +188,79 @@ private Map> findAndParseValidations(R for (Element element : env.getElementsAnnotatedWith(OnlyNumberValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parseOnlyNumberValidation(element, targetClassInfos, fieldValidationInfos); + parseOnlyNumberValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, OnlyNumberValidation.class, e); } } // Process each @PasswordValidation element. - processPasswordValidations(env, targetClassInfos, fieldValidationInfos); + processPasswordValidations(env, parents, validationFields); // Process each @ConfirmPasswordValidation element. - processConfirmPasswordValidations(env, targetClassInfos, fieldValidationInfos); + processConfirmPasswordValidations(env, parents, validationFields); + + + // Assemble the validation classes and fields + for (Element parent : parents) { + ValidationClass validationClass = new ValidationClass(parent, this.elementUtils); - TargetClassInfo targetClassInfo = findTargetInfoElement(targetClassInfos); + for (ValidationField validationField : validationFields) { + Element element = validationField.getElement(); + if (element.getEnclosingElement().equals(parent)) { + validationClass.addField(validationField); + } + } - if (targetClassInfo != null && fieldValidationInfos.size() > 0) { - map.put(targetClassInfo, fieldValidationInfos); + validationClasses.add(validationClass); } - return map; + return validationClasses; } - private void parseNotEmptyValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parseNotEmptyValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(NotEmptyValidation.class, element) || isInaccessible(NotEmptyValidation.class, element); if (hasError) { return; } - int id = element.getAnnotation(NotEmptyValidation.class).value(); - QualifiedId qualifiedId = elementToQualifiedId(element, id); + int errorMessageResourceId = element.getAnnotation(NotEmptyValidation.class).value(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, NotEmptyValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, NotEmptyValidation.class.getCanonicalName(), getId(qualifiedId))); } - private void parseEmailValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parseEmailValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(EmailValidation.class, element) || isInaccessible(EmailValidation.class, element); if (hasError) { return; } - int id = element.getAnnotation(EmailValidation.class).value(); - QualifiedId qualifiedId = elementToQualifiedId(element, id); + int errorMessageResourceId = element.getAnnotation(EmailValidation.class).value(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, EmailValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, EmailValidation.class.getCanonicalName(), getId(qualifiedId))); } - private void parsePatternValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parsePatternValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(PatternValidation.class, element) || isInaccessible(PatternValidation.class, element); if (hasError) { return; } - int errorMessage = element.getAnnotation(PatternValidation.class).errorMessage(); - QualifiedId qualifiedId = elementToQualifiedId(element, errorMessage); + int errorMessageResourceId = element.getAnnotation(PatternValidation.class).errorMessage(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, PatternValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, PatternValidation.class.getCanonicalName(), getId(qualifiedId))); } - private void parseLengthValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parseLengthValidation(Element element, Set parents, List validationFields) { int minLength = element.getAnnotation(LengthValidation.class).min(); int maxLength = element.getAnnotation(LengthValidation.class).max(); @@ -288,39 +279,31 @@ private void parseLengthValidation( return; } - int errorMessage = element.getAnnotation(LengthValidation.class).errorMessage(); - QualifiedId qualifiedId = elementToQualifiedId(element, errorMessage); + int errorMessageResourceId = element.getAnnotation(LengthValidation.class).errorMessage(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, LengthValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, LengthValidation.class.getCanonicalName(), getId(qualifiedId))); } catch (Exception e) { logParsingError(element, LengthValidation.class, e); } } - private void parseOnlyNumberValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parseOnlyNumberValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(OnlyNumberValidation.class, element) || isInaccessible(OnlyNumberValidation.class, element); if (hasError) { return; } - int id = element.getAnnotation(OnlyNumberValidation.class).value(); - QualifiedId qualifiedId = elementToQualifiedId(element, id); + int errorMessageResourceId = element.getAnnotation(OnlyNumberValidation.class).value(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, OnlyNumberValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, OnlyNumberValidation.class.getCanonicalName(), getId(qualifiedId))); } - private void processPasswordValidations( - RoundEnvironment env, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void processPasswordValidations(RoundEnvironment env, Set parents, List validationFields) { Set passwordElements = env.getElementsAnnotatedWith(PasswordValidation.class); if (passwordElements.size() > 1) { @@ -336,35 +319,28 @@ private void processPasswordValidations( for (Element element : env.getElementsAnnotatedWith(PasswordValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parsePasswordValidation(element, targetClassInfos, fieldValidationInfos); + parsePasswordValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, PasswordValidation.class, e); } } } - private void parsePasswordValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parsePasswordValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(PasswordValidation.class, element) || isInaccessible(PasswordValidation.class, element); if (hasError) { return; } - int errorMessage = element.getAnnotation(PasswordValidation.class).errorMessage(); - QualifiedId qualifiedId = elementToQualifiedId(element, errorMessage); + int errorMessageResourceId = element.getAnnotation(PasswordValidation.class).errorMessage(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, PasswordValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, PasswordValidation.class.getCanonicalName(), getId(qualifiedId))); } - private void processConfirmPasswordValidations( - RoundEnvironment env, - Set targetClassInfos, - Set fieldValidationInfos) { + private void processConfirmPasswordValidations(RoundEnvironment env, Set parents, List validationFields) { Set passwordElements = env.getElementsAnnotatedWith(PasswordValidation.class); Set confirmPasswordElements = env.getElementsAnnotatedWith(ConfirmPasswordValidation.class); @@ -391,29 +367,25 @@ private void processConfirmPasswordValidations( for (Element element : env.getElementsAnnotatedWith(ConfirmPasswordValidation.class)) { if (!SuperficialValidation.validateElement(element)) continue; try { - parseConfirmPasswordValidation(element, targetClassInfos, fieldValidationInfos); + parseConfirmPasswordValidation(element, parents, validationFields); } catch (Exception e) { logParsingError(element, ConfirmPasswordValidation.class, e); } } } - private void parseConfirmPasswordValidation( - Element element, - Set targetClassInfos, - Set fieldValidationInfos) { - + private void parseConfirmPasswordValidation(Element element, Set parents, List validationFields) { boolean hasError = isInvalid(ConfirmPasswordValidation.class, element) || isInaccessible(ConfirmPasswordValidation.class, element); if (hasError) { return; } - int id = element.getAnnotation(ConfirmPasswordValidation.class).value(); - QualifiedId qualifiedId = elementToQualifiedId(element, id); + int errorMessageResourceId = element.getAnnotation(ConfirmPasswordValidation.class).value(); + QualifiedId qualifiedId = elementToQualifiedId(element, errorMessageResourceId); - targetClassInfos.add(new TargetClassInfo(element.getEnclosingElement(), this.elementUtils)); - fieldValidationInfos.add(new FieldValidationInfo(element, ConfirmPasswordValidation.class.getCanonicalName(), getId(qualifiedId))); + parents.add(element.getEnclosingElement()); + validationFields.add(new ValidationField(element, ConfirmPasswordValidation.class.getCanonicalName(), getId(qualifiedId))); } private boolean isInvalid(Class annotationClass, Element element) { @@ -497,10 +469,6 @@ private boolean isInaccessible(Class annotationClass, Elem return hasError; } - private TargetClassInfo findTargetInfoElement(Set targetClassInfos) { - return targetClassInfos.iterator().hasNext() ? targetClassInfos.iterator().next() : null; - } - private static AnnotationMirror getMirror(Element element, Class annotation) { for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) { if (annotationMirror.getAnnotationType().toString().equals(annotation.getCanonicalName())) { diff --git a/convalida-compiler/src/main/java/convalida/compiler/JavaFiler.java b/convalida-compiler/src/main/java/convalida/compiler/JavaFiler.java index 1f98911..3595593 100644 --- a/convalida-compiler/src/main/java/convalida/compiler/JavaFiler.java +++ b/convalida-compiler/src/main/java/convalida/compiler/JavaFiler.java @@ -7,16 +7,14 @@ import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; -import java.util.Set; - import javax.lang.model.element.Modifier; import convalida.annotations.LengthValidation; import convalida.annotations.PasswordValidation; import convalida.annotations.PatternValidation; -import convalida.compiler.internal.FieldValidationInfo; import convalida.compiler.internal.Id; -import convalida.compiler.internal.TargetClassInfo; +import convalida.compiler.internal.ValidationClass; +import convalida.compiler.internal.ValidationField; /** * @author Wellington Costa on 19/06/2017. @@ -48,77 +46,76 @@ class JavaFiler { private static final ClassName CONFIRM_PASSWORD_VALIDATOR = ClassName.get(VALIDATORS_PACKAGE, "ConfirmPasswordValidator"); - static JavaFile cookJava(TargetClassInfo targetClassInfo, Set fieldValidationInfos) { - TypeSpec classValidator = TypeSpec.classBuilder(targetClassInfo.getClassName()) + static JavaFile cookJava(ValidationClass validationClass) { + TypeSpec classValidator = TypeSpec.classBuilder(validationClass.getClassName()) .addSuperinterface(VALIDATOR) .addModifiers(Modifier.PUBLIC) .addField(VALIDATION_SET, "validationSet", Modifier.PRIVATE) - .addMethod(createConstructor(targetClassInfo, fieldValidationInfos)) + .addMethod(createConstructor(validationClass)) .addMethod(createValidateMethod()) .addMethod(createClearValidationMethod()) .build(); - return JavaFile.builder(targetClassInfo.getPackageName(), classValidator) + return JavaFile.builder(validationClass.getPackageName(), classValidator) .addFileComment("Generated code from Convalida. Do not modify!") .build(); } - private static MethodSpec createConstructor(TargetClassInfo targetClassInfo, Set fieldValidationInfos) { + private static MethodSpec createConstructor(ValidationClass validationClass) { MethodSpec.Builder constructorBuilder = MethodSpec.constructorBuilder() .addAnnotation(UI_THREAD) .addModifiers(Modifier.PUBLIC) - .addParameter(targetClassInfo.getTypeName(), "target") + .addParameter(validationClass.getTypeName(), "target") .addStatement("this.$N = new $T()", "validationSet", VALIDATION_SET); - for (FieldValidationInfo fieldValidationInfo : fieldValidationInfos) { - chooseValidationStrategy(constructorBuilder, fieldValidationInfo); + for (ValidationField validationField : validationClass.getFields()) { + chooseValidationStrategy(constructorBuilder, validationField); } return constructorBuilder.build(); } - private static void chooseValidationStrategy(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { - String annotationClass = fieldValidationInfo.getAnnotationClass(); + private static void chooseValidationStrategy(MethodSpec.Builder constructorBuilder, ValidationField validationField) { + String annotationClass = validationField.getAnnotationClass(); switch (annotationClass) { case NOT_EMPTY_ANNOTATION: - createNotEmptyValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createNotEmptyValidationCodeBlock(constructorBuilder, validationField); break; case EMAIL_ANNOTATION: - createEmailValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createEmailValidationCodeBlock(constructorBuilder, validationField); break; case PATTERN_ANNOTATION: - createPatternValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createPatternValidationCodeBlock(constructorBuilder, validationField); break; case LENGTH_ANNOTATION: - createLengthValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createLengthValidationCodeBlock(constructorBuilder, validationField); break; - case ONLY_NUMBER_ANNOTATION: - createOnlyNumberValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createOnlyNumberValidationCodeBlock(constructorBuilder, validationField); break; case PASSWORD_ANNOTATION: - createPasswordValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createPasswordValidationCodeBlock(constructorBuilder, validationField); break; case CONFIRM_PASSWORD_ANNOTATION: - createConfirmPasswordValidationCodeBlock(constructorBuilder, fieldValidationInfo); + createConfirmPasswordValidationCodeBlock(constructorBuilder, validationField); break; } } - private static void createNotEmptyValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { + private static void createNotEmptyValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { CodeBlock notEmptyValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createElementDeclarationCode(validationField)) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N))", "validationSet", NOT_EMPTY_VALIDATOR, - fieldValidationInfo.getName(), + validationField.getName(), "errorMessage" ) .unindent() @@ -129,19 +126,19 @@ private static void createNotEmptyValidationCodeBlock(MethodSpec.Builder constru constructorBuilder.addCode(notEmptyValidationCodeBlock); } - private static void createEmailValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { + private static void createEmailValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { CodeBlock emailValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createElementDeclarationCode(validationField)) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N))", "validationSet", EMAIL_VALIDATOR, - fieldValidationInfo.getName(), + validationField.getName(), "errorMessage" ) .unindent() @@ -152,23 +149,23 @@ private static void createEmailValidationCodeBlock(MethodSpec.Builder constructo constructorBuilder.addCode(emailValidationCodeBlock); } - private static void createPatternValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { + private static void createPatternValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { CodeBlock patternValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createElementDeclarationCode(validationField)) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "String pattern = $S", - fieldValidationInfo.getElement().getAnnotation(PatternValidation.class).pattern() + validationField.getElement().getAnnotation(PatternValidation.class).pattern() ) .addStatement( "this.$N.addValidator(new $T($N, $N, $N))", "validationSet", PATTERN_VALIDATOR, - fieldValidationInfo.getName(), + validationField.getName(), "errorMessage", "pattern" ) @@ -180,23 +177,23 @@ private static void createPatternValidationCodeBlock(MethodSpec.Builder construc constructorBuilder.addCode(patternValidationCodeBlock); } - private static void createLengthValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { - int minLength = fieldValidationInfo.getElement().getAnnotation(LengthValidation.class).min(); - int maxLength = fieldValidationInfo.getElement().getAnnotation(LengthValidation.class).max(); + private static void createLengthValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { + int minLength = validationField.getElement().getAnnotation(LengthValidation.class).min(); + int maxLength = validationField.getElement().getAnnotation(LengthValidation.class).max(); CodeBlock lengthValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) + .add(createElementDeclarationCode(validationField)) .addStatement("int minLength = $L", minLength) .addStatement("int maxLength = $L", maxLength) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N, $N, $N))", "validationSet", LENGTH_VALIDATOR, - fieldValidationInfo.getName(), + validationField.getName(), "minLength", "maxLength", "errorMessage" @@ -209,19 +206,19 @@ private static void createLengthValidationCodeBlock(MethodSpec.Builder construct constructorBuilder.addCode(lengthValidationCodeBlock); } - private static void createOnlyNumberValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { + private static void createOnlyNumberValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { CodeBlock numericOnlyValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createElementDeclarationCode(validationField)) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N))", "validationSet", ONLY_NUMBER_VALIDATOR, - fieldValidationInfo.getName(), + validationField.getName(), "errorMessage" ) .unindent() @@ -232,16 +229,16 @@ private static void createOnlyNumberValidationCodeBlock(MethodSpec.Builder const constructorBuilder.addCode(numericOnlyValidationCodeBlock); } - private static void createPasswordValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { - int minPasswordLength = fieldValidationInfo.getElement().getAnnotation(PasswordValidation.class).min(); - String passwordPattern = fieldValidationInfo.getElement().getAnnotation(PasswordValidation.class).pattern(); + private static void createPasswordValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { + int minPasswordLength = validationField.getElement().getAnnotation(PasswordValidation.class).min(); + String passwordPattern = validationField.getElement().getAnnotation(PasswordValidation.class).pattern(); CodeBlock passwordValidationCodeBlock = CodeBlock.builder() .add("\n") .addStatement( "$T passwordField = $N.$N", - fieldValidationInfo.getTypeName(), + validationField.getTypeName(), "target", - fieldValidationInfo.getName() + validationField.getName() ) .add("\n") .add("{") @@ -249,7 +246,7 @@ private static void createPasswordValidationCodeBlock(MethodSpec.Builder constru .indent() .addStatement("int min = $L", minPasswordLength) .addStatement("String pattern = $S", passwordPattern) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N, $N, $N))", "validationSet", @@ -267,20 +264,20 @@ private static void createPasswordValidationCodeBlock(MethodSpec.Builder constru constructorBuilder.addCode(passwordValidationCodeBlock); } - private static void createConfirmPasswordValidationCodeBlock(MethodSpec.Builder constructorBuilder, FieldValidationInfo fieldValidationInfo) { + private static void createConfirmPasswordValidationCodeBlock(MethodSpec.Builder constructorBuilder, ValidationField validationField) { CodeBlock confirmPasswordValidationCodeBlock = CodeBlock.builder() .add("\n") .add("{") .add("\n") .indent() - .add(createElementDeclarationCode(fieldValidationInfo)) - .add(createErrorMessageDeclarationCode(fieldValidationInfo.getId())) + .add(createElementDeclarationCode(validationField)) + .add(createErrorMessageDeclarationCode(validationField.getId())) .addStatement( "this.$N.addValidator(new $T($N, $N, $N))", "validationSet", CONFIRM_PASSWORD_VALIDATOR, "passwordField", - fieldValidationInfo.getName(), + validationField.getName(), "errorMessage" ) .unindent() @@ -291,14 +288,14 @@ private static void createConfirmPasswordValidationCodeBlock(MethodSpec.Builder constructorBuilder.addCode(confirmPasswordValidationCodeBlock); } - private static CodeBlock createElementDeclarationCode(FieldValidationInfo fieldValidationInfo) { + private static CodeBlock createElementDeclarationCode(ValidationField validationField) { return CodeBlock.builder() .addStatement( "$T $N = $N.$N", - fieldValidationInfo.getTypeName(), - fieldValidationInfo.getName(), + validationField.getTypeName(), + validationField.getName(), "target", - fieldValidationInfo.getName() + validationField.getName() ) .build(); } diff --git a/convalida-compiler/src/main/java/convalida/compiler/internal/TargetClassInfo.java b/convalida-compiler/src/main/java/convalida/compiler/internal/ValidationClass.java similarity index 67% rename from convalida-compiler/src/main/java/convalida/compiler/internal/TargetClassInfo.java rename to convalida-compiler/src/main/java/convalida/compiler/internal/ValidationClass.java index cc920ad..3024596 100644 --- a/convalida-compiler/src/main/java/convalida/compiler/internal/TargetClassInfo.java +++ b/convalida-compiler/src/main/java/convalida/compiler/internal/ValidationClass.java @@ -2,35 +2,44 @@ import com.squareup.javapoet.TypeName; +import java.util.ArrayList; +import java.util.List; + import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.lang.model.util.Elements; /** - * @author Wellington Costa on 19/06/2017. + * @author Wellington Costa on 27/09/2017. */ -public class TargetClassInfo { +public class ValidationClass { - private String packageName; private String className; + private String packageName; private TypeName typeName; private TypeElement typeElement; + private List fields; - public TargetClassInfo(Element element, Elements elements) { + public ValidationClass(Element element, Elements elements) { this.packageName = elements.getPackageOf(element).toString(); this.className = element.getSimpleName().toString() + "_Validation"; this.typeName = TypeName.get(element.asType()); this.typeElement = (TypeElement) element; + this.fields = new ArrayList<>(); } - public String getPackageName() { - return packageName; + public void addField(ValidationField field) { + fields.add(field); } public String getClassName() { return className; } + public String getPackageName() { + return packageName; + } + public TypeName getTypeName() { return typeName; } @@ -38,4 +47,8 @@ public TypeName getTypeName() { public TypeElement getTypeElement() { return typeElement; } -} + + public List getFields() { + return fields; + } +} \ No newline at end of file diff --git a/convalida-compiler/src/main/java/convalida/compiler/internal/FieldValidationInfo.java b/convalida-compiler/src/main/java/convalida/compiler/internal/ValidationField.java similarity index 80% rename from convalida-compiler/src/main/java/convalida/compiler/internal/FieldValidationInfo.java rename to convalida-compiler/src/main/java/convalida/compiler/internal/ValidationField.java index c23b489..868b976 100644 --- a/convalida-compiler/src/main/java/convalida/compiler/internal/FieldValidationInfo.java +++ b/convalida-compiler/src/main/java/convalida/compiler/internal/ValidationField.java @@ -1,13 +1,14 @@ package convalida.compiler.internal; import com.squareup.javapoet.TypeName; -import java.lang.annotation.Annotation; + import javax.lang.model.element.Element; /** - * @author Wellington Costa on 19/06/2017. + * @author Wellington Costa on 27/09/2017. */ -public class FieldValidationInfo { + +public class ValidationField { private Element element; private String name; @@ -15,7 +16,7 @@ public class FieldValidationInfo { private String annotationClass; private Id id; - public FieldValidationInfo(Element element, String annotationClass, Id id) { + public ValidationField(Element element, String annotationClass, Id id) { this.element = element; this.name = element.getSimpleName().toString(); this.typeName = TypeName.get(element.asType()); @@ -42,4 +43,4 @@ public String getAnnotationClass() { public Id getId() { return id; } -} +} \ No newline at end of file diff --git a/convalida/src/main/java/convalida/library/validation/validator/AbstractValidator.java b/convalida/src/main/java/convalida/library/validation/validator/AbstractValidator.java index 99059c5..444217d 100644 --- a/convalida/src/main/java/convalida/library/validation/validator/AbstractValidator.java +++ b/convalida/src/main/java/convalida/library/validation/validator/AbstractValidator.java @@ -59,7 +59,7 @@ public void afterTextChanged(Editable s) { } }); } - void setError() { + private void setError() { if (layout != null) { layout.setErrorEnabled(true); layout.setError(errorMessage); @@ -70,7 +70,7 @@ void setError() { hasError = true; } - void clearError() { + private void clearError() { if (layout != null) { layout.setErrorEnabled(false); layout.setError(null);