46
46
import org .springframework .javapoet .CodeBlock ;
47
47
import org .springframework .test .context .DynamicPropertyRegistry ;
48
48
import org .springframework .test .context .DynamicPropertySource ;
49
+ import org .springframework .test .util .ReflectionTestUtils ;
49
50
import org .springframework .util .Assert ;
50
51
import org .springframework .util .ClassUtils ;
51
52
import org .springframework .util .ReflectionUtils ;
@@ -201,7 +202,6 @@ private static GeneratedMethod generateMethod(GenerationContext generationContex
201
202
code .addModifiers (javax .lang .model .element .Modifier .PRIVATE ,
202
203
javax .lang .model .element .Modifier .STATIC );
203
204
code .addParameter (DynamicPropertyRegistry .class , DYNAMIC_PROPERTY_REGISTRY );
204
-
205
205
if (isMethodAccessible (generatedClass , method )) {
206
206
code .addStatement (CodeBlock .of ("$T.$L($L)" , method .getDeclaringClass (), method .getName (),
207
207
DYNAMIC_PROPERTY_REGISTRY ));
@@ -211,12 +211,9 @@ private static GeneratedMethod generateMethod(GenerationContext generationContex
211
211
code .beginControlFlow ("try" );
212
212
code .addStatement ("$T<?> clazz = $T.forName($S, $T.class.getClassLoader())" , Class .class ,
213
213
ClassUtils .class , ClassName .get (method .getDeclaringClass ()), generatedClass .getName ());
214
- code .addStatement ("$T method = $T.findMethod(clazz, $S, $T.class)" , Method .class ,
215
- ReflectionUtils .class , method .getName (), DynamicPropertyRegistry .class );
216
- code .addStatement ("$T.notNull(method, $S)" , Assert .class ,
217
- "Method '" + method .getName () + "' is not found" );
218
- code .addStatement ("$T.makeAccessible(method)" , ReflectionUtils .class );
219
- code .addStatement ("$T.invokeMethod(method, null, $L)" , ReflectionUtils .class ,
214
+ // ReflectionTestUtils can be used here because
215
+ // @DynamicPropertyRegistry in a test module.
216
+ code .addStatement ("$T.invokeMethod(clazz, $S, $L)" , ReflectionTestUtils .class , method .getName (),
220
217
DYNAMIC_PROPERTY_REGISTRY );
221
218
code .nextControlFlow ("catch ($T ex)" , ClassNotFoundException .class );
222
219
code .addStatement ("throw new $T(ex)" , RuntimeException .class );
0 commit comments