Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 14, 2015
1 parent d05094a commit 2b3dc2a
Showing 1 changed file with 28 additions and 8 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -432,6 +446,12 @@ private static class ResourceGroup {
}
}

public static class ResourceClassDef {
public String value;
public String group;
public int priority;
}

private static class ResourceClass {
final String pack;
final String fqcn;
Expand Down

0 comments on commit 2b3dc2a

Please sign in to comment.