Skip to content

Commit

Permalink
Remove ReflectiveAsyncFeign
Browse files Browse the repository at this point in the history
  • Loading branch information
wplong11 committed Sep 19, 2022
1 parent ab5feca commit 46c098b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 74 deletions.
52 changes: 47 additions & 5 deletions core/src/main/java/feign/AsyncFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -42,7 +47,7 @@
* be done (for example, creating and submitting a task to an {@link ExecutorService}).
*/
@Experimental
public abstract class AsyncFeign<C> {
public final class AsyncFeign<C> {
public static <C> AsyncBuilder<C> builder() {
return new AsyncBuilder<>();
}
Expand Down Expand Up @@ -212,14 +217,14 @@ public AsyncFeign<C> build() {
errorDecoder, methodHandlerFactory);
final ReflectiveFeign<C> feign =
new ReflectiveFeign<>(handlersByName, invocationHandlerFactory, queryMapEncoder);
return new ReflectiveAsyncFeign<>(feign, defaultContextSupplier);
return new AsyncFeign<>(feign, defaultContextSupplier);
}
}

private final ReflectiveFeign<C> feign;
private final AsyncContextSupplier<C> defaultContextSupplier;

protected AsyncFeign(ReflectiveFeign<C> feign, AsyncContextSupplier<C> defaultContextSupplier) {
private AsyncFeign(ReflectiveFeign<C> feign, AsyncContextSupplier<C> defaultContextSupplier) {
this.feign = feign;
this.defaultContextSupplier = defaultContextSupplier;
}
Expand All @@ -229,8 +234,45 @@ public <T> T newInstance(Target<T> target) {
}

public <T> T newInstance(Target<T> target, C context) {
return wrap(target.type(), feign.newInstance(target, context), context);
verifyTargetSpecfication(target);
return feign.newInstance(target, context);
}

private <T> void verifyTargetSpecfication(Target<T> target) {
Class<T> type = target.type();
if (!type.isInterface()) {
throw new IllegalArgumentException("Type must be an interface: " + type);
}

for (final Method m : type.getMethods()) {
final Class<?> retType = m.getReturnType();

if (!CompletableFuture.class.isAssignableFrom(retType)) {
continue; // synchronous case
}

if (retType != CompletableFuture.class) {
throw new IllegalArgumentException("Method return type is not CompleteableFuture: "
+ getFullMethodName(type, retType, m));
}

final Type genRetType = m.getGenericReturnType();

if (!ParameterizedType.class.isInstance(genRetType)) {
throw new IllegalArgumentException("Method return type is not parameterized: "
+ getFullMethodName(type, genRetType, m));
}

if (WildcardType.class
.isInstance(ParameterizedType.class.cast(genRetType).getActualTypeArguments()[0])) {
throw new IllegalArgumentException(
"Wildcards are not supported for return-type parameters: "
+ getFullMethodName(type, genRetType, m));
}
}
}

protected abstract <T> T wrap(Class<T> type, T instance, C context);
private String getFullMethodName(Class<?> type, Type retType, Method m) {
return retType.getTypeName() + " " + type.toGenericString() + "." + m.getName();
}
}
69 changes: 0 additions & 69 deletions core/src/main/java/feign/ReflectiveAsyncFeign.java

This file was deleted.

0 comments on commit 46c098b

Please sign in to comment.