Skip to content

Commit 5214174

Browse files
committed
#48 - Fix for use of generated client in src/test - updated avaje-http-client-generator
Supports suffix of $HttpClient and HttpClient, removing the $ to better support use in src/test
1 parent eeddaec commit 5214174

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
<dependency>
3838
<groupId>io.avaje</groupId>
3939
<artifactId>avaje-slf4j-jpl</artifactId>
40-
<version>1.0</version>
40+
<version>1.1</version>
4141
<scope>test</scope>
4242
</dependency>
4343

4444
<dependency>
4545
<groupId>io.avaje</groupId>
4646
<artifactId>junit</artifactId>
47-
<version>1.0</version>
47+
<version>1.1</version>
4848
<scope>test</scope>
4949
</dependency>
5050

client/src/main/java/io/avaje/http/client/DHttpClientContext.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,29 @@ public <T> T create(Class<T> clientInterface) {
6363
if (apiProvider != null) {
6464
return apiProvider.provide(this);
6565
}
66-
String implClassName = clientImplementationClassName(clientInterface);
6766
try {
68-
Class<?> serviceClass = Class.forName(implClassName);
69-
Constructor<?> constructor = serviceClass.getConstructor(HttpClientContext.class);
70-
Object service = constructor.newInstance(this);
71-
return (T) service;
67+
Class<?> implementationClass = implementationClass(clientInterface);
68+
Constructor<?> constructor = implementationClass.getConstructor(HttpClientContext.class);
69+
return (T) constructor.newInstance(this);
7270
} catch (Exception e) {
73-
throw new IllegalStateException("Failed to create http client service " + implClassName, e);
71+
String cn = implementationClassName(clientInterface, "HttpClient");
72+
throw new IllegalStateException("Failed to create http client service " + cn, e);
7473
}
7574
}
7675

77-
private <T> String clientImplementationClassName(Class<T> clientInterface) {
76+
private Class<?> implementationClass(Class<?> clientInterface) throws ClassNotFoundException {
77+
try {
78+
return Class.forName(implementationClassName(clientInterface, "HttpClient"));
79+
} catch (ClassNotFoundException e) {
80+
// try the older generated client suffix
81+
return Class.forName(implementationClassName(clientInterface, "$HttpClient"));
82+
}
83+
}
84+
85+
private <T> String implementationClassName(Class<T> clientInterface, String suffix) {
7886
String packageName = clientInterface.getPackageName();
7987
String simpleName = clientInterface.getSimpleName();
80-
return packageName + ".httpclient." + simpleName + "$HttpClient";
88+
return packageName + ".httpclient." + simpleName + suffix;
8189
}
8290

8391
@Override

0 commit comments

Comments
 (0)