@@ -83,50 +83,80 @@ public GrpcClientBeanPostProcessor(final ApplicationContext applicationContext)
83
83
public Object postProcessBeforeInitialization (final Object bean , final String beanName ) throws BeansException {
84
84
Class <?> clazz = bean .getClass ();
85
85
do {
86
- for (final Field field : clazz .getDeclaredFields ()) {
87
- final GrpcClient annotation = AnnotationUtils .findAnnotation (field , GrpcClient .class );
88
- if (annotation != null ) {
89
- ReflectionUtils .makeAccessible (field );
90
- ReflectionUtils .setField (field , bean , processInjectionPoint (field , field .getType (), annotation ));
91
- }
92
- }
93
- for (final Method method : clazz .getDeclaredMethods ()) {
94
- final GrpcClient annotation = AnnotationUtils .findAnnotation (method , GrpcClient .class );
95
- if (annotation != null ) {
96
- final Class <?>[] paramTypes = method .getParameterTypes ();
97
- if (paramTypes .length != 1 ) {
98
- throw new BeanDefinitionStoreException (
99
- "Method " + method + " doesn't have exactly one parameter." );
100
- }
101
- ReflectionUtils .makeAccessible (method );
102
- ReflectionUtils .invokeMethod (method , bean ,
103
- processInjectionPoint (method , paramTypes [0 ], annotation ));
104
- }
105
- }
86
+ processFields (clazz , bean );
87
+ processMethods (clazz , bean );
106
88
107
89
if (isAnnotatedWithConfiguration (clazz )) {
108
- for (final GrpcClientBean annotation : clazz .getAnnotationsByType (GrpcClientBean .class )) {
109
-
110
- final String beanNameToCreate = getBeanName (annotation );
111
- try {
112
- final ConfigurableListableBeanFactory beanFactory = getConfigurableBeanFactory ();
113
- final Object beanValue =
114
- processInjectionPoint (null , annotation .clazz (), annotation .client ());
115
- beanFactory .registerSingleton (beanNameToCreate , beanValue );
116
- } catch (final Exception e ) {
117
- throw new BeanCreationException (
118
- "Could not create and register grpc client bean " + beanNameToCreate + " from class " +
119
- clazz .getSimpleName (),
120
- e );
121
- }
122
- }
90
+ processGrpcClientBeansAnnotations (clazz );
123
91
}
124
92
125
93
clazz = clazz .getSuperclass ();
126
94
} while (clazz != null );
127
95
return bean ;
128
96
}
129
97
98
+ /**
99
+ * Processes the bean's fields in the given class.
100
+ *
101
+ * @param clazz The class to process.
102
+ * @param bean The bean to process.
103
+ */
104
+ private void processFields (final Class <?> clazz , final Object bean ) {
105
+ for (final Field field : clazz .getDeclaredFields ()) {
106
+ final GrpcClient annotation = AnnotationUtils .findAnnotation (field , GrpcClient .class );
107
+ if (annotation != null ) {
108
+ ReflectionUtils .makeAccessible (field );
109
+ ReflectionUtils .setField (field , bean , processInjectionPoint (field , field .getType (), annotation ));
110
+ }
111
+ }
112
+ }
113
+
114
+
115
+ /**
116
+ * Processes the bean's methods in the given class.
117
+ *
118
+ * @param clazz The class to process.
119
+ * @param bean The bean to process.
120
+ */
121
+ private void processMethods (final Class <?> clazz , final Object bean ) {
122
+ for (final Method method : clazz .getDeclaredMethods ()) {
123
+ final GrpcClient annotation = AnnotationUtils .findAnnotation (method , GrpcClient .class );
124
+ if (annotation != null ) {
125
+ final Class <?>[] paramTypes = method .getParameterTypes ();
126
+ if (paramTypes .length != 1 ) {
127
+ throw new BeanDefinitionStoreException (
128
+ "Method " + method + " doesn't have exactly one parameter." );
129
+ }
130
+ ReflectionUtils .makeAccessible (method );
131
+ ReflectionUtils .invokeMethod (method , bean ,
132
+ processInjectionPoint (method , paramTypes [0 ], annotation ));
133
+ }
134
+ }
135
+ }
136
+
137
+
138
+ /**
139
+ * Processes the given class's {@link GrpcClientBean} annotations.
140
+ *
141
+ * @param clazz The class to process.
142
+ */
143
+ private void processGrpcClientBeansAnnotations (final Class <?> clazz ) {
144
+ for (final GrpcClientBean annotation : clazz .getAnnotationsByType (GrpcClientBean .class )) {
145
+
146
+ final String beanNameToCreate = getBeanName (annotation );
147
+ try {
148
+ final ConfigurableListableBeanFactory beanFactory = getConfigurableBeanFactory ();
149
+ final Object beanValue =
150
+ processInjectionPoint (null , annotation .clazz (), annotation .client ());
151
+ beanFactory .registerSingleton (beanNameToCreate , beanValue );
152
+ } catch (final Exception e ) {
153
+ throw new BeanCreationException (annotation + " on class " + clazz .getName (), beanNameToCreate ,
154
+ "Unexpected exception while creating and registering bean" ,
155
+ e );
156
+ }
157
+ }
158
+ }
159
+
130
160
/**
131
161
* Processes the given injection point and computes the appropriate value for the injection.
132
162
*
@@ -253,8 +283,7 @@ protected <T> T valueForMember(final String name, final Member injectionTarget,
253
283
throw new InvalidPropertyException (injectionTarget .getDeclaringClass (), injectionTarget .getName (),
254
284
"Unsupported type " + injectionType .getName ());
255
285
} else {
256
- throw new InvalidPropertyException (injectionType .getDeclaringClass (), injectionType .getName (),
257
- "Unsupported type " + injectionType .getName ());
286
+ throw new BeanInstantiationException (injectionType , "Unsupported grpc stub or channel type" );
258
287
}
259
288
}
260
289
}
0 commit comments