Use consistent ternary expression style

Update all ternary expressions so that the condition is always in
parentheses and "not equals" is used in the test. This helps to bring
consistency across the codebase which makes ternary expression easier
to scan.

For example: `a = (a != null) ? a : b`

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-30 00:31:13 -07:00
committed by Rob Winch
parent 8d3f039f76
commit 834dcf5bcf
131 changed files with 277 additions and 265 deletions

View File

@@ -47,16 +47,16 @@ abstract class AbstractExpressionBasedMethodConfigAttribute implements ConfigAtt
Assert.isTrue(filterExpression != null || authorizeExpression != null,
"Filter and authorization Expressions cannot both be null");
SpelExpressionParser parser = new SpelExpressionParser();
this.filterExpression = filterExpression == null ? null : parser.parseExpression(filterExpression);
this.authorizeExpression = authorizeExpression == null ? null : parser.parseExpression(authorizeExpression);
this.filterExpression = (filterExpression != null) ? parser.parseExpression(filterExpression) : null;
this.authorizeExpression = (authorizeExpression != null) ? parser.parseExpression(authorizeExpression) : null;
}
AbstractExpressionBasedMethodConfigAttribute(Expression filterExpression, Expression authorizeExpression)
throws ParseException {
Assert.isTrue(filterExpression != null || authorizeExpression != null,
"Filter and authorization Expressions cannot both be null");
this.filterExpression = filterExpression == null ? null : filterExpression;
this.authorizeExpression = authorizeExpression == null ? null : authorizeExpression;
this.filterExpression = (filterExpression != null) ? filterExpression : null;
this.authorizeExpression = (authorizeExpression != null) ? authorizeExpression : null;
}
Expression getFilterExpression() {

View File

@@ -49,10 +49,10 @@ public class ExpressionBasedAnnotationAttributeFactory implements PrePostInvocat
try {
// TODO: Optimization of permitAll
ExpressionParser parser = getParser();
Expression preAuthorizeExpression = preAuthorizeAttribute == null ? parser.parseExpression("permitAll")
: parser.parseExpression(preAuthorizeAttribute);
Expression preFilterExpression = preFilterAttribute == null ? null
: parser.parseExpression(preFilterAttribute);
Expression preAuthorizeExpression = (preAuthorizeAttribute != null)
? parser.parseExpression(preAuthorizeAttribute) : parser.parseExpression("permitAll");
Expression preFilterExpression = (preFilterAttribute != null) ? parser.parseExpression(preFilterAttribute)
: null;
return new PreInvocationExpressionAttribute(preFilterExpression, filterObject, preAuthorizeExpression);
}
catch (ParseException ex) {
@@ -65,11 +65,10 @@ public class ExpressionBasedAnnotationAttributeFactory implements PrePostInvocat
String postAuthorizeAttribute) {
try {
ExpressionParser parser = getParser();
Expression postAuthorizeExpression = postAuthorizeAttribute == null ? null
: parser.parseExpression(postAuthorizeAttribute);
Expression postFilterExpression = postFilterAttribute == null ? null
: parser.parseExpression(postFilterAttribute);
Expression postAuthorizeExpression = (postAuthorizeAttribute != null)
? parser.parseExpression(postAuthorizeAttribute) : null;
Expression postFilterExpression = (postFilterAttribute != null)
? parser.parseExpression(postFilterAttribute) : null;
if (postFilterExpression != null || postAuthorizeExpression != null) {
return new PostInvocationExpressionAttribute(postFilterExpression, postAuthorizeExpression);
}

View File

@@ -41,8 +41,8 @@ class PostInvocationExpressionAttribute extends AbstractExpressionBasedMethodCon
StringBuilder sb = new StringBuilder();
Expression authorize = getAuthorizeExpression();
Expression filter = getFilterExpression();
sb.append("[authorize: '").append(authorize == null ? "null" : authorize.getExpressionString());
sb.append("', filter: '").append(filter == null ? "null" : filter.getExpressionString()).append("']");
sb.append("[authorize: '").append((authorize != null) ? authorize.getExpressionString() : "null");
sb.append("', filter: '").append((filter != null) ? filter.getExpressionString() : "null").append("']");
return sb.toString();
}

View File

@@ -57,8 +57,8 @@ class PreInvocationExpressionAttribute extends AbstractExpressionBasedMethodConf
StringBuilder sb = new StringBuilder();
Expression authorize = getAuthorizeExpression();
Expression filter = getFilterExpression();
sb.append("[authorize: '").append(authorize == null ? "null" : authorize.getExpressionString());
sb.append("', filter: '").append(filter == null ? "null" : filter.getExpressionString());
sb.append("[authorize: '").append((authorize != null) ? authorize.getExpressionString() : "null");
sb.append("', filter: '").append((filter != null) ? filter.getExpressionString() : "null");
sb.append("', filterTarget: '").append(this.filterTarget).append("']");
return sb.toString();
}

View File

@@ -73,7 +73,7 @@ public class RunAsUserToken extends AbstractAuthenticationToken {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(super.toString());
String className = this.originalAuthentication == null ? null : this.originalAuthentication.getName();
String className = (this.originalAuthentication != null) ? this.originalAuthentication.getName() : null;
sb.append("; Original Class: ").append(className);
return sb.toString();

View File

@@ -44,7 +44,7 @@ public abstract class AbstractMethodSecurityMetadataSource implements MethodSecu
Class<?> targetClass = null;
if (target != null) {
targetClass = target instanceof Class<?> ? (Class<?>) target
targetClass = (target instanceof Class<?>) ? (Class<?>) target
: AopProxyUtils.ultimateTargetClass(target);
}
Collection<ConfigAttribute> attrs = getAttributes(mi.getMethod(), targetClass);

View File

@@ -122,12 +122,12 @@ public final class DelegatingMethodSecurityMetadataSource extends AbstractMethod
@Override
public int hashCode() {
return this.method.hashCode() * 21 + (this.targetClass != null ? this.targetClass.hashCode() : 0);
return this.method.hashCode() * 21 + ((this.targetClass != null) ? this.targetClass.hashCode() : 0);
}
@Override
public String toString() {
return "CacheKey[" + (this.targetClass == null ? "-" : this.targetClass.getName()) + "; " + this.method
return "CacheKey[" + ((this.targetClass != null) ? this.targetClass.getName() : "-") + "; " + this.method
+ "]";
}

View File

@@ -93,17 +93,17 @@ public class PrePostAdviceReactiveMethodInterceptor implements MethodInterceptor
if (Mono.class.isAssignableFrom(returnType)) {
return toInvoke.flatMap((auth) -> PrePostAdviceReactiveMethodInterceptor.<Mono<?>>proceed(invocation)
.map((r) -> attr == null ? r : this.postAdvice.after(auth, invocation, attr, r)));
.map((r) -> (attr != null) ? this.postAdvice.after(auth, invocation, attr, r) : r));
}
if (Flux.class.isAssignableFrom(returnType)) {
return toInvoke.flatMapMany((auth) -> PrePostAdviceReactiveMethodInterceptor.<Flux<?>>proceed(invocation)
.map((r) -> attr == null ? r : this.postAdvice.after(auth, invocation, attr, r)));
.map((r) -> (attr != null) ? this.postAdvice.after(auth, invocation, attr, r) : r));
}
return toInvoke.flatMapMany(
(auth) -> Flux.from(PrePostAdviceReactiveMethodInterceptor.<Publisher<?>>proceed(invocation))
.map((r) -> attr == null ? r : this.postAdvice.after(auth, invocation, attr, r)));
.map((r) -> (attr != null) ? this.postAdvice.after(auth, invocation, attr, r) : r));
}
private static <T extends Publisher<?>> T proceed(final MethodInvocation invocation) {

View File

@@ -76,11 +76,11 @@ public class PrePostAnnotationSecurityMetadataSource extends AbstractMethodSecur
return Collections.emptyList();
}
String preFilterAttribute = preFilter == null ? null : preFilter.value();
String filterObject = preFilter == null ? null : preFilter.filterTarget();
String preAuthorizeAttribute = preAuthorize == null ? null : preAuthorize.value();
String postFilterAttribute = postFilter == null ? null : postFilter.value();
String postAuthorizeAttribute = postAuthorize == null ? null : postAuthorize.value();
String preFilterAttribute = (preFilter != null) ? preFilter.value() : null;
String filterObject = (preFilter != null) ? preFilter.filterTarget() : null;
String preAuthorizeAttribute = (preAuthorize != null) ? preAuthorize.value() : null;
String postFilterAttribute = (postFilter != null) ? postFilter.value() : null;
String postAuthorizeAttribute = (postAuthorize != null) ? postAuthorize.value() : null;
ArrayList<ConfigAttribute> attrs = new ArrayList<>(2);

View File

@@ -129,7 +129,7 @@ public class DefaultAuthenticationEventPublisher
private Constructor<? extends AbstractAuthenticationEvent> getEventConstructor(AuthenticationException exception) {
Constructor<? extends AbstractAuthenticationEvent> eventConstructor = this.exceptionMappings
.get(exception.getClass().getName());
return (eventConstructor == null ? this.defaultAuthenticationFailureEventConstructor : eventConstructor);
return (eventConstructor != null) ? eventConstructor : this.defaultAuthenticationFailureEventConstructor;
}
@Override

View File

@@ -80,7 +80,7 @@ public class InMemoryConfiguration extends Configuration {
@Override
public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
AppConfigurationEntry[] mappedResult = this.mappedConfigurations.get(name);
return mappedResult == null ? this.defaultConfiguration : mappedResult;
return (mappedResult != null) ? mappedResult : this.defaultConfiguration;
}
/**

View File

@@ -63,7 +63,7 @@ public class RemoteAuthenticationProvider implements AuthenticationProvider, Ini
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getPrincipal().toString();
Object credentials = authentication.getCredentials();
String password = credentials == null ? null : credentials.toString();
String password = (credentials != null) ? credentials.toString() : null;
Collection<? extends GrantedAuthority> authorities = this.remoteAuthenticationManager
.attemptAuthentication(username, password);

View File

@@ -113,8 +113,8 @@ public final class DelegatingSecurityContextCallable<V> implements Callable<V> {
* @return
*/
public static <V> Callable<V> create(Callable<V> delegate, SecurityContext securityContext) {
return securityContext == null ? new DelegatingSecurityContextCallable<>(delegate)
: new DelegatingSecurityContextCallable<>(delegate, securityContext);
return (securityContext != null) ? new DelegatingSecurityContextCallable<>(delegate, securityContext)
: new DelegatingSecurityContextCallable<>(delegate);
}
}

View File

@@ -111,8 +111,8 @@ public final class DelegatingSecurityContextRunnable implements Runnable {
*/
public static Runnable create(Runnable delegate, SecurityContext securityContext) {
Assert.notNull(delegate, "delegate cannot be null");
return securityContext == null ? new DelegatingSecurityContextRunnable(delegate)
: new DelegatingSecurityContextRunnable(delegate, securityContext);
return (securityContext != null) ? new DelegatingSecurityContextRunnable(delegate, securityContext)
: new DelegatingSecurityContextRunnable(delegate);
}
}

View File

@@ -55,7 +55,7 @@ public final class SpringSecurityCoreVersion {
public static String getVersion() {
Package pkg = SpringSecurityCoreVersion.class.getPackage();
return (pkg != null ? pkg.getImplementationVersion() : null);
return (pkg != null) ? pkg.getImplementationVersion() : null;
}
/**

View File

@@ -109,7 +109,7 @@ public class SimpleAttributes2GrantedAuthoritiesMapper
}
private String getAttributePrefix() {
return this.attributePrefix == null ? "" : this.attributePrefix;
return (this.attributePrefix != null) ? this.attributePrefix : "";
}
public void setAttributePrefix(String string) {

View File

@@ -67,7 +67,7 @@ public class MapReactiveUserDetailsService implements ReactiveUserDetailsService
public Mono<UserDetails> findByUsername(String username) {
String key = getKey(username);
UserDetails result = this.users.get(key);
return result == null ? Mono.empty() : Mono.just(User.withUserDetails(result).build());
return (result != null) ? Mono.just(User.withUserDetails(result).build()) : Mono.empty();
}
@Override

View File

@@ -43,7 +43,7 @@ public class SpringCacheBasedUserCache implements UserCache {
@Override
public UserDetails getUserFromCache(String username) {
Cache.ValueWrapper element = username != null ? this.cache.get(username) : null;
Cache.ValueWrapper element = (username != null) ? this.cache.get(username) : null;
if (logger.isDebugEnabled()) {
logger.debug("Cache hit: " + (element != null) + "; username: " + username);

View File

@@ -37,7 +37,7 @@ public class SimpleMethodInvocation implements MethodInvocation {
public SimpleMethodInvocation(Object targetObject, Method method, Object... arguments) {
this.targetObject = targetObject;
this.method = method;
this.arguments = arguments == null ? new Object[0] : arguments;
this.arguments = (arguments != null) ? arguments : new Object[0];
}
public SimpleMethodInvocation() {