Skip to content

Commit 798ca73

Browse files
authored
Merge pull request #118 from SentryMan/master
Use @singleton again
2 parents fc44373 + 22a0781 commit 798ca73

File tree

9 files changed

+73
-21
lines changed

9 files changed

+73
-21
lines changed

README.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ If there are other annotation processors and they are specified via <i>maven-com
6868
</configuration>
6969
</plugin>
7070
```
71+
7172
## Define a Controller (These APT processors work with both Java and Kotlin.)
7273
```java
7374
package org.example.hello;
@@ -98,11 +99,23 @@ public class WidgetController {
9899
record Widget(int id, String name){};
99100
}
100101
```
102+
## DI Usage
103+
The annotation processor will generate controller adapters that can register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the below examples do and they use [Avaje-Inject](https://avaje.io/inject/) to do this. The AP will automatically detect the presence of avaje-inject and generate the class to use avaje-inject's `@Component` as the DI annotation.
101104

102-
## Usage
103-
The annotation processor will generate controller adapters that can register routes to Javalin/Helidon. The natural way to use the generated adapters is to get a DI library to find and wire them. This is what the below examples do and they use [Avaje-Inject](https://avaje.io/inject/) to do this.
105+
There isn't a hard requirement to use Avaje for dependency injection. In the absence of avaje-inject the generated class will use `@jakarta.inject.Singleton` or `@javax.inject.Singleton` depending on what's on the classpath. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
104106

105-
Note that there isn't a requirement to use Avaje for dependency injection. Any DI library that can find and wire the generated @Singleton beans can be used. You can even use Dagger2 or Guice to wire the controllers if you so desire.
107+
To force the AP to generate with `@javax.inject.Singleton`(in the case where you have both jakarta and javax on the classpath), use the compiler arg `-AuseJavax=true`
108+
```
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<configuration>
113+
<compilerArgs>
114+
<arg>-AuseJavax=true</arg>
115+
</compilerArgs>
116+
</configuration>
117+
</plugin>
118+
```
106119

107120
### Usage with Javalin
108121

@@ -156,7 +169,7 @@ WebServer.builder()
156169

157170
```java
158171
@Generated("avaje-javalin-generator")
159-
@Component
172+
@Singleton
160173
public class WidgetController$Route implements WebRoutes {
161174

162175
private final WidgetController controller;
@@ -188,7 +201,7 @@ public class WidgetController$Route implements WebRoutes {
188201

189202
### (Helidon SE) The generated WidgetController$Route.java is:
190203
```java
191-
@Generated("io.dinject.helidon-generator")
204+
@Generated("avaje-helidon-generator")
192205
@Singleton
193206
public class WidgetController$Route implements Service {
194207

@@ -221,7 +234,7 @@ public class WidgetController$Route implements Service {
221234

222235
```java
223236
@Generated("avaje-helidon-nima-generator")
224-
@Component
237+
@Singleton
225238
public class WidgetController$Route implements HttpService {
226239

227240
private final WidgetController controller;

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package io.avaje.http.generator.core;
22

3-
import io.avaje.http.api.Controller;
4-
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
5-
import io.swagger.v3.oas.annotations.tags.Tag;
6-
import io.swagger.v3.oas.annotations.tags.Tags;
3+
import java.io.IOException;
4+
import java.util.LinkedHashSet;
5+
import java.util.Set;
76

87
import javax.annotation.processing.AbstractProcessor;
98
import javax.annotation.processing.ProcessingEnvironment;
109
import javax.annotation.processing.RoundEnvironment;
10+
import javax.annotation.processing.SupportedOptions;
1111
import javax.lang.model.SourceVersion;
1212
import javax.lang.model.element.Element;
1313
import javax.lang.model.element.TypeElement;
14-
import java.io.IOException;
15-
import java.util.LinkedHashSet;
16-
import java.util.Set;
1714

15+
import io.avaje.http.api.Controller;
16+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
17+
import io.swagger.v3.oas.annotations.tags.Tag;
18+
import io.swagger.v3.oas.annotations.tags.Tags;
19+
20+
@SupportedOptions({"useJavax"})
1821
public abstract class BaseProcessor extends AbstractProcessor {
1922

2023
protected ProcessingContext ctx;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ public class Constants {
99
public static final String FACTORY_SUFFIX = "$Factory";
1010

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

14+
static final String SINGLETON_JAKARTA = "jakarta.inject.Singleton";
15+
static final String SINGLETON_JAVAX = "javax.inject.Singleton";
16+
1517
static final String IMPORT_PATH_TYPE_CONVERT = "import static io.avaje.http.api.PathTypeConversion.*;";
1618

1719
static final String IMPORT_HTTP_API = "io.avaje.http.api.*";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ protected void addImports(boolean withSingleton) {
6767
importTypes.add(Constants.VALIDATOR);
6868
}
6969
if (withSingleton) {
70-
importTypes.add(Constants.COMPONENT);
70+
if (ctx.useComponent()) {
71+
importTypes.add(Constants.COMPONENT);
72+
} else {
73+
importTypes.add(ctx.useJavax() ? Constants.SINGLETON_JAVAX : Constants.SINGLETON_JAKARTA);
74+
}
7175
}
7276
}
7377

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

Lines changed: 32 additions & 2 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,6 +27,9 @@ public class ProcessingContext {
2627
private final Types types;
2728
private final boolean openApiAvailable;
2829
private final DocContext docContext;
30+
private final boolean useComponent;
31+
private final boolean useJavax;
32+
private final String diAnnotation;
2933

3034
public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter) {
3135
this.readAdapter = readAdapter;
@@ -35,6 +39,20 @@ public ProcessingContext(ProcessingEnvironment env, PlatformAdapter readAdapter)
3539
this.types = env.getTypeUtils();
3640
this.openApiAvailable = isTypeAvailable(Constants.OPENAPIDEFINITION);
3741
this.docContext = new DocContext(env, openApiAvailable);
42+
this.useComponent = isTypeAvailable(Constants.COMPONENT);
43+
this.diAnnotation = useComponent ? "@Component" : "@Singleton";
44+
45+
final var javax = isTypeAvailable(Constants.SINGLETON_JAVAX);
46+
final var jakarta = isTypeAvailable(Constants.SINGLETON_JAKARTA);
47+
final var override = Boolean.getBoolean(env.getOptions().get("useJavax"));
48+
49+
if (override || (javax && jakarta)) {
50+
this.useJavax = override;
51+
} else if (javax) {
52+
this.useJavax = true;
53+
} else {
54+
useJavax = false;
55+
}
3856
}
3957

4058
private boolean isTypeAvailable(String canonicalName) {
@@ -49,6 +67,14 @@ public boolean isOpenApiAvailable() {
4967
return openApiAvailable;
5068
}
5169

70+
public boolean useJavax() {
71+
return useJavax;
72+
}
73+
74+
public boolean useComponent() {
75+
return useComponent;
76+
}
77+
5278
public void logError(Element e, String msg, Object... args) {
5379
messager.printMessage(Diagnostic.Kind.ERROR, String.format(msg, args), e);
5480
}
@@ -86,4 +112,8 @@ public TypeMirror asMemberOf(DeclaredType declaredType, Element element) {
86112
public PlatformAdapter platform() {
87113
return readAdapter;
88114
}
115+
116+
public String getDIAnnotation() {
117+
return diAnnotation;
118+
}
89119
}

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("@Component").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("@Component").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("@Component").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("@Component").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)