Skip to content

Commit 7874f66

Browse files
committed
optional
perhaps I like optional too much
1 parent 32849cd commit 7874f66

File tree

8 files changed

+104
-60
lines changed

8 files changed

+104
-60
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>1.26-SNAPSHOT</version>
23+
<version>${project.version}</version>
2424
</dependency>
2525

2626
</dependencies>

http-generator-core/dependency-reduced-pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<path>
1717
<groupId>io.avaje</groupId>
1818
<artifactId>avaje-prisms</artifactId>
19-
<version>1.3-SNAPSHOT</version>
19+
<version>1.3</version>
2020
</path>
2121
</annotationProcessorPaths>
2222
</configuration>
@@ -27,7 +27,7 @@
2727
<dependency>
2828
<groupId>io.avaje</groupId>
2929
<artifactId>avaje-prisms</artifactId>
30-
<version>1.3-SNAPSHOT</version>
30+
<version>1.3</version>
3131
<scope>provided</scope>
3232
<optional>true</optional>
3333
</dependency>

http-generator-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<dependency>
2222
<groupId>io.avaje</groupId>
2323
<artifactId>avaje-prisms</artifactId>
24-
<version>1.3-SNAPSHOT</version>
24+
<version>1.3</version>
2525
<optional>true</optional>
2626
<scope>provided</scope>
2727
</dependency>
@@ -92,7 +92,7 @@
9292
<path>
9393
<groupId>io.avaje</groupId>
9494
<artifactId>avaje-prisms</artifactId>
95-
<version>1.3-SNAPSHOT</version>
95+
<version>1.3</version>
9696
</path>
9797
</annotationProcessorPaths>
9898
</configuration>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import javax.lang.model.element.TypeElement;
1414

1515
@SupportedOptions({"useJavax", "useSingleton"})
16-
@SupportedAnnotationTypes({ControllerPrism.PRISM_TYPE, OpenAPIDefinitionPrism.PRISM_TYPE})
1716
public abstract class BaseProcessor extends AbstractProcessor {
1817

1918
protected ProcessingContext ctx;

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,49 +120,48 @@ private List<ExecutableElement> initInterfaceMethods() {
120120
return ifaceMethods;
121121
}
122122

123-
private <A> A findAnnotation(Function<Element, A> func) {
123+
private <A> Optional<A> findAnnotation(Function<Element, Optional<A>> func) {
124124
var annotation = func.apply(beanType);
125-
if (annotation != null) {
125+
if (annotation.isPresent()) {
126126
return annotation;
127127
}
128128
for (final Element anInterface : interfaces) {
129129
annotation = func.apply(anInterface);
130-
if (annotation != null) {
130+
if (annotation.isPresent()) {
131131
return annotation;
132132
}
133133
}
134-
return null;
134+
return Optional.empty();
135135
}
136136

137-
<A> A findMethodAnnotation(Function<Element, A> func, ExecutableElement element) {
137+
<A> Optional<A> findMethodAnnotation(Function<Element, Optional<A>> func, ExecutableElement element) {
138138
for (final ExecutableElement interfaceMethod : interfaceMethods) {
139139
if (matchMethod(interfaceMethod, element)) {
140140
final var annotation = func.apply(interfaceMethod);
141-
if (annotation != null) {
141+
if (annotation.isPresent()) {
142142
return annotation;
143143
}
144144
}
145145
}
146-
return null;
146+
return Optional.empty();
147147
}
148148

149149
private boolean matchMethod(ExecutableElement interfaceMethod, ExecutableElement element) {
150150
return interfaceMethod.toString().equals(element.toString());
151151
}
152152

153153
private String initProduces() {
154-
final var produces = findAnnotation(ProducesPrism::getInstanceOn);
155-
return (produces == null) ? null : produces.value();
154+
return findAnnotation(ProducesPrism::getOptionalOn).map(ProducesPrism::value).orElse(null);
156155
}
157156

158157
private boolean initDocHidden() {
159-
return findAnnotation(HiddenPrism::getInstanceOn) != null;
158+
return findAnnotation(HiddenPrism::getOptionalOn).isPresent();
160159
}
161160

162161
private boolean initHasValid() {
163162

164-
return findAnnotation(JavaxValidPrism::getInstanceOn) != null
165-
|| findAnnotation(ValidPrism::getInstanceOn) != null;
163+
return findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
164+
|| findAnnotation(ValidPrism::getOptionalOn).isPresent();
166165
}
167166

168167
String produces() {
@@ -281,12 +280,12 @@ public List<OpenAPIResponsePrism> openApiResponses() {
281280

282281
public String path() {
283282

284-
return Optional.ofNullable(findAnnotation(ControllerPrism::getInstanceOn))
283+
return findAnnotation(ControllerPrism::getOptionalOn)
285284
.map(ControllerPrism::value)
286285
.filter(not(String::isBlank))
287286
.or(
288287
() ->
289-
Optional.ofNullable(findAnnotation(PathPrism::getInstanceOn)).map(PathPrism::value))
288+
findAnnotation(PathPrism::getOptionalOn).map(PathPrism::value))
290289
.map(Util::trimPath)
291290
.orElse(null);
292291
}

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

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class MethodReader {
5353
this.actualParams = (actualExecutable == null) ? null : actualExecutable.getParameterTypes();
5454
this.isVoid = element.getReturnType().getKind() == TypeKind.VOID;
5555
this.methodRoles = Util.findRoles(element);
56-
this.producesAnnotation = Optional.ofNullable(findAnnotation(ProducesPrism::getInstanceOn));
56+
this.producesAnnotation = findAnnotation(ProducesPrism::getOptionalOn);
5757
initWebMethodViaAnnotation();
5858

5959
this.superMethods =
@@ -85,17 +85,17 @@ private Javadoc buildJavadoc(ExecutableElement element, ProcessingContext ctx) {
8585
}
8686

8787
private boolean initValid() {
88-
return findAnnotation(ValidPrism::getInstanceOn) != null
89-
|| findAnnotation(JavaxValidPrism::getInstanceOn) != null
88+
return findAnnotation(ValidPrism::getOptionalOn).isPresent()
89+
|| findAnnotation(JavaxValidPrism::getOptionalOn).isPresent()
9090
|| superMethodHasValid();
9191
}
9292

9393
private boolean superMethodHasValid() {
9494
return superMethods.stream()
9595
.anyMatch(
9696
e ->
97-
findAnnotation(ValidPrism::getInstanceOn) != null
98-
|| findAnnotation(JavaxValidPrism::getInstanceOn) != null);
97+
findAnnotation(ValidPrism::getOptionalOn).isPresent()
98+
|| findAnnotation(JavaxValidPrism::getOptionalOn).isPresent());
9999
}
100100

101101
@Override
@@ -104,34 +104,24 @@ public String toString() {
104104
}
105105

106106
private void initWebMethodViaAnnotation() {
107-
final var form = findAnnotation(FormPrism::getInstanceOn);
108-
if (form != null) {
107+
108+
if (findAnnotation(FormPrism::getOptionalOn).isPresent()) {
109109
this.formMarker = true;
110110
}
111-
final var get = findAnnotation(GetPrism::getInstanceOn);
112-
if (get != null) {
113-
initSetWebMethod(WebMethod.GET, get.value());
114-
return;
115-
}
116-
final var put = findAnnotation(PutPrism::getInstanceOn);
117-
if (put != null) {
118-
initSetWebMethod(WebMethod.PUT, put.value());
119-
return;
120-
}
121-
final var post = findAnnotation(PostPrism::getInstanceOn);
122-
if (post != null) {
123-
initSetWebMethod(WebMethod.POST, post.value());
124-
return;
125-
}
126-
final var patch = findAnnotation(PatchPrism::getInstanceOn);
127-
if (patch != null) {
128-
initSetWebMethod(WebMethod.PATCH, patch.value());
129-
return;
130-
}
131-
final var delete = findAnnotation(DeletePrism::getInstanceOn);
132-
if (delete != null) {
133-
initSetWebMethod(WebMethod.DELETE, delete.value());
134-
}
111+
112+
findAnnotation(GetPrism::getOptionalOn)
113+
.ifPresent(get -> initSetWebMethod(WebMethod.GET, get.value()));
114+
115+
findAnnotation(PutPrism::getOptionalOn)
116+
.ifPresent(put -> initSetWebMethod(WebMethod.PUT, put.value()));
117+
118+
findAnnotation(PostPrism::getOptionalOn)
119+
.ifPresent(post -> initSetWebMethod(WebMethod.POST, post.value()));
120+
121+
findAnnotation(PatchPrism::getOptionalOn)
122+
.ifPresent(patch -> initSetWebMethod(WebMethod.PATCH, patch.value()));
123+
findAnnotation(DeletePrism::getOptionalOn)
124+
.ifPresent(delete -> initSetWebMethod(WebMethod.DELETE, delete.value()));
135125
}
136126

137127
private void initSetWebMethod(WebMethod webMethod, String value) {
@@ -144,8 +134,9 @@ public Javadoc javadoc() {
144134
}
145135

146136
private List<OpenAPIResponsePrism> buildApiResponses() {
137+
147138
final var container =
148-
Optional.ofNullable(findAnnotation(OpenAPIResponsesPrism::getInstanceOn)).stream()
139+
findAnnotation(OpenAPIResponsesPrism::getOptionalOn).stream()
149140
.map(OpenAPIResponsesPrism::value)
150141
.flatMap(List::stream);
151142

@@ -157,9 +148,7 @@ private List<OpenAPIResponsePrism> buildApiResponses() {
157148
.flatMap(
158149
method ->
159150
Stream.concat(
160-
Optional.ofNullable(
161-
findAnnotation(OpenAPIResponsesPrism::getInstanceOn, method))
162-
.stream()
151+
findAnnotation(OpenAPIResponsesPrism::getOptionalOn, method).stream()
163152
.map(OpenAPIResponsesPrism::value)
164153
.flatMap(List::stream),
165154
OpenAPIResponsePrism.getAllInstancesOn(method).stream()));
@@ -168,16 +157,18 @@ private List<OpenAPIResponsePrism> buildApiResponses() {
168157
Stream.concat(methodResponses, superMethodResponses).collect(Collectors.toList());
169158

170159
responses.addAll(bean.openApiResponses());
160+
171161
return responses;
172162
}
173163

174-
public <A> A findAnnotation(Function<Element, A> prismFunc) {
164+
public <A> Optional<A> findAnnotation(Function<Element, Optional<A>> prismFunc) {
175165
return findAnnotation(prismFunc, element);
176166
}
177167

178-
public <A> A findAnnotation(Function<Element, A> prismFunc, ExecutableElement elem) {
168+
public <A> Optional<A> findAnnotation(
169+
Function<Element, Optional<A>> prismFunc, ExecutableElement elem) {
179170
final var annotation = prismFunc.apply(elem);
180-
if (annotation != null) {
171+
if (annotation.isPresent()) {
181172
return annotation;
182173
}
183174
return bean.findMethodAnnotation(prismFunc, elem);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public MethodDocBuilder(MethodReader methodReader, DocContext ctx) {
2828
public void build() {
2929

3030
if (ctx.isOpenApiAvailable()
31-
&& methodReader.findAnnotation(HiddenPrism::getInstanceOn) != null) {
31+
&& methodReader.findAnnotation(HiddenPrism::getOptionalOn).isPresent()) {
3232
return;
3333
}
3434

@@ -38,7 +38,7 @@ public void build() {
3838
operation.setTags(methodReader.tags());
3939

4040
if (javadoc.isDeprecated()
41-
|| (methodReader.findAnnotation(DeprecatedPrism::getInstanceOn) != null)) {
41+
|| methodReader.findAnnotation(DeprecatedPrism::getOptionalOn).isPresent()) {
4242
operation.setDeprecated(true);
4343
}
4444

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<parent>
4+
<artifactId>avaje-http-parent</artifactId>
5+
<groupId>io.avaje</groupId>
6+
<version>1.26-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<artifactId>avaje-http-nima-generator</artifactId>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<artifactId>maven-compiler-plugin</artifactId>
14+
<version>3.10.1</version>
15+
<configuration>
16+
<source>19</source>
17+
<target>19</target>
18+
<compilerArgument>-proc:none</compilerArgument>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
<dependencies>
24+
<dependency>
25+
<groupId>io.avaje</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>1.1</version>
28+
<scope>test</scope>
29+
<exclusions>
30+
<exclusion>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<groupId>org.junit.jupiter</groupId>
33+
</exclusion>
34+
<exclusion>
35+
<artifactId>junit-jupiter-engine</artifactId>
36+
<groupId>org.junit.jupiter</groupId>
37+
</exclusion>
38+
<exclusion>
39+
<artifactId>assertj-core</artifactId>
40+
<groupId>org.assertj</groupId>
41+
</exclusion>
42+
<exclusion>
43+
<artifactId>mockito-core</artifactId>
44+
<groupId>org.mockito</groupId>
45+
</exclusion>
46+
</exclusions>
47+
</dependency>
48+
</dependencies>
49+
<properties>
50+
<maven.compiler.target>19</maven.compiler.target>
51+
<maven.compiler.source>19</maven.compiler.source>
52+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
53+
<java.release>19</java.release>
54+
</properties>
55+
</project>

0 commit comments

Comments
 (0)