Skip to content

Commit 448429a

Browse files
committed
Attributes from class level annotations (MVC) are missing in Jooby 2.x fix jooby-project#1525
1 parent 414b5f5 commit 448429a

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

modules/jooby-apt/src/main/java/io/jooby/internal/apt/asm/RouteAttributesWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ public void process(ExecutableElement method) throws NoSuchMethodException {
108108
}
109109

110110
private Map<String, Object> annotationMap(ExecutableElement method) {
111-
return annotationMap(method.getAnnotationMirrors(), null);
111+
// class
112+
Map<String, Object> attributes = annotationMap(
113+
method.getEnclosingElement().getAnnotationMirrors(), null);
114+
// method
115+
attributes.putAll(annotationMap(method.getAnnotationMirrors(), null));
116+
return attributes;
112117
}
113118

114119
private Map<String, Object> annotationMap(List<? extends AnnotationMirror> annotations,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package source;
2+
3+
import io.jooby.annotations.GET;
4+
5+
@RoleAnnotation("User")
6+
public class RouteClassAttributes {
7+
@RoleAnnotation("Admin")
8+
@GET("/admin")
9+
public String admin() {
10+
return "...";
11+
}
12+
13+
@GET("/user")
14+
public String user() {
15+
return "...";
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tests;
2+
3+
import io.jooby.Route;
4+
import io.jooby.apt.MvcModuleCompilerRunner;
5+
import org.junit.jupiter.api.Test;
6+
import source.RouteClassAttributes;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
public class Issue1525 {
11+
@Test
12+
public void routeClassAttributes() throws Exception {
13+
new MvcModuleCompilerRunner(new RouteClassAttributes())
14+
.module(app -> {
15+
Route route0 = app.getRoutes().get(0);
16+
assertEquals(2, route0.getAttributes().size(), route0.getAttributes().toString());
17+
assertEquals("Admin", route0.attribute("roleAnnotation"));
18+
19+
Route route1 = app.getRoutes().get(1);
20+
assertEquals(2, route1.getAttributes().size(), route1.getAttributes().toString());
21+
assertEquals("User", route1.attribute("roleAnnotation"));
22+
})
23+
;
24+
}
25+
}

0 commit comments

Comments
 (0)