Skip to content

Commit b954cac

Browse files
committed
Fixes in response to code review by @ST-DDT
1 parent 62c6960 commit b954cac

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/inject/GrpcClientBeanPostProcessor.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import java.lang.reflect.Field;
2323
import java.lang.reflect.Member;
2424
import java.lang.reflect.Method;
25-
import java.util.*;
25+
import java.util.ArrayList;
26+
import java.util.Collection;
27+
import java.util.List;
2628

29+
import org.springframework.beans.BeanInstantiationException;
2730
import org.springframework.beans.BeansException;
2831
import org.springframework.beans.InvalidPropertyException;
2932
import org.springframework.beans.factory.BeanCreationException;
@@ -57,7 +60,7 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
5760
// which could lead to problems with the correct bean setup.
5861
private GrpcChannelFactory channelFactory = null;
5962
private List<StubTransformer> stubTransformers = null;
60-
private final List<StubFactory> stubFactories;
63+
private List<StubFactory> stubFactories = null;
6164

6265
/**
6366
* Creates a new GrpcClientBeanPostProcessor with the given ApplicationContext.
@@ -67,7 +70,6 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
6770
*/
6871
public GrpcClientBeanPostProcessor(final ApplicationContext applicationContext) {
6972
this.applicationContext = requireNonNull(applicationContext, "applicationContext");
70-
stubFactories = new ArrayList<>(applicationContext.getBeansOfType(StubFactory.class).values());
7173
}
7274

7375
@Override
@@ -226,25 +228,36 @@ protected <T> T valueForMember(final String name, final Member injectionTarget,
226228
}
227229

228230
/**
229-
* Creates a stub instance for the specified stub type.
231+
* Creates a stub instance for the specified stub type using the resolved {@link StubFactory}.
230232
*
231233
* @param stubClass
232234
* @param channel
233-
* @throws IllegalArgumentException If the method was called with an unsupported stub type.
234-
* @throws IllegalStateException If failed creating the stub instance.
235-
* @return
235+
* @throws BeanInstantiationException If the stub couldn't be created, either because the type isn't supported or because of a failure in creation.
236+
* @return A newly created gRPC stub.
236237
*/
237238
private AbstractStub<?> createStub(Class<? extends AbstractStub<?>> stubClass, Channel channel) {
238-
final StubFactory factory = this.stubFactories.stream()
239+
final StubFactory factory = getStubFactories().stream()
239240
.filter(stubFactory -> stubFactory.isApplicable(stubClass))
240241
.findFirst()
241-
.orElseThrow(() -> new IllegalArgumentException(
242+
.orElseThrow(() -> new BeanInstantiationException(stubClass,
242243
"Unsupported stub type: " + stubClass.getName() + " -> Please report this issue."));
243244

244245
try {
245246
return factory.createStub(stubClass, channel);
246247
} catch (Exception exception) {
247-
throw new IllegalStateException("Failed to create gRPC stub of type " + stubClass.getName(), exception);
248+
throw new BeanInstantiationException(stubClass, "Failed to create gRPC stub of type " + stubClass.getName(), exception);
248249
}
249250
}
251+
252+
/**
253+
* Lazy getter for the list of defined {@link StubFactory} beans.
254+
*
255+
* @return A list of all defined {@link StubFactory} beans.
256+
*/
257+
private List<StubFactory> getStubFactories() {
258+
if (this.stubFactories == null) {
259+
stubFactories = new ArrayList<>(applicationContext.getBeansOfType(StubFactory.class).values());
260+
}
261+
return stubFactories;
262+
}
250263
}

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/stubfactory/StandardJavaGrpcStubFactory.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,27 @@
1717

1818
package net.devh.boot.grpc.client.stubfactory;
1919

20-
import java.lang.reflect.Constructor;
21-
import java.lang.reflect.Method;
22-
23-
import org.springframework.beans.BeanInstantiationException;
24-
2520
import io.grpc.Channel;
2621
import io.grpc.stub.AbstractStub;
22+
import org.springframework.beans.BeanInstantiationException;
23+
24+
import java.lang.reflect.Method;
2725

2826
/**
2927
* A factory for creating stubs provided by standard grpc Java library. This is an abstract super-type that can be
3028
* extended to support the different provided types.
3129
*/
3230
public abstract class StandardJavaGrpcStubFactory implements StubFactory {
3331

34-
/**
35-
* Creates a stub of the given type.
36-
*
37-
* @param stubType The type of the stub to create.
38-
* @param channel The channel used to create the stub.
39-
* @return The newly created stub.
40-
*
41-
* @throws BeanInstantiationException If the stub couldn't be created.
42-
*/
4332
@Override
4433
public AbstractStub<?> createStub(final Class<? extends AbstractStub<?>> stubType, final Channel channel) {
4534
try {
46-
// First try the public static factory method
35+
// Use the public static factory method
4736
final String methodName = getFactoryMethodName();
4837
final Class<?> enclosingClass = stubType.getEnclosingClass();
4938
final Method factoryMethod = enclosingClass.getMethod(methodName, Channel.class);
5039
return stubType.cast(factoryMethod.invoke(null, channel));
5140
} catch (final Exception e) {
52-
try {
53-
// Use the private constructor as backup
54-
final Constructor<? extends AbstractStub<?>> constructor =
55-
stubType.getDeclaredConstructor(Channel.class);
56-
constructor.setAccessible(true);
57-
return constructor.newInstance(channel);
58-
} catch (final Exception e1) {
59-
e.addSuppressed(e1);
60-
}
6141
throw new BeanInstantiationException(stubType, "Failed to create gRPC client", e);
6242
}
6343
}

grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/stubfactory/StubFactory.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,31 @@
1919

2020
import io.grpc.Channel;
2121
import io.grpc.stub.AbstractStub;
22+
import org.springframework.beans.BeanInstantiationException;
2223

24+
/**
25+
* A factory for gRPC stubs. This is an extension mechanism for supporting different types of gRPC compiled stubs
26+
* in addition to the standard Java compiled gRPC.
27+
*
28+
* Spring beans implementing this type will be picked up automatically and added to the list of supported types.
29+
*/
2330
public interface StubFactory {
2431

32+
/**
33+
* Creates a stub of the given type.
34+
*
35+
* @param stubType The type of the stub to create.
36+
* @param channel The channel used to create the stub.
37+
* @return The newly created stub.
38+
*
39+
* @throws BeanInstantiationException If the stub couldn't be created.
40+
*/
2541
AbstractStub<?> createStub(final Class<? extends AbstractStub<?>> stubType, final Channel channel);
2642

43+
/**
44+
* Used to resolve a factory that matches the particular stub type.
45+
* @param stubType The type of the stub that needs to be created.
46+
* @return True if this particular factory is capable of creating instances of this stub type. False otherwise.
47+
*/
2748
boolean isApplicable(Class<? extends AbstractStub<?>> stubType);
2849
}

0 commit comments

Comments
 (0)