Skip to content

Commit 0bc554d

Browse files
committed
core
1 parent 7412e64 commit 0bc554d

File tree

21 files changed

+711
-447
lines changed

21 files changed

+711
-447
lines changed

http-generator-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>io.avaje</groupId>
2222
<artifactId>avaje-http-generator-core</artifactId>
23-
<version>${project.version}</version>
23+
<version>1.26-SNAPSHOT</version>
2424
</dependency>
2525

2626
</dependencies>

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientProcessor.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.avaje.http.generator.client;
22

3-
import io.avaje.http.api.Client;
4-
import io.avaje.http.generator.core.ControllerReader;
5-
import io.avaje.http.generator.core.JsonBUtil;
6-
import io.avaje.http.generator.core.ProcessingContext;
3+
import java.io.IOException;
4+
import java.util.LinkedHashSet;
5+
import java.util.List;
6+
import java.util.Set;
77

88
import javax.annotation.processing.AbstractProcessor;
99
import javax.annotation.processing.ProcessingEnvironment;
@@ -13,13 +13,14 @@
1313
import javax.lang.model.element.AnnotationValue;
1414
import javax.lang.model.element.Element;
1515
import javax.lang.model.element.TypeElement;
16-
import javax.tools.FileObject;
17-
import java.io.IOException;
18-
import java.io.Writer;
19-
import java.util.LinkedHashSet;
20-
import java.util.List;
21-
import java.util.Set;
2216

17+
import io.avaje.http.api.Client;
18+
import io.avaje.http.generator.core.ControllerReader;
19+
import io.avaje.http.generator.core.JsonBUtil;
20+
import io.avaje.http.generator.core.ProcessingContext;
21+
import io.avaje.prism.GeneratePrism;
22+
23+
@GeneratePrism(Client.class)
2324
public class ClientProcessor extends AbstractProcessor {
2425

2526
private static final String METAINF_SERVICES_PROVIDER = "META-INF/services/io.avaje.http.client.HttpApiProvider";
@@ -45,7 +46,7 @@ public SourceVersion getSupportedSourceVersion() {
4546

4647
@Override
4748
public Set<String> getSupportedAnnotationTypes() {
48-
Set<String> annotations = new LinkedHashSet<>();
49+
final Set<String> annotations = new LinkedHashSet<>();
4950
annotations.add(Client.class.getCanonicalName());
5051
annotations.add(Client.Import.class.getCanonicalName());
5152
return annotations;
@@ -60,10 +61,10 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
6061

6162
@Override
6263
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) {
63-
for (Element controller : round.getElementsAnnotatedWith(Client.class)) {
64+
for (final Element controller : round.getElementsAnnotatedWith(Client.class)) {
6465
writeClient(controller);
6566
}
66-
for (Element importedElement : round.getElementsAnnotatedWith(Client.Import.class)) {
67+
for (final Element importedElement : round.getElementsAnnotatedWith(Client.Import.class)) {
6768
writeForImported(importedElement);
6869
}
6970
if (round.processingOver()) {
@@ -74,21 +75,21 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
7475

7576
private void writeServicesFile() {
7677
try {
77-
final FileObject metaInfWriter = ctx.createMetaInfWriter(METAINF_SERVICES_PROVIDER);
78-
final Writer writer = metaInfWriter.openWriter();
79-
for (String generatedClient : generatedClients) {
78+
final var metaInfWriter = ctx.createMetaInfWriter(METAINF_SERVICES_PROVIDER);
79+
final var writer = metaInfWriter.openWriter();
80+
for (final String generatedClient : generatedClients) {
8081
writer.append(generatedClient).append("$Provider\n");
8182
}
8283
writer.close();
83-
} catch (IOException e) {
84+
} catch (final IOException e) {
8485
ctx.logError(null, "Error writing services file " + e, e);
8586
}
8687
}
8788

8889
private void writeForImported(Element importedElement) {
89-
for (AnnotationMirror annotationMirror : importedElement.getAnnotationMirrors()) {
90-
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
91-
for (Object apiClassDef : (List<?>) value.getValue()) {
90+
for (final AnnotationMirror annotationMirror : importedElement.getAnnotationMirrors()) {
91+
for (final AnnotationValue value : annotationMirror.getElementValues().values()) {
92+
for (final Object apiClassDef : (List<?>) value.getValue()) {
9293
writeImported(apiClassDef.toString());
9394
}
9495
}
@@ -97,20 +98,20 @@ private void writeForImported(Element importedElement) {
9798

9899
private void writeImported(String fullName) {
99100
// trim .class suffix
100-
String apiClassName = fullName.substring(0, fullName.length() - 6);
101-
TypeElement typeElement = ctx.typeElement(apiClassName);
101+
final var apiClassName = fullName.substring(0, fullName.length() - 6);
102+
final var typeElement = ctx.typeElement(apiClassName);
102103
if (typeElement != null) {
103104
writeClient(typeElement);
104105
}
105106
}
106107

107108
private void writeClient(Element controller) {
108109
if (controller instanceof TypeElement) {
109-
ControllerReader reader = new ControllerReader((TypeElement) controller, ctx);
110+
final var reader = new ControllerReader((TypeElement) controller, ctx);
110111
reader.read(false);
111112
try {
112113
generatedClients.add(writeClientAdapter(ctx, reader));
113-
} catch (Throwable e) {
114+
} catch (final Throwable e) {
114115
e.printStackTrace();
115116
ctx.logError(reader.beanType(), "Failed to write client class " + e);
116117
}

http-generator-client/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
provides javax.annotation.processing.Processor with io.avaje.http.generator.client.ClientProcessor;
44

55
requires transitive io.avaje.http.generator.core;
6+
67
requires java.compiler;
78
}

http-generator-core/pom.xml

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,58 @@
1717

1818
<dependencies>
1919

20-
<dependency>
21-
<groupId>jakarta.inject</groupId>
22-
<artifactId>jakarta.inject-api</artifactId>
23-
<version>2.0.0</version>
20+
21+
<dependency>
22+
<groupId>io.avaje</groupId>
23+
<artifactId>avaje-prisms</artifactId>
24+
<version>1.3-SNAPSHOT</version>
25+
<scope>provided</scope>
2426
</dependency>
27+
<dependency>
28+
<groupId>jakarta.validation</groupId>
29+
<artifactId>jakarta.validation-api</artifactId>
30+
<version>3.0.2</version>
31+
<optional>true</optional>
32+
<scope>provided</scope>
33+
</dependency>
2534

2635
<dependency>
2736
<groupId>javax.validation</groupId>
2837
<artifactId>validation-api</artifactId>
2938
<version>2.0.1.Final</version>
39+
<optional>true</optional>
40+
<scope>provided</scope>
3041
</dependency>
3142

43+
<dependency>
44+
<groupId>jakarta.inject</groupId>
45+
<artifactId>jakarta.inject-api</artifactId>
46+
<version>2.0.1</version>
47+
<optional>true</optional>
48+
<scope>provided</scope>
49+
</dependency>
50+
3251
<dependency>
3352
<groupId>io.avaje</groupId>
3453
<artifactId>avaje-http-api</artifactId>
3554
<version>1.26-SNAPSHOT</version>
55+
<scope>provided</scope>
3656
</dependency>
3757

3858
<dependency>
3959
<groupId>io.swagger.core.v3</groupId>
4060
<artifactId>swagger-models</artifactId>
4161
<version>${swagger.version}</version>
62+
<optional>true</optional>
63+
<scope>provided</scope>
4264
</dependency>
4365

4466
<dependency>
4567
<groupId>io.swagger.core.v3</groupId>
4668
<artifactId>swagger-annotations</artifactId>
4769
<version>${swagger.version}</version>
70+
<optional>true</optional>
71+
<scope>provided</scope>
4872
</dependency>
4973

5074
</dependencies>
@@ -54,12 +78,14 @@
5478
<plugin>
5579
<groupId>org.apache.maven.plugins</groupId>
5680
<artifactId>maven-compiler-plugin</artifactId>
57-
<version>3.10.1</version>
5881
<configuration>
59-
<source>11</source>
60-
<target>11</target>
61-
<!-- Turn off annotation processing for building -->
62-
<compilerArgument>-proc:none</compilerArgument>
82+
<annotationProcessorPaths>
83+
<path>
84+
<groupId>io.avaje</groupId>
85+
<artifactId>avaje-prisms</artifactId>
86+
<version>1.3-SNAPSHOT</version>
87+
</path>
88+
</annotationProcessorPaths>
6389
</configuration>
6490
</plugin>
6591
</plugins>
Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package io.avaje.http.generator.core;
22

33
import java.io.IOException;
4-
import java.util.LinkedHashSet;
54
import java.util.Set;
65

76
import javax.annotation.processing.AbstractProcessor;
87
import javax.annotation.processing.ProcessingEnvironment;
98
import javax.annotation.processing.RoundEnvironment;
9+
import javax.annotation.processing.SupportedAnnotationTypes;
1010
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;
1414

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","useSingleton"})
15+
@SupportedOptions({"useJavax", "useSingleton"})
16+
@SupportedAnnotationTypes({ControllerPrism.PRISM_TYPE, OpenAPIDefinitionPrism.PRISM_TYPE})
2117
public abstract class BaseProcessor extends AbstractProcessor {
2218

2319
protected ProcessingContext ctx;
@@ -27,23 +23,13 @@ public SourceVersion getSupportedSourceVersion() {
2723
return SourceVersion.latest();
2824
}
2925

30-
@Override
31-
public Set<String> getSupportedAnnotationTypes() {
32-
Set<String> annotations = new LinkedHashSet<>();
33-
annotations.add(Controller.class.getCanonicalName());
34-
annotations.add(OpenAPIDefinition.class.getCanonicalName());
35-
return annotations;
36-
}
37-
3826
@Override
3927
public synchronized void init(ProcessingEnvironment processingEnv) {
4028
super.init(processingEnv);
4129
this.ctx = new ProcessingContext(processingEnv, providePlatformAdapter());
4230
}
4331

44-
/**
45-
* Provide the platform specific adapter to use for Javalin, Helidon etc.
46-
*/
32+
/** Provide the platform specific adapter to use for Javalin, Helidon etc. */
4733
protected abstract PlatformAdapter providePlatformAdapter();
4834

4935
@Override
@@ -54,8 +40,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
5440
readTagDefinitions(round);
5541
}
5642

57-
Set<? extends Element> controllers = round.getElementsAnnotatedWith(Controller.class);
58-
for (Element controller : controllers) {
43+
final Set<? extends Element> controllers =
44+
round.getElementsAnnotatedWith(ctx.typeElement(ControllerPrism.PRISM_TYPE));
45+
for (final Element controller : controllers) {
5946
writeControllerAdapter(controller);
6047
}
6148

@@ -66,20 +53,22 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
6653
}
6754

6855
private void readOpenApiDefinition(RoundEnvironment round) {
69-
Set<? extends Element> elements = round.getElementsAnnotatedWith(OpenAPIDefinition.class);
70-
for (Element element : elements) {
56+
final Set<? extends Element> elements =
57+
round.getElementsAnnotatedWith(ctx.typeElement(OpenAPIDefinitionPrism.PRISM_TYPE));
58+
for (final Element element : elements) {
7159
ctx.doc().readApiDefinition(element);
7260
}
7361
}
7462

7563
private void readTagDefinitions(RoundEnvironment round) {
76-
Set<? extends Element> elements = round.getElementsAnnotatedWith(Tag.class);
77-
for (Element element : elements) {
64+
Set<? extends Element> elements =
65+
round.getElementsAnnotatedWith(ctx.typeElement(TagPrism.PRISM_TYPE));
66+
for (final Element element : elements) {
7867
ctx.doc().addTagDefinition(element);
7968
}
8069

81-
elements = round.getElementsAnnotatedWith(Tags.class);
82-
for (Element element : elements) {
70+
elements = round.getElementsAnnotatedWith(ctx.typeElement(TagsPrism.PRISM_TYPE));
71+
for (final Element element : elements) {
8372
ctx.doc().addTagsDefinition(element);
8473
}
8574
}
@@ -90,20 +79,18 @@ private void writeOpenAPI() {
9079

9180
private void writeControllerAdapter(Element controller) {
9281
if (controller instanceof TypeElement) {
93-
ControllerReader reader = new ControllerReader((TypeElement) controller, ctx);
82+
final var reader = new ControllerReader((TypeElement) controller, ctx);
9483
reader.read(true);
9584
try {
9685
writeControllerAdapter(ctx, reader);
97-
} catch (Throwable e) {
86+
} catch (final Throwable e) {
9887
e.printStackTrace();
9988
ctx.logError(reader.beanType(), "Failed to write $Route class " + e);
10089
}
10190
}
10291
}
10392

104-
/**
105-
* Write the adapter code for the given controller.
106-
*/
107-
public abstract void writeControllerAdapter(ProcessingContext ctx, ControllerReader reader) throws IOException;
108-
93+
/** Write the adapter code for the given controller. */
94+
public abstract void writeControllerAdapter(ProcessingContext ctx, ControllerReader reader)
95+
throws IOException;
10996
}

0 commit comments

Comments
 (0)