Skip to content

Commit e5bdeb7

Browse files
committed
jooby-apt: add option to remove mvcMethod and returnType from route
- fix #3492
1 parent e1d12d1 commit e5bdeb7

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

jooby/src/main/java/io/jooby/Route.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ public boolean isNonBlockingSet() {
652652
}
653653

654654
/**
655-
* Return return type.
655+
* Route return type.
656656
*
657657
* @return Return type.
658658
*/

modules/jooby-apt/src/main/java/io/jooby/apt/JoobyProcessor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030

3131
import io.jooby.internal.apt.*;
3232

33-
@SupportedOptions({HANDLER, DEBUG, INCREMENTAL, SERVICES, SKIP_ATTRIBUTE_ANNOTATIONS})
33+
@SupportedOptions({
34+
HANDLER,
35+
DEBUG,
36+
INCREMENTAL,
37+
SERVICES,
38+
MVC_METHOD,
39+
RETURN_TYPE,
40+
SKIP_ATTRIBUTE_ANNOTATIONS
41+
})
3442
@SupportedSourceVersion(SourceVersion.RELEASE_17)
3543
public class JoobyProcessor extends AbstractProcessor {
3644
public interface Options {
@@ -39,6 +47,8 @@ public interface Options {
3947
String ROUTER_PREFIX = "jooby.routerPrefix";
4048
String ROUTER_SUFFIX = "jooby.routerSuffix";
4149
String INCREMENTAL = "jooby.incremental";
50+
String RETURN_TYPE = "jooby.returnType";
51+
String MVC_METHOD = "jooby.mvcMethod";
4252
String SERVICES = "jooby.services";
4353
String SKIP_ATTRIBUTE_ANNOTATIONS = "jooby.skipAttributeAnnotations";
4454

@@ -49,7 +59,9 @@ static boolean boolOpt(ProcessingEnvironment environment, String option, boolean
4959

5060
static List<String> stringListOpt(ProcessingEnvironment environment, String option) {
5161
String value = string(environment, option, null);
52-
return value == null || value.isEmpty() ? List.of() : List.of(value.split(","));
62+
return value == null || value.isEmpty()
63+
? List.of()
64+
: Stream.of(value.split(",")).filter(it -> !it.isBlank()).map(String::trim).toList();
5365
}
5466

5567
static String string(ProcessingEnvironment environment, String option, String defaultValue) {
@@ -307,7 +319,7 @@ public Set<String> getSupportedOptions() {
307319
options.add(
308320
String.format(
309321
"org.gradle.annotation.processing.%s",
310-
context.isServices() ? "aggregating" : "isolating"));
322+
context.generateServices() ? "aggregating" : "isolating"));
311323
}
312324

313325
return options;

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcContext.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ private record ResultType(String type, String handler, boolean nonBlocking) {}
3232
private final String routerSuffix;
3333
private final Consumer<String> output;
3434
private final List<MvcRouter> routers = new ArrayList<>();
35+
private final boolean returnType;
36+
private final boolean mvcMethod;
3537
private final Map<TypeElement, ResultType> handler = new HashMap<>();
3638

3739
public MvcContext(ProcessingEnvironment processingEnvironment, Consumer<String> output) {
3840
this.processingEnvironment = processingEnvironment;
3941
this.output = output;
4042
this.debug = Options.boolOpt(processingEnvironment, Options.DEBUG, false);
4143
this.incremental = Options.boolOpt(processingEnvironment, Options.INCREMENTAL, true);
44+
this.returnType = Options.boolOpt(processingEnvironment, Options.RETURN_TYPE, true);
45+
this.mvcMethod = Options.boolOpt(processingEnvironment, Options.MVC_METHOD, true);
4246
this.services = Options.boolOpt(processingEnvironment, Options.SERVICES, true);
4347
this.routerPrefix = Options.string(processingEnvironment, Options.ROUTER_PREFIX, "");
4448
this.routerSuffix = Options.string(processingEnvironment, Options.ROUTER_SUFFIX, "_");
@@ -240,12 +244,16 @@ public boolean generateServices() {
240244
return services;
241245
}
242246

243-
public boolean isIncremental() {
244-
return incremental;
247+
public boolean generateMvcMethod() {
248+
return mvcMethod;
245249
}
246250

247-
public boolean isServices() {
248-
return services;
251+
public boolean generateReturnType() {
252+
return returnType;
253+
}
254+
255+
public boolean isIncremental() {
256+
return incremental;
249257
}
250258

251259
public void debug(String message, Object... args) {

modules/jooby-apt/src/main/java/io/jooby/internal/apt/MvcRoute.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,33 @@ public List<String> generateMapping(boolean kt) {
142142
.toSourceCode(kt, this, 2)
143143
.ifPresent(
144144
attributes -> block.add(statement(indent(2), ".setAttributes(", attributes, ")")));
145-
/* returnType */
146-
block.add(statement(indent(2), ".setReturnType(", returnType.toSourceCode(kt), ")"));
147-
/* mvcMethod */
145+
if (context.generateReturnType()) {
146+
/* returnType */
147+
block.add(statement(indent(2), ".setReturnType(", returnType.toSourceCode(kt), ")"));
148+
}
148149
var lineSep = lastLine ? lineSeparator() : lineSeparator() + lineSeparator();
149-
block.add(
150-
CodeBlock.of(
151-
indent(2),
152-
".setMvcMethod(",
153-
router.getTargetType().getSimpleName(),
154-
clazz(kt),
155-
".getMethod(",
156-
string(getMethodName()),
157-
paramString.isEmpty() ? "" : ", " + paramString,
158-
"))",
159-
semicolon(kt),
160-
lineSep));
150+
if (context.generateMvcMethod()) {
151+
/* mvcMethod */
152+
block.add(
153+
CodeBlock.of(
154+
indent(2),
155+
".setMvcMethod(",
156+
router.getTargetType().getSimpleName(),
157+
clazz(kt),
158+
".getMethod(",
159+
string(getMethodName()),
160+
paramString.isEmpty() ? "" : ", " + paramString,
161+
"))",
162+
semicolon(kt),
163+
lineSep));
164+
} else {
165+
var lastStatement = block.get(block.size() - 1);
166+
if (lastStatement.endsWith(lineSeparator())) {
167+
lastStatement =
168+
lastStatement.substring(0, lastStatement.length() - lineSeparator().length());
169+
}
170+
block.set(block.size() - 1, lastStatement + semicolon(kt) + lineSep);
171+
}
161172
}
162173
}
163174
return block;

0 commit comments

Comments
 (0)