Skip to content

Commit

Permalink
validation - Fixing some ClassNotFoundException when retrieving @Vali…
Browse files Browse the repository at this point in the history
…datedFor.value()

since in most of cases, plain Class instance won't be available yet at
annotation processing time

Note that current impl has a drawback by relying on com.sun.tools.javac.code
hidden package
  • Loading branch information
fcamblor committed Jan 2, 2015
1 parent 4afdbce commit d65fd01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package restx.annotations;

import com.sun.tools.javac.code.Attribute;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author fcamblor
*/
public class Annotations {
public static String[] getAnnotationClassValuesAsFQCN(VariableElement p, Class annotationClazz, String methodName) {
List<? extends AnnotationMirror> annotationMirrors = p.getAnnotationMirrors();
for(AnnotationMirror annotationMirror : annotationMirrors){
if(annotationMirror.getAnnotationType().toString().equals(annotationClazz.getCanonicalName())){
for(Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()){
if(entry.getKey().getSimpleName().contentEquals(methodName)){
Attribute.Array array = (Attribute.Array) entry.getValue();

List<String> fqcns = new ArrayList<>();
for(Attribute attribute : array.getValue()) {
DeclaredType type = (DeclaredType) attribute.getValue();
fqcns.add(type.toString());
}
return fqcns.toArray(new String[fqcns.size()]);
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso

String[] validationGroups = new String[0];
if(validatedFor != null) {
validationGroups = Collections2.transform(Arrays.asList(validatedFor.value()), FQN_EXTRACTOR).toArray(new String[0]);
validationGroups = Annotations.getAnnotationClassValuesAsFQCN(p, ValidatedFor.class, "value");
}

resourceMethod.parameters.add(new ResourceMethodParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @author fcamblor
*/
public interface FormValidations {
public static final String DefaultFQN = "javax.validation.groups.Default";
public static final String CreateFQN = "restx.validation.stereotypes.FormValidations.Create";
public static interface Create extends Default{}
public static final String UpdateFQN = "restx.validation.stereotypes.FormValidations.Update";
public static interface Update extends Default{}
}

0 comments on commit d65fd01

Please sign in to comment.