Permalink
Browse files

[refactor] make RestxAnnotationProcessor reusable for different annot…

…ation

Now one can extend RestxAnnotationProcessor to define its own processor
recognizing annotations other than RestxResource
  • Loading branch information...
xhanin authored and a-peyrard committed Jun 5, 2015
1 parent d05094a commit 2b3dc2a7ea8a949a7d0507aff048b2fef29a226a
@@ -22,6 +22,7 @@
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.element.*;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -59,7 +60,7 @@ protected boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvir
for (ResourceMethodAnnotation annotation : getResourceMethodAnnotationsInRound(roundEnv)) {
try {
TypeElement typeElem = (TypeElement) annotation.methodElem.getEnclosingElement();
RestxResource r = typeElem.getAnnotation(RestxResource.class);
ResourceClassDef r = getResourceClassDef(typeElem);
if (r == null) {
error(
String.format("%s rest method found - enclosing class %s must be annotated with @RestxResource",
@@ -85,7 +86,7 @@ protected boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvir
ResourceMethod resourceMethod = new ResourceMethod(
resourceClass,
annotation.httpMethod, r.value() + annotation.path,
annotation.httpMethod, r.value + annotation.path,
annotation.methodElem.getSimpleName().toString(),
annotation.methodElem.getReturnType().toString(),
successStatus, logLevel, permission,
@@ -108,6 +109,19 @@ protected boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvir
return true;
}
protected ResourceClassDef getResourceClassDef(TypeElement typeElem) {
RestxResource r = typeElem.getAnnotation(RestxResource.class);
ResourceClassDef resourceClassDef = new ResourceClassDef();
resourceClassDef.value = r.value();
resourceClassDef.priority = r.priority();
resourceClassDef.group = r.group();
return resourceClassDef;
}
protected Class<? extends Annotation> getRestAnnotationClass() {
return RestxResource.class;
}
private String buildPermission(ResourceMethodAnnotation annotation, TypeElement typeElem) {
String permission;
PermitAll permitAll = annotation.methodElem.getAnnotation(PermitAll.class);
@@ -193,21 +207,21 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso
}
}
private ResourceGroup getResourceGroup(RestxResource r, Map<String, ResourceGroup> groups) {
ResourceGroup group = groups.get(r.group());
private ResourceGroup getResourceGroup(ResourceClassDef r, Map<String, ResourceGroup> groups) {
ResourceGroup group = groups.get(r.group);
if (group == null) {
groups.put(r.group(), group = new ResourceGroup(r.group()));
groups.put(r.group, group = new ResourceGroup(r.group));
}
return group;
}
private ResourceClass getResourceClass(TypeElement typeElem, RestxResource r, ResourceGroup group, Set<Element> modulesListOriginatingElements) {
private ResourceClass getResourceClass(TypeElement typeElem, ResourceClassDef r, ResourceGroup group, Set<Element> modulesListOriginatingElements) {
String fqcn = typeElem.getQualifiedName().toString();
ResourceClass resourceClass = group.resourceClasses.get(fqcn);
if (resourceClass == null) {
modulesListOriginatingElements.add(typeElem);
When when = typeElem.getAnnotation(When.class);
group.resourceClasses.put(fqcn, resourceClass = new ResourceClass(group, fqcn, r.priority(),
group.resourceClasses.put(fqcn, resourceClass = new ResourceClass(group, fqcn, r.priority,
when == null ? ""
: ("@restx.factory.When(name=\"" + when.name() + "\", value=\"" + when.value() + "\")")));
resourceClass.originatingElements.add(typeElem);
@@ -371,7 +385,7 @@ private String toTypeDescription(String type) {
private Collection<ResourceMethodAnnotation> getResourceMethodAnnotationsInRound(RoundEnvironment roundEnv) {
Collection<ResourceMethodAnnotation> methodAnnotations = Lists.newArrayList();
for (Element resourceElem : roundEnv.getElementsAnnotatedWith(RestxResource.class)) {
for (Element resourceElem : roundEnv.getElementsAnnotatedWith(getRestAnnotationClass())) {
if (! (resourceElem instanceof TypeElement)) {
error(
String.format("Only a class can be annotated with @RestxResource. Found %s",
@@ -432,6 +446,12 @@ private ResourceMethodAnnotation(String httpMethod, Element methodElem, String p
}
}
public static class ResourceClassDef {
public String value;
public String group;
public int priority;
}
private static class ResourceClass {
final String pack;
final String fqcn;

0 comments on commit 2b3dc2a

Please sign in to comment.