Skip to content

Commit 5fa2297

Browse files
committed
client processor jsonb generics
1 parent a155061 commit 5fa2297

File tree

5 files changed

+115
-34
lines changed

5 files changed

+115
-34
lines changed

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.lang.model.element.TypeElement;
66
import java.util.Set;
7+
import java.util.stream.Collectors;
78

89
/**
910
* Write code to register Web route for a given controller method.
@@ -22,13 +23,15 @@ class ClientMethodWriter {
2223
private final UType returnType;
2324
private MethodParam bodyHandlerParam;
2425
private String methodGenericParams = "";
26+
private final boolean useJsonb;
2527

26-
ClientMethodWriter(MethodReader method, Append writer, ProcessingContext ctx) {
28+
ClientMethodWriter(MethodReader method, Append writer, ProcessingContext ctx, boolean useJsonb) {
2729
this.method = method;
2830
this.writer = writer;
2931
this.webMethod = method.webMethod();
3032
this.ctx = ctx;
3133
this.returnType = Util.parseType(method.returnType());
34+
this.useJsonb = useJsonb;
3235
}
3336

3437
void addImportTypes(ControllerReader reader) {
@@ -111,37 +114,55 @@ private void writeEnd() {
111114

112115
private void writeSyncResponse() {
113116
writer.append(" ");
114-
String type0 = returnType.mainType();
115-
String type1 = returnType.param0();
116-
writeResponse(type0, type1);
117+
writeResponse(returnType);
117118
}
118119

119120
private void writeAsyncResponse() {
120121
writer.append(" .async()");
121-
String type0 = returnType.param0();
122-
String type1 = returnType.param1();
123-
writeResponse(type0, type1);
122+
writeResponse(UType.parse(returnType.param0()));
124123
}
125124

126125
private void writeCallResponse() {
127126
writer.append(" .call()");
128-
String type0 = returnType.param0();
129-
String type1 = returnType.param1();
130-
writeResponse(type0, type1);
127+
writeResponse(UType.parse(returnType.param0()));
131128
}
132129

133-
private void writeResponse(String type0, String type1) {
134-
if (isList(type0)) {
135-
writer.append(".list(%s.class);", Util.shortName(type1)).eol();
136-
} else if (isStream(type0)) {
137-
writer.append(".stream(%s.class);", Util.shortName(type1)).eol();
138-
} else if (isHttpResponse(type0)){
130+
private void writeResponse(UType type) {
131+
final var mainType = type.mainType();
132+
final var param1 = type.paramRaw();
133+
if (isList(mainType)) {
134+
writer.append(".list(");
135+
writeGeneric(UType.parse(param1));
136+
} else if (isStream(mainType)) {
137+
writer.append(".stream(");
138+
writeGeneric(UType.parse(param1));
139+
} else if (isHttpResponse(mainType)) {
139140
writeWithHandler();
140141
} else {
141-
writer.append(".bean(%s.class);", Util.shortName(type0)).eol();
142+
writer.append(".bean(", Util.shortName(mainType)).eol();
143+
writeGeneric(type);
142144
}
143145
}
144146

147+
void writeGeneric(UType type) {
148+
if (useJsonb && type.isGeneric()) {
149+
final var params =
150+
type.importTypes().stream()
151+
.skip(1)
152+
.map(Util::shortName)
153+
.collect(Collectors.joining(".class, "))
154+
+ ".class";
155+
156+
writer.append(
157+
"Types.newParameterizedType(%s.class, %s)", Util.shortName(type.mainType()), params);
158+
} else {
159+
writer.append("%s.class", Util.shortName(type.mainType()));
160+
}
161+
writer.append(");");
162+
163+
writer.eol();
164+
}
165+
145166
private void writeWithHandler() {
146167
if (bodyHandlerParam != null) {
147168
writer.append(".handler(%s);", bodyHandlerParam.name()).eol();

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public class ClientProcessor extends AbstractProcessor {
2727

2828
protected ProcessingContext ctx;
2929

30+
private boolean useJsonB;
31+
32+
public ClientProcessor() {
33+
try {
34+
Class.forName("io.avaje.jsonb.Jsonb");
35+
this.useJsonB = true;
36+
} catch (final ClassNotFoundException e) {
37+
this.useJsonB = false;
38+
}
39+
}
40+
41+
public ClientProcessor(boolean useJsonb) {
42+
useJsonB = useJsonb;
43+
}
44+
3045
@Override
3146
public SourceVersion getSupportedSourceVersion() {
3247
return SourceVersion.latest();
@@ -107,7 +122,7 @@ private void writeClient(Element controller) {
107122
}
108123

109124
protected String writeClientAdapter(ProcessingContext ctx, ControllerReader reader) throws IOException {
110-
return new ClientWriter(reader, ctx).write();
125+
return new ClientWriter(reader, ctx,useJsonB).write();
111126
}
112127

113128
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ class ClientWriter extends BaseControllerWriter {
2121
private static final String SUFFIX = "HttpClient";
2222

2323
private final List<ClientMethodWriter> methodList = new ArrayList<>();
24+
private final boolean useJsonb;
2425

25-
ClientWriter(ControllerReader reader, ProcessingContext ctx) throws IOException {
26+
ClientWriter(ControllerReader reader, ProcessingContext ctx, boolean useJsonB) throws IOException {
2627
super(reader, ctx, SUFFIX);
2728
reader.addImportType(HTTP_CLIENT_CONTEXT);
2829
reader.addImportType(HTTP_API_PROVIDER);
30+
this.useJsonb = useJsonB;
2931
readMethods();
32+
if (useJsonB) reader.addImportType("io.avaje.jsonb.Types");
3033
}
3134

3235
@Override
@@ -38,7 +41,7 @@ protected String initPackageName(String originName) {
3841
private void readMethods() {
3942
for (MethodReader method : reader.methods()) {
4043
if (method.isWebMethod()) {
41-
ClientMethodWriter methodWriter = new ClientMethodWriter(method, writer, ctx);
44+
final var methodWriter = new ClientMethodWriter(method, writer, ctx, useJsonb);
4245
methodWriter.addImportTypes(reader);
4346
methodList.add(methodWriter);
4447
}

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,46 @@ public static void writeJsonbType(UType type, Append writer) {
4141

4242
writer.append(" this.%sJsonType = jsonB.type(", type.shortName());
4343
if (!type.isGeneric()) {
44-
writer.append("%s.class)", PrimitiveUtil.wrap(type.full()));
44+
writer.append("%s.class)", Util.shortName(PrimitiveUtil.wrap(type.full())));
4545
} else {
4646
switch (type.mainType()) {
4747
case "java.util.List":
48-
writer.append("%s.class).list()", type.param0());
48+
writeType(UType.parse(type.paramRaw()), writer);
49+
writer.append(".list()");
4950
break;
5051
case "java.util.Set":
51-
writer.append("%s.class).set()", type.param0());
52+
writeType(UType.parse(type.paramRaw()), writer);
53+
writer.append(".set()");
5254
break;
5355
case "java.util.Map":
54-
writer.append("%s.class).map()", type.param1());
56+
writeType(UType.parse(type.paramRaw()), writer);
57+
writer.append(".map()");
5558
break;
5659
default:
5760
{
5861
if (type.mainType().contains("java.util"))
5962
throw new UnsupportedOperationException(
6063
"Only java.util Map, Set and List are supported JsonB Controller Collection Types");
61-
62-
final var params =
63-
type.importTypes().stream().skip(1).collect(Collectors.joining(".class, ")) + ".class";
64-
65-
writer.append("Types.newParameterizedType(%s.class, %s))", type.mainType(), params);
64+
writeType(type, writer);
6665
}
6766
}
6867
}
6968
writer.append(";").eol();
7069
}
70+
71+
static void writeType(UType type, Append writer) {
72+
if (type.isGeneric()) {
73+
final var params =
74+
type.importTypes().stream()
75+
.skip(1)
76+
.map(Util::shortName)
77+
.collect(Collectors.joining(".class, "))
78+
+ ".class";
79+
80+
writer.append(
81+
"Types.newParameterizedType(%s.class, %s))", Util.shortName(type.mainType()), params);
82+
} else {
83+
writer.append("%s.class)", Util.shortName(type.mainType()));
84+
}
85+
}
7186
}

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ default String param1() {
4848
return null;
4949
}
5050

51-
/**
52-
* Return the raw type.
53-
*/
51+
/** Return the raw generic parameter if this UType is a Collection. */
52+
default String paramRaw() {
53+
return null;
54+
}
55+
56+
/** Return the raw type. */
5457
String full();
5558

5659
default boolean isGeneric() {
@@ -125,23 +128,42 @@ public String shortName() {
125128
public String mainType() {
126129
return rawType;
127130
}
128-
129131
}
130132

131133
/**
132134
* Generic type.
133135
*/
134136
class Generic implements UType {
135137
final String rawType;
138+
final String rawParamType;
136139
final List<String> allTypes;
137140
final String shortRawType;
138141
final String shortName;
139142

140143
Generic(String rawTypeInput) {
141-
this.rawType = rawTypeInput.replace(" ",""); // trim whitespace
144+
this.rawType = rawTypeInput.replace(" ", ""); // trim whitespace
142145
this.allTypes = Arrays.asList(rawType.split("[<|>|,]"));
143146
this.shortRawType = shortRawType(rawType, allTypes);
144147
this.shortName = Util.name(shortRawType);
148+
this.rawParamType = extractCollectionParam();
149+
}
150+
151+
private String extractCollectionParam() {
152+
153+
switch (mainType()) {
154+
case "java.util.Set":
155+
case "java.util.Stream":
156+
case "java.util.List":
157+
var first = rawType.indexOf("<") + 1;
158+
var end = rawType.lastIndexOf(">");
159+
return rawType.substring(first, end);
160+
case "java.util.Map":
161+
first = rawType.indexOf(",") + 1;
162+
end = rawType.lastIndexOf(">");
163+
return rawType.substring(first, end);
164+
default:
165+
return rawType;
166+
}
145167
}
146168

147169
private String shortRawType(String rawType, List<String> allTypes) {
@@ -213,5 +235,10 @@ public String param0() {
213235
public String param1() {
214236
return allTypes.size() < 3 ? null : allTypes.get(2);
215237
}
238+
239+
@Override
240+
public String paramRaw() {
241+
return rawParamType;
242+
}
216243
}
217244
}

0 commit comments

Comments
 (0)