Skip to content

Commit 2b3dc2a

Browse files
xhanina-peyrard
authored andcommitted
[refactor] make RestxAnnotationProcessor reusable for different annotation
Now one can extend RestxAnnotationProcessor to define its own processor recognizing annotations other than RestxResource
1 parent d05094a commit 2b3dc2a

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

‎restx-core-annotation-processor/src/main/java/restx/annotations/processor/RestxAnnotationProcessor.java‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.annotation.processing.SupportedOptions;
2323
import javax.lang.model.element.*;
2424
import java.io.IOException;
25+
import java.lang.annotation.Annotation;
2526
import java.util.*;
2627
import java.util.regex.Matcher;
2728
import java.util.regex.Pattern;
@@ -59,7 +60,7 @@ protected boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvir
5960
for (ResourceMethodAnnotation annotation : getResourceMethodAnnotationsInRound(roundEnv)) {
6061
try {
6162
TypeElement typeElem = (TypeElement) annotation.methodElem.getEnclosingElement();
62-
RestxResource r = typeElem.getAnnotation(RestxResource.class);
63+
ResourceClassDef r = getResourceClassDef(typeElem);
6364
if (r == null) {
6465
error(
6566
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
8586

8687
ResourceMethod resourceMethod = new ResourceMethod(
8788
resourceClass,
88-
annotation.httpMethod, r.value() + annotation.path,
89+
annotation.httpMethod, r.value + annotation.path,
8990
annotation.methodElem.getSimpleName().toString(),
9091
annotation.methodElem.getReturnType().toString(),
9192
successStatus, logLevel, permission,
@@ -108,6 +109,19 @@ protected boolean processImpl(Set<? extends TypeElement> annotations, RoundEnvir
108109
return true;
109110
}
110111

112+
protected ResourceClassDef getResourceClassDef(TypeElement typeElem) {
113+
RestxResource r = typeElem.getAnnotation(RestxResource.class);
114+
ResourceClassDef resourceClassDef = new ResourceClassDef();
115+
resourceClassDef.value = r.value();
116+
resourceClassDef.priority = r.priority();
117+
resourceClassDef.group = r.group();
118+
return resourceClassDef;
119+
}
120+
121+
protected Class<? extends Annotation> getRestAnnotationClass() {
122+
return RestxResource.class;
123+
}
124+
111125
private String buildPermission(ResourceMethodAnnotation annotation, TypeElement typeElem) {
112126
String permission;
113127
PermitAll permitAll = annotation.methodElem.getAnnotation(PermitAll.class);
@@ -193,21 +207,21 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso
193207
}
194208
}
195209

196-
private ResourceGroup getResourceGroup(RestxResource r, Map<String, ResourceGroup> groups) {
197-
ResourceGroup group = groups.get(r.group());
210+
private ResourceGroup getResourceGroup(ResourceClassDef r, Map<String, ResourceGroup> groups) {
211+
ResourceGroup group = groups.get(r.group);
198212
if (group == null) {
199-
groups.put(r.group(), group = new ResourceGroup(r.group()));
213+
groups.put(r.group, group = new ResourceGroup(r.group));
200214
}
201215
return group;
202216
}
203217

204-
private ResourceClass getResourceClass(TypeElement typeElem, RestxResource r, ResourceGroup group, Set<Element> modulesListOriginatingElements) {
218+
private ResourceClass getResourceClass(TypeElement typeElem, ResourceClassDef r, ResourceGroup group, Set<Element> modulesListOriginatingElements) {
205219
String fqcn = typeElem.getQualifiedName().toString();
206220
ResourceClass resourceClass = group.resourceClasses.get(fqcn);
207221
if (resourceClass == null) {
208222
modulesListOriginatingElements.add(typeElem);
209223
When when = typeElem.getAnnotation(When.class);
210-
group.resourceClasses.put(fqcn, resourceClass = new ResourceClass(group, fqcn, r.priority(),
224+
group.resourceClasses.put(fqcn, resourceClass = new ResourceClass(group, fqcn, r.priority,
211225
when == null ? ""
212226
: ("@restx.factory.When(name=\"" + when.name() + "\", value=\"" + when.value() + "\")")));
213227
resourceClass.originatingElements.add(typeElem);
@@ -371,7 +385,7 @@ private String toTypeDescription(String type) {
371385

372386
private Collection<ResourceMethodAnnotation> getResourceMethodAnnotationsInRound(RoundEnvironment roundEnv) {
373387
Collection<ResourceMethodAnnotation> methodAnnotations = Lists.newArrayList();
374-
for (Element resourceElem : roundEnv.getElementsAnnotatedWith(RestxResource.class)) {
388+
for (Element resourceElem : roundEnv.getElementsAnnotatedWith(getRestAnnotationClass())) {
375389
if (! (resourceElem instanceof TypeElement)) {
376390
error(
377391
String.format("Only a class can be annotated with @RestxResource. Found %s",
@@ -432,6 +446,12 @@ private static class ResourceGroup {
432446
}
433447
}
434448

449+
public static class ResourceClassDef {
450+
public String value;
451+
public String group;
452+
public int priority;
453+
}
454+
435455
private static class ResourceClass {
436456
final String pack;
437457
final String fqcn;

0 commit comments

Comments
 (0)