Skip to content

Commit

Permalink
Refactoring and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingtoncosta committed Sep 28, 2017
1 parent 93d94e3 commit 2ca4929
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -120,47 +122,43 @@ private Set<Class<? extends Annotation>> getSupportedAnnotations() {

@Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment env) {
Map<TargetClassInfo, Set<FieldValidationInfo>> validationsMap = findAndParseValidations(env);

for (Map.Entry<TargetClassInfo, Set<FieldValidationInfo>> entry : validationsMap.entrySet()) {
TargetClassInfo targetClassInfo = entry.getKey();
TypeElement typeElement = targetClassInfo.getTypeElement();
Set<FieldValidationInfo> fields = entry.getValue();
List<ValidationClass> 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<TargetClassInfo, Set<FieldValidationInfo>> findAndParseValidations(RoundEnvironment env) {
Map<TargetClassInfo, Set<FieldValidationInfo>> map = new LinkedHashMap<>();
Set<TargetClassInfo> targetClassInfos = new LinkedHashSet<>();
Set<FieldValidationInfo> fieldValidationInfos = new LinkedHashSet<>();
private List<ValidationClass> findAndParseValidations(RoundEnvironment env) {
Set<Element> parents = new HashSet<>();
List<ValidationField> validationFields = new ArrayList<>();
List<ValidationClass> validationClasses = new ArrayList<>();

scanForRClasses(env);

// Process each @NotEmptyValidation element
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);
}
Expand All @@ -170,7 +168,7 @@ private Map<TargetClassInfo, Set<FieldValidationInfo>> 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);
}
Expand All @@ -180,7 +178,7 @@ private Map<TargetClassInfo, Set<FieldValidationInfo>> 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);
}
Expand All @@ -190,86 +188,79 @@ private Map<TargetClassInfo, Set<FieldValidationInfo>> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parseNotEmptyValidation(Element element, Set<Element> parents, List<ValidationField> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parseEmailValidation(Element element, Set<Element> parents, List<ValidationField> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parsePatternValidation(Element element, Set<Element> parents, List<ValidationField> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parseLengthValidation(Element element, Set<Element> parents, List<ValidationField> validationFields) {
int minLength = element.getAnnotation(LengthValidation.class).min();
int maxLength = element.getAnnotation(LengthValidation.class).max();

Expand All @@ -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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parseOnlyNumberValidation(Element element, Set<Element> parents, List<ValidationField> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void processPasswordValidations(RoundEnvironment env, Set<Element> parents, List<ValidationField> validationFields) {
Set<? extends Element> passwordElements = env.getElementsAnnotatedWith(PasswordValidation.class);

if (passwordElements.size() > 1) {
Expand All @@ -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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parsePasswordValidation(Element element, Set<Element> parents, List<ValidationField> 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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {
private void processConfirmPasswordValidations(RoundEnvironment env, Set<Element> parents, List<ValidationField> validationFields) {

Set<? extends Element> passwordElements = env.getElementsAnnotatedWith(PasswordValidation.class);
Set<? extends Element> confirmPasswordElements = env.getElementsAnnotatedWith(ConfirmPasswordValidation.class);
Expand All @@ -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<TargetClassInfo> targetClassInfos,
Set<FieldValidationInfo> fieldValidationInfos) {

private void parseConfirmPasswordValidation(Element element, Set<Element> parents, List<ValidationField> 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<? extends Annotation> annotationClass, Element element) {
Expand Down Expand Up @@ -497,10 +469,6 @@ private boolean isInaccessible(Class<? extends Annotation> annotationClass, Elem
return hasError;
}

private TargetClassInfo findTargetInfoElement(Set<TargetClassInfo> targetClassInfos) {
return targetClassInfos.iterator().hasNext() ? targetClassInfos.iterator().next() : null;
}

private static AnnotationMirror getMirror(Element element, Class<? extends Annotation> annotation) {
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
if (annotationMirror.getAnnotationType().toString().equals(annotation.getCanonicalName())) {
Expand Down
Loading

0 comments on commit 2ca4929

Please sign in to comment.