Skip to content

Commit d6e40d7

Browse files
author
ghost
committed
Data flow style change from if-else to water flow.
1 parent cd609ac commit d6e40d7

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

core/src/main/java/org/springframework/security/access/expression/method/ExpressionBasedPreInvocationAdvice.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,51 @@ public boolean before(Authentication authentication, MethodInvocation mi, PreInv
5757

5858
private Object findFilterTarget(String filterTargetName, EvaluationContext ctx, MethodInvocation invocation) {
5959
Object filterTarget = null;
60+
61+
Object[] arguments = invocation.getArguments();
62+
63+
StringBuilder assertionMessageBuilder = new StringBuilder();
64+
6065
if (filterTargetName.length() > 0) {
6166
filterTarget = ctx.lookupVariable(filterTargetName);
62-
Assert.notNull(filterTarget,
63-
() -> "Filter target was null, or no argument with name " + filterTargetName + " found in method");
67+
assertionMessageBuilder.append("Filter target was null, or no argument with name ").append(filterTargetName).append(" found in method");
6468
}
65-
else if (invocation.getArguments().length == 1) {
66-
Object arg = invocation.getArguments()[0];
67-
if (arg.getClass().isArray() || arg instanceof Collection<?>) {
68-
filterTarget = arg;
69-
}
70-
Assert.notNull(filterTarget, () -> "A PreFilter expression was set but the method argument type"
71-
+ arg.getClass() + " is not filterable");
72-
}
73-
else if (invocation.getArguments().length > 1) {
69+
70+
if (filterTargetName.length() == 0 && arguments.length > 1) {
7471
throw new IllegalArgumentException(
7572
"Unable to determine the method argument for filtering. Specify the filter target.");
7673
}
77-
Assert.isTrue(!filterTarget.getClass().isArray(),
78-
"Pre-filtering on array types is not supported. Using a Collection will solve this problem");
74+
75+
if (filterTargetName.length() == 0 &&
76+
arguments.length == 1 &&
77+
argumentsIsArrayOrCollection(arguments[0])
78+
) {
79+
filterTarget = arguments[0];
80+
assertionMessageBuilder.append("A PreFilter expression was set but the method argument type")
81+
.append(
82+
filterTarget.getClass()).append(" is not filterable");
83+
}
84+
85+
Assert.notNull(filterTarget, assertionMessageBuilder.toString());
86+
87+
assertionMessageBuilder.setLength(0);
88+
assertionMessageBuilder.append("Pre-filtering on array types is not supported. Using a Collection will solve this problem");
89+
90+
Assert.isTrue(!filterTarget.getClass().isArray(), assertionMessageBuilder.toString());
91+
92+
7993
return filterTarget;
8094
}
8195

96+
private boolean argumentsIsArrayOrCollection(Object argument) {
97+
if (argument.getClass().isArray()) {
98+
return true;
99+
}
100+
101+
return argument instanceof Collection<?>;
102+
103+
}
104+
82105
public void setExpressionHandler(MethodSecurityExpressionHandler expressionHandler) {
83106
this.expressionHandler = expressionHandler;
84107
}

0 commit comments

Comments
 (0)