@@ -39,6 +39,9 @@ public class ConvertToSecurityDslVisitor<P> extends JavaIsoVisitor<P> {
39
39
40
40
public static final String FQN_CUSTOMIZER = "org.springframework.security.config.Customizer" ;
41
41
42
+ private static final JavaType .FullyQualified CUSTOMIZER_SHALLOW_TYPE =
43
+ (JavaType .ShallowClass ) JavaType .buildType (FQN_CUSTOMIZER );
44
+
42
45
private final String securityFqn ;
43
46
44
47
private final Collection <String > convertableMethods ;
@@ -79,7 +82,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation initialMethod
79
82
J .MethodInvocation method = super .visitMethodInvocation (initialMethod , executionContext );
80
83
if (isApplicableMethod (method )) {
81
84
J .MethodInvocation m = method ;
82
- method = findDesiredReplacement (method )
85
+ method = createDesiredReplacement (method )
83
86
.map (newMethodType -> {
84
87
List <J .MethodInvocation > chain = computeAndMarkChain ();
85
88
boolean keepArg = keepArg (m .getSimpleName ());
@@ -89,9 +92,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation initialMethod
89
92
.withName (m .getName ().withSimpleName (newMethodType .getName ()))
90
93
.withArguments (ListUtils .concat (
91
94
keepArg ? m .getArguments ().get (0 ) : null ,
92
- Collections .singletonList (chain .isEmpty ()
93
- ? createDefaultsCall (newMethodType . getParameterTypes (). get ( keepArg ? 1 : 0 ))
94
- : createLambdaParam (paramName , newMethodType .getParameterTypes ().get (keepArg ? 1 : 0 ), chain ))
95
+ Collections .singletonList (chain .isEmpty () ?
96
+ createDefaultsCall () :
97
+ createLambdaParam (paramName , newMethodType .getParameterTypes ().get (keepArg ? 1 : 0 ), chain ))
95
98
)
96
99
);
97
100
})
@@ -171,37 +174,38 @@ private boolean hasHandleableArg(J.MethodInvocation m) {
171
174
&& !TypeUtils .isAssignableTo (FQN_CUSTOMIZER , m .getMethodType ().getParameterTypes ().get (0 ));
172
175
}
173
176
174
- private Optional <JavaType .Method > findDesiredReplacement (J .MethodInvocation m ) {
177
+ private Optional <JavaType .Method > createDesiredReplacement (J .MethodInvocation m ) {
175
178
JavaType .Method methodType = m .getMethodType ();
176
179
if (methodType == null ) {
177
180
return Optional .empty ();
178
181
}
179
- JavaType .FullyQualified httpSecurityType = methodType .getDeclaringType ();
182
+ JavaType .Parameterized customizerArgType = new JavaType .Parameterized (null ,
183
+ CUSTOMIZER_SHALLOW_TYPE , Collections .singletonList (methodType .getReturnType ()));
180
184
boolean keepArg = keepArg (m .getSimpleName ());
181
- int expectedParamCount = keepArg ? 2 : 1 ;
182
- int customizerParamIndex = keepArg ? 1 : 0 ;
183
- return httpSecurityType .getMethods ().stream ()
184
- .filter (availableMethod -> availableMethod .getName ().equals (methodRenames .getOrDefault (m .getSimpleName (), m .getSimpleName ())) &&
185
- availableMethod .getParameterTypes ().size () == expectedParamCount &&
186
- availableMethod .getParameterTypes ().get (customizerParamIndex ) instanceof JavaType .FullyQualified &&
187
- FQN_CUSTOMIZER .equals (((JavaType .FullyQualified ) availableMethod .getParameterTypes ().get (customizerParamIndex )).getFullyQualifiedName ()))
188
- .findFirst ();
185
+ List <String > paramNames = keepArg ? ListUtils .concat (methodType .getParameterNames (), "arg1" )
186
+ : Collections .singletonList ("arg0" );
187
+ List <JavaType > paramTypes = keepArg ? ListUtils .concat (methodType .getParameterTypes (), customizerArgType )
188
+ : Collections .singletonList (customizerArgType );
189
+ return Optional .of (methodType .withReturnType (methodType .getDeclaringType ())
190
+ .withName (methodRenames .getOrDefault (methodType .getName (), methodType .getName ()))
191
+ .withParameterNames (paramNames )
192
+ .withParameterTypes (paramTypes )
193
+ );
189
194
}
190
195
191
196
private boolean keepArg (String methodName ) {
192
197
return argReplacements .containsKey (methodName ) && argReplacements .get (methodName ) == null ;
193
198
}
194
199
195
- private Optional <JavaType .Method > findDesiredReplacementForArg (J .MethodInvocation m ) {
200
+ private Optional <JavaType .Method > createDesiredReplacementForArg (J .MethodInvocation m ) {
196
201
JavaType .Method methodType = m .getMethodType ();
197
202
if (methodType == null || !hasHandleableArg (m ) || !(methodType .getReturnType () instanceof JavaType .Class )) {
198
203
return Optional .empty ();
199
204
}
200
- JavaType .Class returnType = (JavaType .Class ) methodType .getReturnType ();
201
- return returnType .getMethods ().stream ()
202
- .filter (availableMethod -> availableMethod .getName ().equals (argReplacements .get (m .getSimpleName ())) &&
203
- availableMethod .getParameterTypes ().size () == 1 )
204
- .findFirst ();
205
+ return Optional .of (
206
+ methodType .withName (argReplacements .get (m .getSimpleName ()))
207
+ .withDeclaringType ((JavaType .FullyQualified ) methodType .getReturnType ())
208
+ );
205
209
}
206
210
207
211
// this method is unused in this repo, but, useful in Spring Tool Suite integration
@@ -232,7 +236,7 @@ private List<J.MethodInvocation> computeAndMarkChain() {
232
236
List <J .MethodInvocation > chain = new ArrayList <>();
233
237
Cursor cursor = getCursor ();
234
238
J .MethodInvocation initialMethodInvocation = cursor .getValue ();
235
- findDesiredReplacementForArg (initialMethodInvocation ).ifPresent (methodType ->
239
+ createDesiredReplacementForArg (initialMethodInvocation ).ifPresent (methodType ->
236
240
chain .add (initialMethodInvocation .withMethodType (methodType )
237
241
.withName (initialMethodInvocation .getName ().withSimpleName (methodType .getName ()))));
238
242
cursor = cursor .getParent (2 );
@@ -272,13 +276,10 @@ private boolean isDisableMethod(J.MethodInvocation method) {
272
276
return new MethodMatcher ("org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer disable()" , true ).matches (method );
273
277
}
274
278
275
- private J .MethodInvocation createDefaultsCall (JavaType type ) {
276
- JavaType .FullyQualified fullyQualified = TypeUtils .asFullyQualified (type );
277
- assert fullyQualified != null ;
278
- JavaType .Method methodType = fullyQualified .getMethods ().stream ().filter (m -> "withDefaults" .equals (m .getName ()) && m .getParameterTypes ().isEmpty () && m .getFlags ().contains (Flag .Static )).findFirst ().orElse (null );
279
- if (methodType == null ) {
280
- throw new IllegalStateException ();
281
- }
279
+ private J .MethodInvocation createDefaultsCall () {
280
+ JavaType .Method methodType = new JavaType .Method (null , 9 , CUSTOMIZER_SHALLOW_TYPE , "withDefaults" ,
281
+ new JavaType .GenericTypeVariable (null , "T" , JavaType .GenericTypeVariable .Variance .INVARIANT , null ),
282
+ null , null , null , null );
282
283
maybeAddImport (methodType .getDeclaringType ().getFullyQualifiedName (), methodType .getName ());
283
284
return new J .MethodInvocation (Tree .randomId (), Space .EMPTY , Markers .EMPTY , null , null ,
284
285
new J .Identifier (Tree .randomId (), Space .EMPTY , Markers .EMPTY , emptyList (), "withDefaults" , null , null ),
0 commit comments