22
22
import java .lang .reflect .Field ;
23
23
import java .lang .reflect .Member ;
24
24
import java .lang .reflect .Method ;
25
- import java .util .*;
25
+ import java .util .ArrayList ;
26
+ import java .util .Collection ;
27
+ import java .util .List ;
26
28
29
+ import org .springframework .beans .BeanInstantiationException ;
27
30
import org .springframework .beans .BeansException ;
28
31
import org .springframework .beans .InvalidPropertyException ;
29
32
import org .springframework .beans .factory .BeanCreationException ;
@@ -57,7 +60,7 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
57
60
// which could lead to problems with the correct bean setup.
58
61
private GrpcChannelFactory channelFactory = null ;
59
62
private List <StubTransformer > stubTransformers = null ;
60
- private final List <StubFactory > stubFactories ;
63
+ private List <StubFactory > stubFactories = null ;
61
64
62
65
/**
63
66
* Creates a new GrpcClientBeanPostProcessor with the given ApplicationContext.
@@ -67,7 +70,6 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor {
67
70
*/
68
71
public GrpcClientBeanPostProcessor (final ApplicationContext applicationContext ) {
69
72
this .applicationContext = requireNonNull (applicationContext , "applicationContext" );
70
- stubFactories = new ArrayList <>(applicationContext .getBeansOfType (StubFactory .class ).values ());
71
73
}
72
74
73
75
@ Override
@@ -226,25 +228,36 @@ protected <T> T valueForMember(final String name, final Member injectionTarget,
226
228
}
227
229
228
230
/**
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} .
230
232
*
231
233
* @param stubClass
232
234
* @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.
236
237
*/
237
238
private AbstractStub <?> createStub (Class <? extends AbstractStub <?>> stubClass , Channel channel ) {
238
- final StubFactory factory = this . stubFactories .stream ()
239
+ final StubFactory factory = getStubFactories () .stream ()
239
240
.filter (stubFactory -> stubFactory .isApplicable (stubClass ))
240
241
.findFirst ()
241
- .orElseThrow (() -> new IllegalArgumentException (
242
+ .orElseThrow (() -> new BeanInstantiationException ( stubClass ,
242
243
"Unsupported stub type: " + stubClass .getName () + " -> Please report this issue." ));
243
244
244
245
try {
245
246
return factory .createStub (stubClass , channel );
246
247
} 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 );
248
249
}
249
250
}
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
+ }
250
263
}
0 commit comments