Create an implementation of the API endpoints defined by the service interface. The relative path for a given method is obtained from an annotation on the method describing the request type. The built-in methods are retrofit2.http.GET GET, retrofit2.http.PUT PUT, {@link r
(final Class<T> service)
| 154 | * </pre> |
| 155 | */ |
| 156 | @SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety. |
| 157 | public <T> T create(final Class<T> service) { |
| 158 | validateServiceInterface(service); |
| 159 | return (T) |
| 160 | Proxy.newProxyInstance( |
| 161 | service.getClassLoader(), |
| 162 | new Class<?>[] {service}, |
| 163 | new InvocationHandler() { |
| 164 | private final Object[] emptyArgs = new Object[0]; |
| 165 | |
| 166 | @Override |
| 167 | public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args) |
| 168 | throws Throwable { |
| 169 | // If the method is a method from Object then defer to normal invocation. |
| 170 | if (method.getDeclaringClass() == Object.class) { |
| 171 | return method.invoke(this, args); |
| 172 | } |
| 173 | args = args != null ? args : emptyArgs; |
| 174 | Reflection reflection = Platform.reflection; |
| 175 | return reflection.isDefaultMethod(method) |
| 176 | ? reflection.invokeDefaultMethod(method, service, proxy, args) |
| 177 | : loadServiceMethod(service, method).invoke(proxy, args); |
| 178 | } |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | private void validateServiceInterface(Class<?> service) { |
| 183 | if (!service.isInterface()) { |