Skip to content

Commit

Permalink
Refactored to favor immutability and easy of read
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Aug 5, 2024
1 parent 7544bc0 commit 19ad5e9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 118 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/feign/AsyncFeign.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ closeAfterDecode, decodeVoid, responseInterceptorChain()),
responseHandler, logger, logLevel,
propagationPolicy, methodInfoResolver,
new RequestTemplateFactoryResolver(encoder, queryMapEncoder),
options, decoder, errorDecoder);
options);
final ReflectiveFeign<C> feign =
new ReflectiveFeign<>(contract, methodHandlerFactory, invocationHandlerFactory,
defaultContextSupplier);
Expand Down
56 changes: 18 additions & 38 deletions core/src/main/java/feign/AsynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import feign.InvocationHandlerFactory.MethodHandler;
import feign.Request.Options;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
Expand All @@ -35,29 +33,14 @@ final class AsynchronousMethodHandler<C> implements MethodHandler {
private final C requestContext;
private final AsyncResponseHandler asyncResponseHandler;
private final MethodInfo methodInfo;
private MethodHandlerConfiguration methodHandlerConfiguration =
new MethodHandlerConfiguration(null, null, null, null, null, null, null, null, null);


private AsynchronousMethodHandler(Target<?> target, AsyncClient<C> client, Retryer retryer,
List<RequestInterceptor> requestInterceptors,
Logger logger, Logger.Level logLevel, MethodMetadata metadata,
RequestTemplate.Factory buildTemplateFromArgs, Options options,
AsyncResponseHandler asyncResponseHandler, ExceptionPropagationPolicy propagationPolicy,
C requestContext, MethodInfo methodInfo) {

this.methodHandlerConfiguration.setTarget(checkNotNull(target, "target"));
this.client = checkNotNull(client, "client for %s", target);
this.methodHandlerConfiguration.setRetryer(checkNotNull(retryer, "retryer for %s", target));
this.methodHandlerConfiguration.setRequestInterceptors(
checkNotNull(requestInterceptors, "requestInterceptors for %s", target));
this.methodHandlerConfiguration.setLogger(checkNotNull(logger, "logger for %s", target));
this.methodHandlerConfiguration.setLogLevel(checkNotNull(logLevel, "logLevel for %s", target));
this.methodHandlerConfiguration.setMetadata(checkNotNull(metadata, "metadata for %s", target));
this.methodHandlerConfiguration
.setBuildTemplateFromArgs(checkNotNull(buildTemplateFromArgs, "metadata for %s", target));
this.methodHandlerConfiguration.setOptions(checkNotNull(options, "options for %s", target));
this.methodHandlerConfiguration.setPropagationPolicy(propagationPolicy);
private final MethodHandlerConfiguration methodHandlerConfiguration;

private AsynchronousMethodHandler(MethodHandlerConfiguration methodHandlerConfiguration,
AsyncClient<C> client, AsyncResponseHandler asyncResponseHandler, C requestContext,
MethodInfo methodInfo) {
this.methodHandlerConfiguration =
checkNotNull(methodHandlerConfiguration, "methodHandlerConfiguration");
this.client = checkNotNull(client, "client for %s", methodHandlerConfiguration.getTarget());
this.requestContext = requestContext;
this.asyncResponseHandler = asyncResponseHandler;
this.methodInfo = methodInfo;
Expand Down Expand Up @@ -248,18 +231,14 @@ static class Factory<C> implements MethodHandler.Factory<C> {
private final MethodInfoResolver methodInfoResolver;
private final RequestTemplateFactoryResolver requestTemplateFactoryResolver;
private final Options options;
private final Decoder decoder;
private final ErrorDecoder errorDecoder;

Factory(AsyncClient<C> client, Retryer retryer, List<RequestInterceptor> requestInterceptors,
AsyncResponseHandler responseHandler,
Logger logger, Logger.Level logLevel,
ExceptionPropagationPolicy propagationPolicy,
MethodInfoResolver methodInfoResolver,
RequestTemplateFactoryResolver requestTemplateFactoryResolver,
Options options,
Decoder decoder,
ErrorDecoder errorDecoder) {
Options options) {
this.client = checkNotNull(client, "client");
this.retryer = checkNotNull(retryer, "retryer");
this.requestInterceptors = checkNotNull(requestInterceptors, "requestInterceptors");
Expand All @@ -271,19 +250,20 @@ static class Factory<C> implements MethodHandler.Factory<C> {
this.requestTemplateFactoryResolver =
checkNotNull(requestTemplateFactoryResolver, "requestTemplateFactoryResolver");
this.options = checkNotNull(options, "options");
this.errorDecoder = checkNotNull(errorDecoder, "errorDecoder");
this.decoder = checkNotNull(decoder, "decoder");
}

@Override
public MethodHandler create(Target<?> target,
MethodMetadata md,
MethodMetadata metadata,
C requestContext) {
final RequestTemplate.Factory buildTemplateFromArgs =
requestTemplateFactoryResolver.resolve(target, md);
return new AsynchronousMethodHandler<C>(target, client, retryer, requestInterceptors,
logger, logLevel, md, buildTemplateFromArgs, options, responseHandler,
propagationPolicy, requestContext,
methodInfoResolver.resolve(target.type(), md.method()));
requestTemplateFactoryResolver.resolve(target, metadata);

MethodHandlerConfiguration methodHandlerConfiguration =
new MethodHandlerConfiguration(metadata, target, retryer, requestInterceptors, logger,
logLevel, buildTemplateFromArgs, options, propagationPolicy);
return new AsynchronousMethodHandler<C>(methodHandlerConfiguration, client, responseHandler,
requestContext, methodInfoResolver.resolve(target.type(), metadata.method()));
}
}
}
72 changes: 20 additions & 52 deletions core/src/main/java/feign/MethodHandlerConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@
package feign;

import java.util.List;
import static feign.Util.checkNotNull;

public class MethodHandlerConfiguration {

private MethodMetadata metadata;
private final MethodMetadata metadata;

private Target<?> target;
private final Target<?> target;

private Retryer retryer;
private final Retryer retryer;

private List<RequestInterceptor> requestInterceptors;
private final List<RequestInterceptor> requestInterceptors;

private Logger logger;
private final Logger logger;

private Logger.Level logLevel;
private final Logger.Level logLevel;

private RequestTemplate.Factory buildTemplateFromArgs;
private final RequestTemplate.Factory buildTemplateFromArgs;

private Request.Options options;
private final Request.Options options;

private ExceptionPropagationPolicy propagationPolicy;
private final ExceptionPropagationPolicy propagationPolicy;

public MethodMetadata getMetadata() {
return metadata;
Expand Down Expand Up @@ -71,55 +72,22 @@ public ExceptionPropagationPolicy getPropagationPolicy() {
return propagationPolicy;
}

public void setMetadata(MethodMetadata metadata) {
this.metadata = metadata;
}

public void setTarget(Target<?> target) {
this.target = target;
}

public void setRetryer(Retryer retryer) {
this.retryer = retryer;
}

public void setRequestInterceptors(List<RequestInterceptor> requestInterceptors) {
this.requestInterceptors = requestInterceptors;
}

public void setLogger(Logger logger) {
this.logger = logger;
}

public void setLogLevel(Logger.Level logLevel) {
this.logLevel = logLevel;
}

public void setBuildTemplateFromArgs(RequestTemplate.Factory buildTemplateFromArgs) {
this.buildTemplateFromArgs = buildTemplateFromArgs;
}

public void setOptions(Request.Options options) {
this.options = options;
}

public void setPropagationPolicy(ExceptionPropagationPolicy propagationPolicy) {
this.propagationPolicy = propagationPolicy;
}

public MethodHandlerConfiguration(MethodMetadata metadata, Target<?> target,
Retryer retryer, List<RequestInterceptor> requestInterceptors,
Logger logger,
Logger.Level logLevel, RequestTemplate.Factory buildTemplateFromArgs,
Request.Options options, ExceptionPropagationPolicy propagationPolicy) {
this.metadata = metadata;
this.target = target;
this.retryer = retryer;
this.requestInterceptors = requestInterceptors;
this.logger = logger;
this.logLevel = logLevel;
this.buildTemplateFromArgs = buildTemplateFromArgs;
this.options = options;
this.target = checkNotNull(target, "target");
this.retryer = checkNotNull(retryer, "retryer for %s", target);
this.requestInterceptors =
checkNotNull(requestInterceptors, "requestInterceptors for %s", target);
this.logger = checkNotNull(logger, "logger for %s", target);
this.logLevel = checkNotNull(logLevel, "logLevel for %s", target);
this.metadata = checkNotNull(metadata, "metadata for %s", target);
this.buildTemplateFromArgs = checkNotNull(buildTemplateFromArgs, "metadata for %s", target);
this.options = checkNotNull(options, "options for %s", target);
this.propagationPolicy = propagationPolicy;

}
}
40 changes: 13 additions & 27 deletions core/src/main/java/feign/SynchronousMethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import static feign.Util.checkNotNull;
import feign.InvocationHandlerFactory.MethodHandler;
import feign.Request.Options;
import feign.codec.Decoder;
import feign.codec.ErrorDecoder;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -29,28 +27,14 @@ final class SynchronousMethodHandler implements MethodHandler {

private final Client client;
private final ResponseHandler responseHandler;
private MethodHandlerConfiguration methodHandlerConfiguration =
new MethodHandlerConfiguration(null, null, null, null, null, null, null, null, null);


private SynchronousMethodHandler(Target<?> target, Client client, Retryer retryer,
List<RequestInterceptor> requestInterceptors,
Logger logger, Logger.Level logLevel, MethodMetadata metadata,
RequestTemplate.Factory buildTemplateFromArgs, Options options,
ResponseHandler responseHandler, ExceptionPropagationPolicy propagationPolicy) {

this.methodHandlerConfiguration.setTarget(checkNotNull(target, "target"));
this.client = checkNotNull(client, "client for %s", target);
this.methodHandlerConfiguration.setRetryer(checkNotNull(retryer, "retryer for %s", target));
this.methodHandlerConfiguration.setRequestInterceptors(
checkNotNull(requestInterceptors, "requestInterceptors for %s", target));
this.methodHandlerConfiguration.setLogger(checkNotNull(logger, "logger for %s", target));
this.methodHandlerConfiguration.setLogLevel(checkNotNull(logLevel, "logLevel for %s", target));
this.methodHandlerConfiguration.setMetadata(checkNotNull(metadata, "metadata for %s", target));
this.methodHandlerConfiguration
.setBuildTemplateFromArgs(checkNotNull(buildTemplateFromArgs, "metadata for %s", target));
this.methodHandlerConfiguration.setOptions(checkNotNull(options, "options for %s", target));
this.methodHandlerConfiguration.setPropagationPolicy(propagationPolicy);
private final MethodHandlerConfiguration methodHandlerConfiguration;

private SynchronousMethodHandler(MethodHandlerConfiguration methodHandlerConfiguration,
Client client, ResponseHandler responseHandler) {

this.methodHandlerConfiguration =
checkNotNull(methodHandlerConfiguration, "methodHandlerConfiguration");
this.client = checkNotNull(client, "client for %s", methodHandlerConfiguration.getTarget());
this.responseHandler = responseHandler;
}

Expand Down Expand Up @@ -170,14 +154,16 @@ static class Factory implements MethodHandler.Factory<Object> {
this.options = checkNotNull(options, "options");
}

@Override
public MethodHandler create(Target<?> target,
MethodMetadata md,
Object requestContext) {
final RequestTemplate.Factory buildTemplateFromArgs =
requestTemplateFactoryResolver.resolve(target, md);
return new SynchronousMethodHandler(target, client, retryer, requestInterceptors,
logger, logLevel, md, buildTemplateFromArgs, options,
responseHandler, propagationPolicy);
MethodHandlerConfiguration methodHandlerConfiguration =
new MethodHandlerConfiguration(md, target, retryer, requestInterceptors, logger, logLevel,
buildTemplateFromArgs, options, propagationPolicy);
return new SynchronousMethodHandler(methodHandlerConfiguration, client, responseHandler);
}
}
}

0 comments on commit 19ad5e9

Please sign in to comment.