Skip to content

Commit

Permalink
Merge pull request #446 from agoncal/FORGE-1671
Browse files Browse the repository at this point in the history
FORGE-1671: Support for nested annotations
  • Loading branch information
gastaldi committed Apr 29, 2014
2 parents 058feff + 40ff3d8 commit 57f80df
Showing 1 changed file with 9 additions and 1 deletion.
Expand Up @@ -20,6 +20,7 @@
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;

import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
Expand Down Expand Up @@ -66,15 +67,22 @@ public Result execute(UIExecutionContext context) throws Exception
JavaResource javaResource = context.getUIContext().getSelection();
JavaAnnotationSource constraint = javaResource.getJavaType();
// Constraint annotation header
constraint.addAnnotation(Constraint.class).setStringValue("validatedBy = {}");
constraint.addAnnotation(Constraint.class).setLiteralValue("validatedBy", "{}");
constraint.addAnnotation(ReportAsSingleViolation.class);
constraint.addAnnotation(Retention.class).setEnumValue(RUNTIME);
constraint.addAnnotation(Target.class).setEnumValue(METHOD, FIELD, PARAMETER, TYPE, ANNOTATION_TYPE, CONSTRUCTOR);
constraint.addAnnotation(Documented.class);
constraint.addImport(Payload.class);
// Constraint annotation body
constraint.addAnnotationElement("String message() default \"Invalid value\"");
constraint.addAnnotationElement("Class<?>[] groups() default { }");
constraint.addAnnotationElement("Class<? extends Payload>[] payload() default { }");
// Add nested annotation
JavaAnnotationSource listNestedAnnotation = constraint.addNestedType(JavaAnnotationSource.class);
listNestedAnnotation.setName("List");
listNestedAnnotation.addAnnotation(Retention.class).setEnumValue(RUNTIME);
listNestedAnnotation.addAnnotation(Target.class).setEnumValue(METHOD, FIELD, PARAMETER, TYPE, ANNOTATION_TYPE, CONSTRUCTOR);
listNestedAnnotation.addAnnotationElement(constraint.getName() + "[] value()");

javaSourceFacet.saveJavaSource(constraint);
}
Expand Down

0 comments on commit 57f80df

Please sign in to comment.