Skip to content

Commit d59586a

Browse files
committed
detect avaje
1 parent 7146869 commit d59586a

File tree

7 files changed

+29
-11
lines changed

7 files changed

+29
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Constants {
99
public static final String FACTORY_SUFFIX = "$Factory";
1010

1111
static final String OPENAPIDEFINITION = "io.swagger.v3.oas.annotations.OpenAPIDefinition";
12+
static final String COMPONENT = "io.avaje.inject.Component";
1213

1314
static final String SINGLETON = "jakarta.inject.Singleton";
1415

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ protected void addImports(boolean withSingleton) {
6767
importTypes.add(Constants.VALIDATOR);
6868
}
6969
if (withSingleton) {
70-
importTypes.add(
71-
ctx.useJavax() ? Constants.SINGLETON.replace("jakarta", "javax") : Constants.SINGLETON);
70+
if (ctx.isAvajeAvailable()) {
71+
importTypes.add(Constants.COMPONENT);
72+
} else {
73+
importTypes.add(
74+
ctx.useJavax() ? Constants.SINGLETON.replace("jakarta", "javax") : Constants.SINGLETON);
75+
}
7276
}
7377
}
7478

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.avaje.http.generator.core;
22

3-
import io.avaje.http.generator.core.openapi.DocContext;
3+
import java.io.IOException;
44

55
import javax.annotation.processing.Filer;
66
import javax.annotation.processing.Messager;
@@ -15,7 +15,8 @@
1515
import javax.tools.FileObject;
1616
import javax.tools.JavaFileObject;
1717
import javax.tools.StandardLocation;
18-
import java.io.IOException;
18+
19+
import io.avaje.http.generator.core.openapi.DocContext;
1920

2021
public class ProcessingContext {
2122

@@ -26,7 +27,9 @@ public class ProcessingContext {
2627
private final Types types;
2728
private final boolean openApiAvailable;
2829
private final DocContext docContext;
30+
private final boolean avajeAvailable;
2931
private final boolean useJavax;
32+
private final String diAnnotation;
3033

3134
public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter) {
3235
this.readAdapter = readAdapter;
@@ -35,8 +38,10 @@ public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter)
3538
this.elements = env.getElementUtils();
3639
this.types = env.getTypeUtils();
3740
this.openApiAvailable = isTypeAvailable(Constants.OPENAPIDEFINITION);
41+
this.avajeAvailable = isTypeAvailable(Constants.COMPONENT);
3842
this.docContext = new DocContext(env, openApiAvailable);
39-
this.useJavax= Boolean.parseBoolean(env.getOptions().get("useJavax"));
43+
this.useJavax = Boolean.parseBoolean(env.getOptions().get("useJavax"));
44+
this.diAnnotation = avajeAvailable ? "@Component" : "@Singleton";
4045
}
4146

4247
private boolean isTypeAvailable(String canonicalName) {
@@ -51,6 +56,14 @@ public boolean isOpenApiAvailable() {
5156
return openApiAvailable;
5257
}
5358

59+
public boolean useJavax() {
60+
return useJavax;
61+
}
62+
63+
public boolean isAvajeAvailable() {
64+
return avajeAvailable;
65+
}
66+
5467
public void logError(Element e, String msg, Object... args) {
5568
messager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e);
5669
}
@@ -89,7 +102,7 @@ public PlatformAdapter platform() {
89102
return readAdapter;
90103
}
91104

92-
public boolean useJavax() {
93-
return useJavax;
105+
public String getDiAnnotation() {
106+
return diAnnotation;
94107
}
95108
}

http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/ControllerWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void writeRoutes(List<ControllerMethodWriter> methods) {
6060

6161
private void writeClassStart() {
6262
writer.append(AT_GENERATED).eol();
63-
writer.append("@Singleton").eol();
63+
writer.append(ctx.getDiAnnotation()).eol();
6464
writer.append("public class ").append(shortName).append("$Route implements Service {").eol().eol();
6565

6666
String controllerName = "controller";

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private void writeForMethod(MethodReader method) {
6767

6868
private void writeClassStart() {
6969
writer.append(AT_GENERATED).eol();
70-
writer.append("@Singleton").eol();
70+
writer.append(ctx.getDiAnnotation()).eol();
7171
writer
7272
.append("public class ")
7373
.append(shortName)

http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void writeForMethod(MethodReader method) {
4747

4848
private void writeClassStart() {
4949
writer.append(AT_GENERATED).eol();
50-
writer.append("@Singleton").eol();
50+
writer.append(ctx.getDiAnnotation()).eol();
5151
writer.append("public class ").append(shortName).append("$Route implements Routing.Service {").eol().eol();
5252

5353
String controllerName = "controller";

http-generator-nima/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void writeRoutes(List<ControllerMethodWriter> methods) {
7777

7878
private void writeClassStart() {
7979
writer.append(AT_GENERATED).eol();
80-
writer.append("@Singleton").eol();
80+
writer.append(ctx.getDiAnnotation()).eol();
8181
writer.append("public class %s$Route implements HttpService {", shortName).eol().eol();
8282

8383
var controllerName = "controller";

0 commit comments

Comments
 (0)