Skip to content

Commit cd36b01

Browse files
authored
[HttpClient] Generated client to use suffix of Impl or HttpClient (#431)
Avoid duplicating HttpClient or Client in the generated client class name. If the client interface ends in "Client" then use the suffix of "Impl" otherwise use "HttpClient".
1 parent 566c7b7 commit cd36b01

File tree

8 files changed

+119
-19
lines changed

8 files changed

+119
-19
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ private void writeClient(Element controller) {
101101
}
102102

103103
protected String writeClientAdapter(ControllerReader reader) throws IOException {
104-
return new ClientWriter(reader, useJsonB).write();
104+
var suffix = ClientSuffix.fromInterface(reader.beanType().getQualifiedName().toString());
105+
return new ClientWriter(reader, suffix, useJsonB).write();
105106
}
106107

107108
private void initialiseComponent() {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.avaje.http.generator.client;
2+
3+
/**
4+
* Handling client naming suffix.
5+
* <p>
6+
* The suffix chosen should avoid repeating "HttpClient" or "Client"
7+
* in the generated class name.
8+
*/
9+
final class ClientSuffix {
10+
11+
private static final String SUB_PACKAGE = ".httpclient";
12+
13+
/**
14+
* Return the suffix to use for the generated client given the
15+
* client interface name.
16+
*/
17+
static String fromInterface(String fullName) {
18+
if (fullName.endsWith("Client")) {
19+
return "Impl";
20+
} else {
21+
return "HttpClient";
22+
}
23+
}
24+
25+
/**
26+
* Remove the suffix from the generated name.
27+
*/
28+
static String removeSuffix(String clientName) {
29+
if (clientName.endsWith("Impl")) {
30+
return trim(clientName, 4);
31+
} else if (clientName.endsWith("HttpClient")) {
32+
return trim(clientName, 10);
33+
} else {
34+
return clientName;
35+
}
36+
}
37+
38+
/**
39+
* Return the interface name from the generated client name.
40+
*/
41+
static String toInterface(String clientName) {
42+
return removeSubPackage(removeSuffix(clientName));
43+
}
44+
45+
private static String trim(String name, int length) {
46+
return name.substring(0, name.length() - length);
47+
}
48+
49+
private static String removeSubPackage(String className) {
50+
final int pos = className.lastIndexOf(SUB_PACKAGE);
51+
if (pos > -1) {
52+
return className.substring(0, pos) + className.substring(pos + SUB_PACKAGE.length());
53+
}
54+
return className;
55+
}
56+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class ClientWriter extends BaseControllerWriter {
1818
private static final String HTTP_CLIENT = "io.avaje.http.client.HttpClient";
1919

2020
private static final String AT_GENERATED = "@Generated(\"avaje-http-client-generator\")";
21-
private static final String SUFFIX = "HttpClient";
2221

2322
private final List<ClientMethodWriter> methodList = new ArrayList<>();
2423
private final boolean useJsonb;
2524
private final Set<String> propertyConstants = new HashSet<>();
25+
private final String suffix;
2626

27-
28-
ClientWriter(ControllerReader reader, boolean useJsonB) throws IOException {
29-
super(reader, SUFFIX);
27+
ClientWriter(ControllerReader reader, String suffix, boolean useJsonB) throws IOException {
28+
super(reader, suffix);
29+
this.suffix = suffix;
3030
reader.addImportType(HTTP_CLIENT);
3131
this.useJsonb = useJsonB;
3232
readMethods();
@@ -67,11 +67,11 @@ private void writeMethods() {
6767
private void writeClassStart() {
6868
writer.append(AT_GENERATED).eol();
6969
AnnotationUtil.writeAnnotations(writer, reader.beanType());
70-
writer.append("public class %s%s implements %s {", shortName, SUFFIX, shortName).eol().eol();
70+
writer.append("public class %s%s implements %s {", shortName, suffix, shortName).eol().eol();
7171

7272
writer.append(" private final HttpClient client;").eol().eol();
7373

74-
writer.append(" public %s%s(HttpClient client) {", shortName, SUFFIX).eol();
74+
writer.append(" public %s%s(HttpClient client) {", shortName, suffix).eol();
7575
writer.append(" this.client = client;").eol();
7676
writer.append(" }").eol().eol();
7777
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,9 @@ List<String> all() {
5252
Collection<String> allImports() {
5353
final Set<String> packageImports = new TreeSet<>(generatedClients);
5454
generatedClients.stream()
55-
.map(s -> removeLast(removeLast(s, ".httpclient"), "HttpClient"))
56-
.forEach(packageImports::add);
55+
.map(ClientSuffix::toInterface)
56+
.forEach(packageImports::add);
5757

5858
return packageImports;
5959
}
60-
61-
public static String removeLast(String className, String search) {
62-
final int pos = className.lastIndexOf(search);
63-
if (pos > -1) {
64-
return className.substring(0, pos) + className.substring(pos + search.length());
65-
}
66-
return className;
67-
}
6860
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void writeRegister() {
6464

6565
for (final String clientFullName : metaData.all()) {
6666
final String clientShortName = Util.shortName(clientFullName);
67-
final var clientInterface = ComponentMetaData.removeLast(clientShortName, "HttpClient");
67+
final String clientInterface = ClientSuffix.removeSuffix(clientShortName);
6868
writer.append(" providerMap.put(%s.class, %s::new);", clientInterface, clientShortName).eol();
6969
}
7070
writer.append(" }").eol().eol();
@@ -103,7 +103,6 @@ private void writeImports() {
103103

104104
for (final String importType : importTypes) {
105105
writer.append("import %s;", importType).eol();
106-
writer.append("import %s;", importType).eol();
107106
}
108107
writer.eol();
109108
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.avaje.http.generator.client;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.avaje.http.generator.client.ClientSuffix.*;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
class ClientSuffixTest {
9+
10+
@Test
11+
void testFromInterface() {
12+
assertEquals("Impl", fromInterface("foo.MyClient"));
13+
assertEquals("Impl", fromInterface("foo.MyHttpClient"));
14+
assertEquals("HttpClient", fromInterface("foo.My"));
15+
}
16+
17+
@Test
18+
void testRemoveSuffix() {
19+
assertEquals("foo.MyClient", removeSuffix("foo.MyClientImpl"));
20+
assertEquals("foo.MyHttpClient", removeSuffix("foo.MyHttpClientImpl"));
21+
assertEquals("foo.My", removeSuffix("foo.MyHttpClient"));
22+
}
23+
24+
@Test
25+
void testToInterface() {
26+
assertEquals("foo.MyClient", toInterface("foo.httpclient.MyClientImpl"));
27+
assertEquals("foo.MyHttpClient", toInterface("foo.httpclient.MyHttpClientImpl"));
28+
assertEquals("foo.My", toInterface("foo.httpclient.MyHttpClient"));
29+
}
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example;
2+
3+
import io.avaje.http.api.*;
4+
5+
@Client
6+
public interface MyOtherHttpClient {
7+
8+
@Post
9+
String asPlainString();
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.example;
2+
3+
import io.avaje.http.api.*;
4+
5+
@Client
6+
public interface MySillyClient {
7+
8+
@Post
9+
String asPlainString();
10+
11+
}

0 commit comments

Comments
 (0)