Skip to content

Commit

Permalink
Renames Instance to InstanceSupplier to avoid confusions.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <santiago.pericasgeertsen@oracle.com>
  • Loading branch information
spericas committed Jul 5, 2024
1 parent eb55297 commit 7b69f5f
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract static class AbstractHandler<ReqT, RespT> implements MethodHandler<ReqT

private final String methodName;
private final AnnotatedMethod method;
private final Supplier<?> instance;
private final Supplier<?> instanceSupplier;
private final MethodDescriptor.MethodType methodType;
private Class<?> requestType = Empty.class;
private Class<?> responseType = Empty.class;
Expand All @@ -93,16 +93,16 @@ abstract static class AbstractHandler<ReqT, RespT> implements MethodHandler<ReqT
*
* @param methodName the name of the gRPC method
* @param method the underlying handler method this handler should call
* @param instance the supplier to use to obtain the object to call the method on
* @param instanceSupplier the supplier to use to obtain the object to call the method on
* @param methodType the type of method handled by this handler
*/
protected AbstractHandler(String methodName,
AnnotatedMethod method,
Supplier<?> instance,
Supplier<?> instanceSupplier,
MethodDescriptor.MethodType methodType) {
this.methodName = methodName;
this.method = method;
this.instance = instance;
this.instanceSupplier = instanceSupplier;
this.methodType = methodType;
}

Expand All @@ -120,7 +120,7 @@ public void invoke(ReqT request, StreamObserver<RespT> observer) {
}

try {
invoke(method.declaredMethod(), instance.get(), request, safe);
invoke(method.declaredMethod(), instanceSupplier.get(), request, safe);
} catch (Throwable thrown) {
safe.onError(GrpcHelper.ensureStatusException(thrown, Status.INTERNAL));
}
Expand All @@ -143,7 +143,7 @@ protected abstract void invoke(Method method, Object instance, ReqT request, Str
public StreamObserver<ReqT> invoke(StreamObserver<RespT> observer) {
StreamObserver<RespT> safe = SafeStreamObserver.ensureSafeObserver(observer);
try {
return invoke(method.declaredMethod(), instance.get(), safe);
return invoke(method.declaredMethod(), instanceSupplier.get(), safe);
} catch (Throwable thrown) {
throw GrpcHelper.ensureStatusRuntimeException(thrown, Status.INTERNAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ public abstract class AbstractServiceBuilder {

private final Class<?> serviceClass;
private final Class<?> annotatedServiceClass;
private final Supplier<?> instance;
private final Supplier<?> instanceSupplier;
private final List<MethodHandlerSupplier> handlerSuppliers;

/**
* Create a new introspection modeller for a given gRPC service class.
*
* @param serviceClass gRPC service (handler) class.
* @param instance the target instance to call gRPC handler methods on
* @throws NullPointerException if the service or instance parameters are null
* @param instanceSupplier the target instanceSupplier to call gRPC handler methods on
* @throws NullPointerException if the service or instanceSupplier parameters are null
*/
protected AbstractServiceBuilder(Class<?> serviceClass, Supplier<?> instance) {
protected AbstractServiceBuilder(Class<?> serviceClass, Supplier<?> instanceSupplier) {
this.serviceClass = Objects.requireNonNull(serviceClass);
this.annotatedServiceClass = ModelHelper.getAnnotatedResourceClass(serviceClass, Grpc.class);
this.instance = Objects.requireNonNull(instance);
this.instanceSupplier = Objects.requireNonNull(instanceSupplier);
this.handlerSuppliers = HelidonServiceLoader.create(ServiceLoader.load(MethodHandlerSupplier.class)).asList();
}

Expand Down Expand Up @@ -108,9 +108,9 @@ protected MarshallerSupplier getMarshallerSupplier() {
*/
protected static Supplier<?> createInstanceSupplier(Class<?> cls) {
if (cls.isAnnotationPresent(Singleton.class)) {
return Instance.singleton(cls);
return InstanceSupplier.singleton(cls);
} else {
return Instance.create(cls);
return InstanceSupplier.create(cls);
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ protected List<MethodHandlerSupplier> handlerSuppliers() {
* @return the service instance supplier
*/
protected Supplier<?> instanceSupplier() {
return instance;
return instanceSupplier;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean supplies(AnnotatedMethod method) {
}

@Override
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instance) {
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
if (!isRequiredMethodType(method)) {
throw new IllegalArgumentException("Method not annotated as a bi-directional streaming method: " + method);
}
Expand All @@ -57,7 +57,7 @@ public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, Annotated

switch (type) {
case bidiStreaming:
handler = new BidiStreaming<>(methodName, method, instance);
handler = new BidiStreaming<>(methodName, method, instanceSupplier);
break;
case unknown:
default:
Expand Down Expand Up @@ -123,8 +123,8 @@ private enum CallType {
public abstract static class AbstractServerStreamingHandler<ReqT, RespT>
extends AbstractHandler<ReqT, RespT> {

AbstractServerStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance, MethodDescriptor.MethodType.BIDI_STREAMING);
AbstractServerStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier, MethodDescriptor.MethodType.BIDI_STREAMING);
}

@Override
Expand All @@ -148,8 +148,8 @@ protected void invoke(Method method, Object instance, ReqT request, StreamObserv
public static class BidiStreaming<ReqT, RespT>
extends AbstractServerStreamingHandler<ReqT, RespT> {

BidiStreaming(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
BidiStreaming(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(getGenericResponseType(method.genericReturnType()));
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public boolean supplies(AnnotatedMethod method) {

@Override
@SuppressWarnings("unchecked")
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instance) {
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
if (!isRequiredMethodType(method)) {
throw new IllegalArgumentException("Method not annotated as a client streaming method: " + method);
}

CallType type = determineCallType(method);
return switch (type) {
case clientStreaming -> (MethodHandler<ReqT, RespT>) new ClientStreaming<>(methodName, method, instance);
case futureResponse -> new FutureResponse<>(methodName, method, instance);
case clientStreamingIterable -> new ClientStreamingIterable<>(methodName, method, instance);
case clientStreamingStream -> new ClientStreamingStream<>(methodName, method, instance);
case clientStreaming -> (MethodHandler<ReqT, RespT>) new ClientStreaming<>(methodName, method, instanceSupplier);
case futureResponse -> new FutureResponse<>(methodName, method, instanceSupplier);
case clientStreamingIterable -> new ClientStreamingIterable<>(methodName, method, instanceSupplier);
case clientStreamingStream -> new ClientStreamingStream<>(methodName, method, instanceSupplier);
default -> throw new IllegalArgumentException("Not a supported client streaming method signature: " + method);
};
}
Expand Down Expand Up @@ -163,8 +163,8 @@ private enum CallType {
public abstract static class AbstractClientStreamingHandler<ReqT, RespT>
extends AbstractHandler<ReqT, RespT> {

AbstractClientStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance, MethodDescriptor.MethodType.CLIENT_STREAMING);
AbstractClientStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier, MethodDescriptor.MethodType.CLIENT_STREAMING);
}

@Override
Expand All @@ -188,8 +188,8 @@ protected void invoke(Method method, Object instance, ReqT request, StreamObserv
public static class ClientStreaming<ReqT, RespT>
extends AbstractClientStreamingHandler<ReqT, RespT> {

ClientStreaming(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
ClientStreaming(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(getGenericResponseType(method.genericReturnType()));
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}
Expand Down Expand Up @@ -223,8 +223,8 @@ public Object clientStreaming(Object[] args, ClientStreaming client) {
public static class FutureResponse<ReqT, RespT>
extends AbstractClientStreamingHandler<ReqT, RespT> {

FutureResponse(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
FutureResponse(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(getGenericResponseType(method.genericReturnType()));
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}
Expand Down Expand Up @@ -260,8 +260,8 @@ public Object clientStreaming(Object[] args, ClientStreaming client) {
*/
public static class ClientStreamingIterable<ReqT, RespT> extends AbstractClientStreamingHandler<ReqT, RespT> {

ClientStreamingIterable(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
ClientStreamingIterable(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(getGenericResponseType(method.genericReturnType()));
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}
Expand Down Expand Up @@ -312,8 +312,8 @@ public Object clientStreaming(Object[] args, ClientStreaming client) {
*/
public static class ClientStreamingStream<ReqT, RespT> extends AbstractClientStreamingHandler<ReqT, RespT> {

ClientStreamingStream(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
ClientStreamingStream(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(getGenericResponseType(method.genericReturnType()));
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
/**
* A supplier of instances of objects.
*/
public interface Instance {
public interface InstanceSupplier {

/**
* Create a {@link Supplier} that supplies a
* singleton.
*
* @param instance the singleton instance to supply
* @param <T> the type of instance supplied
* @return the singleton instance
* @return the singleton instance
*
* @throws java.lang.NullPointerException if the instance parameter is null
*/
Expand All @@ -48,9 +48,9 @@ static <T> Supplier<T> singleton(T instance) {
* <p>
* The Class provided must have a no-args default constructor.
*
* @param cls the Class of the singleton instance to supply
* @param <T> the type of instance supplied
* @return the singleton instance of the specified Class
* @param cls the Class of the singleton instance to supply
* @param <T> the type of instance supplied
* @return the singleton instance of the specified Class
*
* @throws java.lang.NullPointerException if the class is null
*/
Expand All @@ -67,9 +67,9 @@ static <T> Supplier<T> singleton(Class<T> cls) {
* A new instance of the specified Class is created for every
* call to {@link Supplier#get()}.
*
* @param cls the Class of the singleton instance to supply
* @param <T> the type of instance supplied
* @return the singleton instance of the specified Class
* @param cls the Class of the singleton instance to supply
* @param <T> the type of instance supplied
* @return the singleton instance of the specified Class
*
* @throws java.lang.NullPointerException if the class is null
*/
Expand All @@ -79,13 +79,12 @@ static <T> Supplier<T> create(Class<T> cls) {

/**
* A {@link Supplier} implementation that supplies new instances
* of a class each time its {@link Supplier#get() get()} method
* of a class each time its {@link java.util.function.Supplier#get()} method
* is called.
*
* @param <T> the type of instance supplied
*/
class NewInstance<T>
implements Supplier<T> {
class NewInstance<T> implements Supplier<T> {

private final Class<T> instanceClass;

Expand All @@ -105,7 +104,7 @@ public T get() {

/**
* A {@link Supplier} implementation that supplies the same singleton
* instance of a value each time its {@link Supplier#get() get()}
* instance of a value each time its {@link java.util.function.Supplier#get()}
* method is called.
*
* @param <T> the type of instance supplied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public interface MethodHandlerSupplier {
* Supply a {@link MethodHandler} for a method.
* @param methodName the gRPC method name
* @param method the method to supply a {@link MethodHandler} for
* @param instance the supplier to supply the actual call handler
* @param instanceSupplier the supplier to supply the actual call handler
* @param <ReqT> the request type
* @param <RespT> the response type
* @return a {@link MethodHandler} for the method
* @throws java.lang.NullPointerException if the method is null
*/
<ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instance);
<ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public boolean supplies(AnnotatedMethod method) {
}

@Override
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instance) {
public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
if (!isRequiredMethodType(method)) {
throw new IllegalArgumentException("Method not annotated as a server streaming method: " + method);
}
Expand All @@ -64,16 +64,16 @@ public <ReqT, RespT> MethodHandler<ReqT, RespT> get(String methodName, Annotated

switch (type) {
case serverStreaming:
handler = new ServerStreaming<>(methodName, method, instance);
handler = new ServerStreaming<>(methodName, method, instanceSupplier);
break;
case serverStreamingNoRequest:
handler = new ServerStreamingNoRequest<>(methodName, method, instance);
handler = new ServerStreamingNoRequest<>(methodName, method, instanceSupplier);
break;
case streamResponse:
handler = new StreamResponse<>(methodName, method, instance);
handler = new StreamResponse<>(methodName, method, instanceSupplier);
break;
case streamResponseNoRequest:
handler = new StreamResponseNoRequest<>(methodName, method, instance);
handler = new StreamResponseNoRequest<>(methodName, method, instanceSupplier);
break;
case unknown:
default:
Expand Down Expand Up @@ -181,8 +181,8 @@ private enum CallType {
public abstract static class AbstractServerStreamingHandler<ReqT, RespT>
extends AbstractHandler<ReqT, RespT> {

AbstractServerStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance, MethodDescriptor.MethodType.SERVER_STREAMING);
AbstractServerStreamingHandler(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier, MethodDescriptor.MethodType.SERVER_STREAMING);
}

@Override
Expand All @@ -206,8 +206,8 @@ protected StreamObserver<ReqT> invoke(Method method, Object instance, StreamObse
public static class ServerStreaming<ReqT, RespT>
extends AbstractServerStreamingHandler<ReqT, RespT> {

ServerStreaming(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
ServerStreaming(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(method.parameterTypes()[0]);
setResponseType(getGenericResponseType(method.genericParameterTypes()[1]));
}
Expand Down Expand Up @@ -245,8 +245,8 @@ public Object serverStreaming(Object[] args, ServerStreamingClient client) {
public static class ServerStreamingNoRequest<ReqT, RespT>
extends AbstractServerStreamingHandler<ReqT, RespT> {

ServerStreamingNoRequest(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
ServerStreamingNoRequest(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setResponseType(getGenericResponseType(method.genericParameterTypes()[0]));
}

Expand Down Expand Up @@ -283,8 +283,8 @@ public Object serverStreaming(Object[] args, ServerStreamingClient client) {
public static class StreamResponse<ReqT, RespT>
extends AbstractServerStreamingHandler<ReqT, RespT> {

StreamResponse(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
StreamResponse(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setRequestType(method.parameterTypes()[0]);
setResponseType(getGenericResponseType(method.genericReturnType()));
}
Expand Down Expand Up @@ -324,8 +324,8 @@ public Object serverStreaming(Object[] args, ServerStreamingClient client) {
public static class StreamResponseNoRequest<ReqT, RespT>
extends AbstractServerStreamingHandler<ReqT, RespT> {

StreamResponseNoRequest(String methodName, AnnotatedMethod method, Supplier<?> instance) {
super(methodName, method, instance);
StreamResponseNoRequest(String methodName, AnnotatedMethod method, Supplier<?> instanceSupplier) {
super(methodName, method, instanceSupplier);
setResponseType(getGenericResponseType(method.genericReturnType()));
}

Expand Down
Loading

0 comments on commit 7b69f5f

Please sign in to comment.