Skip to content

Commit f51b1df

Browse files
committed
fix flag
1 parent c9f04dd commit f51b1df

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/BaseProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import io.swagger.v3.oas.annotations.tags.Tag;
1818
import io.swagger.v3.oas.annotations.tags.Tags;
1919

20-
@SupportedOptions({"useJavax"})
20+
@SupportedOptions({"useJavax","useSingleton"})
2121
public abstract class BaseProcessor extends AbstractProcessor {
2222

2323
protected ProcessingContext ctx;

http-generator-core/src/main/java/io/avaje/http/generator/core/ProcessingContext.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,34 @@ public class ProcessingContext {
3232
private final String diAnnotation;
3333

3434
public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter) {
35-
this.readAdapter = readAdapter;
35+
36+
this.readAdapter = readAdapter;
3637
this.messager = env.getMessager();
3738
this.filer = env.getFiler();
3839
this.elements = env.getElementUtils();
3940
this.types = env.getTypeUtils();
4041
this.openApiAvailable = isTypeAvailable(Constants.OPENAPIDEFINITION);
4142
this.docContext = new DocContext(env, openApiAvailable);
42-
this.useComponent = isTypeAvailable(Constants.COMPONENT);
43+
44+
final var options = env.getOptions();
45+
final var singletonOverride = options.get("useSingleton");
46+
47+
if (singletonOverride != null) {
48+
this.useComponent = !Boolean.parseBoolean(singletonOverride);
49+
} else {
50+
this.useComponent = isTypeAvailable(Constants.COMPONENT);
51+
}
52+
4353
this.diAnnotation = useComponent ? "@Component" : "@Singleton";
4454

4555
final var javax = isTypeAvailable(Constants.SINGLETON_JAVAX);
4656
final var jakarta = isTypeAvailable(Constants.SINGLETON_JAKARTA);
47-
final var override = Boolean.getBoolean(env.getOptions().get("useJavax"));
57+
final var override = env.getOptions().get("useJavax");
4858

49-
if (override || (javax && jakarta)) {
50-
this.useJavax = override;
51-
} else if (javax) {
52-
this.useJavax = true;
59+
if (override != null || (javax && jakarta)) {
60+
this.useJavax = Boolean.parseBoolean(override);
61+
} else if (javax && !jakarta) {
62+
useJavax = javax;
5363
} else {
5464
useJavax = false;
5565
}

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/JsonFormatter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public static String prettyPrintJson(Writer writer, String json) throws IOExcept
4040
writer.append(current).append("\n");
4141
addIndents(writer, indentLevel);
4242
break;
43-
case ':':
44-
writer.append(" :");
45-
break;
4643
default:
4744
writer.append(current);
4845
break;

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/OpenAPISerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static String serialize(Object obj) throws IllegalAccessException {
5050
}
5151
sb.append("\"");
5252
sb.append(entry.getKey());
53-
sb.append("\": ");
53+
sb.append("\" : ");
5454

5555
write(sb, entry.getValue());
5656
firstElement = false;
@@ -75,7 +75,7 @@ static String serialize(Object obj) throws IllegalAccessException {
7575
}
7676
sb.append("\"");
7777
sb.append(field.getName());
78-
sb.append("\": ");
78+
sb.append("\" : ");
7979
write(sb, value);
8080
firstField = false;
8181
}

tests/test-javalin-jsonb/src/test/java/io/avaje/http/generator/JavalinProcessorTest.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void deleteGeneratedFiles() throws IOException {
4141
}
4242

4343
@Test
44-
public void runAnnoationProcessor() throws Exception {
44+
public void runAnnotationProcessor() throws Exception {
4545
final var source = Paths.get("src").toAbsolutePath().toString();
4646

4747
final var files = getSourceFiles(source);
@@ -54,10 +54,13 @@ public void runAnnoationProcessor() throws Exception {
5454
task.setProcessors(List.of(new JavalinProcessor()));
5555

5656
assertThat(task.call()).isTrue();
57+
assert Files.readString(
58+
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
59+
.contains("io.avaje.inject.Component");
5760
}
5861

5962
@Test
60-
public void runAnnoationProcessorJsonB() throws Exception {
63+
public void runAnnotationProcessorJsonB() throws Exception {
6164
final var source = Paths.get("src").toAbsolutePath().toString();
6265

6366
final var files = getSourceFiles(source);
@@ -67,9 +70,62 @@ public void runAnnoationProcessorJsonB() throws Exception {
6770
final var task =
6871
compiler.getTask(
6972
new PrintWriter(System.out), null, null, List.of("--release=11"), null, files);
73+
task.setProcessors(List.of(new JavalinProcessor(true), new Processor()));
74+
75+
assertThat(task.call()).isTrue();
76+
assert Files.readString(
77+
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
78+
.contains("io.avaje.jsonb.Jsonb");
79+
}
80+
81+
@Test
82+
public void runAnnotationProcessorJavax() throws Exception {
83+
final var source = Paths.get("src").toAbsolutePath().toString();
84+
85+
final var files = getSourceFiles(source);
86+
87+
final var compiler = ToolProvider.getSystemJavaCompiler();
88+
89+
final var task =
90+
compiler.getTask(
91+
new PrintWriter(System.out),
92+
null,
93+
null,
94+
List.of("--release=11", "-AuseJavax=true", "-AuseSingleton=true"),
95+
null,
96+
files);
97+
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
98+
// we don't have javax on the cp
99+
assertThat(task.call()).isFalse();
100+
101+
assert Files.readString(
102+
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
103+
.contains("javax.inject.Singleton");
104+
}
105+
106+
@Test
107+
public void runAnnotationProcessorJakarta() throws Exception {
108+
final var source = Paths.get("src").toAbsolutePath().toString();
109+
110+
final var files = getSourceFiles(source);
111+
112+
final var compiler = ToolProvider.getSystemJavaCompiler();
113+
114+
final var task =
115+
compiler.getTask(
116+
new PrintWriter(System.out),
117+
null,
118+
null,
119+
List.of("--release=11", "-AuseJavax=false", "-AuseSingleton=true"),
120+
null,
121+
files);
70122
task.setProcessors(List.of(new JavalinProcessor(false), new Processor()));
71123

72124
assertThat(task.call()).isTrue();
125+
126+
assert Files.readString(
127+
Paths.get("org/example/myapp/web/BarController$Route.java").toAbsolutePath())
128+
.contains("jakarta.inject.Singleton");
73129
}
74130

75131
@Test

0 commit comments

Comments
 (0)