|
1 | 1 | package com.mailgun.util;
|
2 | 2 |
|
| 3 | +import java.lang.reflect.Method; |
| 4 | + |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | + |
3 | 7 | import com.mailgun.api.MailgunApi;
|
4 | 8 | import com.mailgun.enums.ApiVersion;
|
| 9 | +import lombok.SneakyThrows; |
5 | 10 | import lombok.experimental.UtilityClass;
|
6 |
| -import org.apache.commons.lang3.StringUtils; |
7 |
| - |
8 |
| -import java.lang.invoke.MethodHandles; |
9 |
| -import java.lang.reflect.Constructor; |
10 |
| -import java.lang.reflect.Proxy; |
11 | 11 |
|
12 | 12 | @UtilityClass
|
13 | 13 | public class MailgunApiUtil {
|
14 | 14 |
|
| 15 | + @SneakyThrows |
15 | 16 | public String getFullUrl(Class<? extends MailgunApi> apiType, String baseUrl) {
|
16 |
| - MailgunApi mailgunApi = (MailgunApi) Proxy.newProxyInstance( |
17 |
| - Thread.currentThread().getContextClassLoader(), |
18 |
| - new Class[]{apiType}, |
19 |
| - (proxy, method, args) -> { |
20 |
| - Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class |
21 |
| - .getDeclaredConstructor(Class.class); |
22 |
| - constructor.setAccessible(true); |
23 |
| - |
24 |
| - return constructor.newInstance(apiType) |
25 |
| - .in(apiType) |
26 |
| - .unreflectSpecial(method, apiType) |
27 |
| - .bindTo(proxy) |
28 |
| - .invokeWithArguments(args); |
29 |
| - } |
30 |
| - ); |
31 |
| - |
32 |
| - ApiVersion apiVersion = mailgunApi.getApiVersion(); |
| 17 | + ApiVersion apiVersion; |
| 18 | + try { |
| 19 | + Method declaredMethod = apiType.getDeclaredMethod("getApiVersion"); |
| 20 | + apiVersion = (ApiVersion) declaredMethod.invoke(null); |
| 21 | + } catch (Exception e) { |
| 22 | + apiVersion = MailgunApi.getApiVersion(); |
| 23 | + } |
33 | 24 |
|
34 | 25 | return getFullUrl(baseUrl, apiVersion.getValue());
|
35 | 26 | }
|
|
0 commit comments