1
0
mirror of synced 2026-08-06 02:08:01 +00:00

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
@@ -151,7 +151,7 @@ public final class SecurityNamespaceHandler implements NamespaceHandler {
private void reportUnsupportedNodeType(String name, ParserContext pc, Node node) {
pc.getReaderContext().fatal("Security namespace does not support decoration of "
+ (node instanceof Element ? "element" : "attribute") + " [" + name + "]", node);
+ ((node instanceof Element) ? "element" : "attribute") + " [" + name + "]", node);
}
private void reportMissingWebClasses(String nodeName, ParserContext pc, Node node) {
@@ -187,8 +187,8 @@ public class LdapAuthenticationProviderConfigurer<B extends ProviderManagerBuild
* @return the {@link LdapAuthenticator} to use
*/
private LdapAuthenticator createLdapAuthenticator(BaseLdapPathContextSource contextSource) {
AbstractLdapAuthenticator ldapAuthenticator = this.passwordEncoder == null
? createBindAuthenticator(contextSource) : createPasswordCompareAuthenticator(contextSource);
AbstractLdapAuthenticator ldapAuthenticator = (this.passwordEncoder != null)
? createPasswordCompareAuthenticator(contextSource) : createBindAuthenticator(contextSource);
LdapUserSearch userSearch = createUserSearch();
if (userSearch != null) {
ldapAuthenticator.setUserSearch(userSearch);
@@ -247,7 +247,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
* @return a {@link List} of {@link AntPathRequestMatcher} instances
*/
static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
String method = httpMethod == null ? null : httpMethod.toString();
String method = (httpMethod != null) ? httpMethod.toString() : null;
List<RequestMatcher> matchers = new ArrayList<>();
for (String pattern : antPatterns) {
matchers.add(new AntPathRequestMatcher(pattern, method));
@@ -275,7 +275,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
* @return a {@link List} of {@link RegexRequestMatcher} instances
*/
static List<RequestMatcher> regexMatchers(HttpMethod httpMethod, String... regexPatterns) {
String method = httpMethod == null ? null : httpMethod.toString();
String method = (httpMethod != null) ? httpMethod.toString() : null;
List<RequestMatcher> matchers = new ArrayList<>();
for (String pattern : regexPatterns) {
matchers.add(new RegexRequestMatcher(pattern, method));
@@ -242,8 +242,8 @@ public final class WebSecurity extends AbstractConfiguredSecurityBuilder<Filter,
if (this.privilegeEvaluator != null) {
return this.privilegeEvaluator;
}
return this.filterSecurityInterceptor == null ? null
: new DefaultWebInvocationPrivilegeEvaluator(this.filterSecurityInterceptor);
return (this.filterSecurityInterceptor != null)
? new DefaultWebInvocationPrivilegeEvaluator(this.filterSecurityInterceptor) : null;
}
/**
@@ -227,7 +227,7 @@ public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAwa
return ((Ordered) obj).getOrder();
}
if (obj != null) {
Class<?> clazz = (obj instanceof Class ? (Class<?>) obj : obj.getClass());
Class<?> clazz = ((obj instanceof Class) ? (Class<?>) obj : obj.getClass());
Order order = AnnotationUtils.findAnnotation(clazz, Order.class);
if (order != null) {
return order.value();
@@ -230,7 +230,7 @@ public final class JeeConfigurer<H extends HttpSecurityBuilder<H>> extends Abstr
* @return the {@link AuthenticationUserDetailsService} to use
*/
private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> getUserDetailsService() {
return this.authenticationUserDetailsService == null
return (this.authenticationUserDetailsService != null)
? new PreAuthenticatedGrantedAuthoritiesUserDetailsService() : this.authenticationUserDetailsService;
}
@@ -369,8 +369,8 @@ public final class RememberMeConfigurer<H extends HttpSecurityBuilder<H>>
* @return the {@link RememberMeServices} to use
*/
private AbstractRememberMeServices createRememberMeServices(H http, String key) {
return this.tokenRepository == null ? createTokenBasedRememberMeServices(http, key)
: createPersistentRememberMeServices(http, key);
return (this.tokenRepository != null) ? createPersistentRememberMeServices(http, key)
: createTokenBasedRememberMeServices(http, key);
}
/**
@@ -90,8 +90,8 @@ public final class SecurityContextConfigurer<H extends HttpSecurityBuilder<H>>
SecurityContextPersistenceFilter securityContextFilter = new SecurityContextPersistenceFilter(
securityContextRepository);
SessionManagementConfigurer<?> sessionManagement = http.getConfigurer(SessionManagementConfigurer.class);
SessionCreationPolicy sessionCreationPolicy = sessionManagement == null ? null
: sessionManagement.getSessionCreationPolicy();
SessionCreationPolicy sessionCreationPolicy = (sessionManagement != null)
? sessionManagement.getSessionCreationPolicy() : null;
if (SessionCreationPolicy.ALWAYS == sessionCreationPolicy) {
securityContextFilter.setForceEagerSessionCreation(true);
}
@@ -80,11 +80,11 @@ public final class ServletApiConfigurer<H extends HttpSecurityBuilder<H>>
public void configure(H http) {
this.securityContextRequestFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
ExceptionHandlingConfigurer<H> exceptionConf = http.getConfigurer(ExceptionHandlingConfigurer.class);
AuthenticationEntryPoint authenticationEntryPoint = exceptionConf == null ? null
: exceptionConf.getAuthenticationEntryPoint(http);
AuthenticationEntryPoint authenticationEntryPoint = (exceptionConf != null)
? exceptionConf.getAuthenticationEntryPoint(http) : null;
this.securityContextRequestFilter.setAuthenticationEntryPoint(authenticationEntryPoint);
LogoutConfigurer<H> logoutConf = http.getConfigurer(LogoutConfigurer.class);
List<LogoutHandler> logoutHandlers = logoutConf == null ? null : logoutConf.getLogoutHandlers();
List<LogoutHandler> logoutHandlers = (logoutConf != null) ? logoutConf.getLogoutHandlers() : null;
this.securityContextRequestFilter.setLogoutHandlers(logoutHandlers);
AuthenticationTrustResolver trustResolver = http.getSharedObject(AuthenticationTrustResolver.class);
if (trustResolver != null) {
@@ -451,7 +451,7 @@ public final class SessionManagementConfigurer<H extends HttpSecurityBuilder<H>>
}
SessionCreationPolicy sessionPolicy = getBuilder().getSharedObject(SessionCreationPolicy.class);
return sessionPolicy == null ? SessionCreationPolicy.IF_REQUIRED : sessionPolicy;
return (sessionPolicy != null) ? sessionPolicy : SessionCreationPolicy.IF_REQUIRED;
}
/**
@@ -98,7 +98,7 @@ public final class ImplicitGrantConfigurer<B extends HttpSecurityBuilder<B>>
}
private String getAuthorizationRequestBaseUri() {
return this.authorizationRequestBaseUri != null ? this.authorizationRequestBaseUri
return (this.authorizationRequestBaseUri != null) ? this.authorizationRequestBaseUri
: OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI;
}
@@ -500,7 +500,7 @@ public final class OAuth2LoginConfigurer<B extends HttpSecurityBuilder<B>>
return Collections.emptyMap();
}
String authorizationRequestBaseUri = this.authorizationEndpointConfig.authorizationRequestBaseUri != null
String authorizationRequestBaseUri = (this.authorizationEndpointConfig.authorizationRequestBaseUri != null)
? this.authorizationEndpointConfig.authorizationRequestBaseUri
: OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI;
Map<String, String> loginUrlToClientName = new HashMap<>();
@@ -107,8 +107,8 @@ public class FilterInvocationSecurityMetadataSourceParser implements BeanDefinit
if (useExpressions) {
Element expressionHandlerElt = DomUtils.getChildElementByTagName(httpElt, Elements.EXPRESSION_HANDLER);
String expressionHandlerRef = expressionHandlerElt == null ? null
: expressionHandlerElt.getAttribute("ref");
String expressionHandlerRef = (expressionHandlerElt != null) ? expressionHandlerElt.getAttribute("ref")
: null;
if (StringUtils.hasText(expressionHandlerRef)) {
logger.info(
@@ -168,7 +168,7 @@ public class FormLoginBeanDefinitionParser {
BeanDefinitionBuilder entryPointBuilder = BeanDefinitionBuilder
.rootBeanDefinition(LoginUrlAuthenticationEntryPoint.class);
entryPointBuilder.getRawBeanDefinition().setSource(source);
entryPointBuilder.addConstructorArgValue(this.loginPage != null ? this.loginPage : DEF_LOGIN_PAGE);
entryPointBuilder.addConstructorArgValue((this.loginPage != null) ? this.loginPage : DEF_LOGIN_PAGE);
entryPointBuilder.addPropertyValue("portMapper", this.portMapper);
entryPointBuilder.addPropertyValue("portResolver", this.portResolver);
this.entryPointBean = (RootBeanDefinition) entryPointBuilder.getBeanDefinition();
@@ -177,8 +177,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseCacheControlElement(boolean addIfNotPresent, Element element) {
Element cacheControlElement = element == null ? null
: DomUtils.getChildElementByTagName(element, CACHE_CONTROL_ELEMENT);
Element cacheControlElement = (element != null)
? DomUtils.getChildElementByTagName(element, CACHE_CONTROL_ELEMENT) : null;
boolean disabled = "true".equals(getAttribute(cacheControlElement, ATT_DISABLED, "false"));
if (disabled) {
return;
@@ -195,7 +195,7 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseHstsElement(boolean addIfNotPresent, Element element, ParserContext context) {
Element hstsElement = element == null ? null : DomUtils.getChildElementByTagName(element, HSTS_ELEMENT);
Element hstsElement = (element != null) ? DomUtils.getChildElementByTagName(element, HSTS_ELEMENT) : null;
if (addIfNotPresent || hstsElement != null) {
addHsts(addIfNotPresent, hstsElement, context);
}
@@ -244,7 +244,7 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseHpkpElement(boolean addIfNotPresent, Element element, ParserContext context) {
Element hpkpElement = element == null ? null : DomUtils.getChildElementByTagName(element, HPKP_ELEMENT);
Element hpkpElement = (element != null) ? DomUtils.getChildElementByTagName(element, HPKP_ELEMENT) : null;
if (addIfNotPresent || hpkpElement != null) {
addHpkp(addIfNotPresent, hpkpElement, context);
}
@@ -342,8 +342,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseReferrerPolicyElement(Element element, ParserContext context) {
Element referrerPolicyElement = (element == null) ? null
: DomUtils.getChildElementByTagName(element, REFERRER_POLICY_ELEMENT);
Element referrerPolicyElement = (element != null)
? DomUtils.getChildElementByTagName(element, REFERRER_POLICY_ELEMENT) : null;
if (referrerPolicyElement != null) {
addReferrerPolicy(referrerPolicyElement, context);
}
@@ -361,8 +361,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseFeaturePolicyElement(Element element, ParserContext context) {
Element featurePolicyElement = (element == null) ? null
: DomUtils.getChildElementByTagName(element, FEATURE_POLICY_ELEMENT);
Element featurePolicyElement = (element != null)
? DomUtils.getChildElementByTagName(element, FEATURE_POLICY_ELEMENT) : null;
if (featurePolicyElement != null) {
addFeaturePolicy(featurePolicyElement, context);
}
@@ -390,8 +390,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseHeaderElements(Element element) {
List<Element> headerElts = element == null ? Collections.<Element>emptyList()
: DomUtils.getChildElementsByTagName(element, GENERIC_HEADER_ELEMENT);
List<Element> headerElts = (element != null)
? DomUtils.getChildElementsByTagName(element, GENERIC_HEADER_ELEMENT) : Collections.emptyList();
for (Element headerElt : headerElts) {
String headerFactoryRef = headerElt.getAttribute(ATT_REF);
if (StringUtils.hasText(headerFactoryRef)) {
@@ -407,8 +407,8 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseContentTypeOptionsElement(boolean addIfNotPresent, Element element) {
Element contentTypeElt = element == null ? null
: DomUtils.getChildElementByTagName(element, CONTENT_TYPE_ELEMENT);
Element contentTypeElt = (element != null) ? DomUtils.getChildElementByTagName(element, CONTENT_TYPE_ELEMENT)
: null;
boolean disabled = "true".equals(getAttribute(contentTypeElt, ATT_DISABLED, "false"));
if (disabled) {
return;
@@ -504,7 +504,7 @@ public class HeadersBeanDefinitionParser implements BeanDefinitionParser {
}
private void parseXssElement(boolean addIfNotPresent, Element element, ParserContext parserContext) {
Element xssElt = element == null ? null : DomUtils.getChildElementByTagName(element, XSS_ELEMENT);
Element xssElt = (element != null) ? DomUtils.getChildElementByTagName(element, XSS_ELEMENT) : null;
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XXssProtectionHeaderWriter.class);
if (xssElt != null) {
boolean disabled = "true".equals(getAttribute(xssElt, ATT_DISABLED, "false"));
@@ -83,7 +83,7 @@ final class OAuth2ClientBeanDefinitionParser implements BeanDefinitionParser {
BeanDefinitionBuilder authorizationRequestRedirectFilterBuilder = BeanDefinitionBuilder
.rootBeanDefinition(OAuth2AuthorizationRequestRedirectFilter.class);
String authorizationRequestResolverRef = authorizationCodeGrantElt != null
String authorizationRequestResolverRef = (authorizationCodeGrantElt != null)
? authorizationCodeGrantElt.getAttribute(ATT_AUTHORIZATION_REQUEST_RESOLVER_REF) : null;
if (!StringUtils.isEmpty(authorizationRequestResolverRef)) {
authorizationRequestRedirectFilterBuilder.addConstructorArgReference(authorizationRequestResolverRef);
@@ -112,7 +112,7 @@ final class OAuth2ClientBeanDefinitionParser implements BeanDefinitionParser {
private BeanMetadataElement getAuthorizationRequestRepository(Element element) {
BeanMetadataElement authorizationRequestRepository;
String authorizationRequestRepositoryRef = element != null
String authorizationRequestRepositoryRef = (element != null)
? element.getAttribute(ATT_AUTHORIZATION_REQUEST_REPOSITORY_REF) : null;
if (!StringUtils.isEmpty(authorizationRequestRepositoryRef)) {
authorizationRequestRepository = new RuntimeBeanReference(authorizationRequestRepositoryRef);
@@ -127,7 +127,7 @@ final class OAuth2ClientBeanDefinitionParser implements BeanDefinitionParser {
private BeanMetadataElement getAccessTokenResponseClient(Element element) {
BeanMetadataElement accessTokenResponseClient;
String accessTokenResponseClientRef = element != null
String accessTokenResponseClientRef = (element != null)
? element.getAttribute(ATT_ACCESS_TOKEN_RESPONSE_CLIENT_REF) : null;
if (!StringUtils.isEmpty(accessTokenResponseClientRef)) {
accessTokenResponseClient = new RuntimeBeanReference(accessTokenResponseClientRef);
@@ -178,8 +178,8 @@ public class GlobalMethodSecurityBeanDefinitionParser implements BeanDefinitionP
}
else {
// The default expression-based system
String expressionHandlerRef = expressionHandlerElt == null ? null
: expressionHandlerElt.getAttribute("ref");
String expressionHandlerRef = (expressionHandlerElt != null) ? expressionHandlerElt.getAttribute("ref")
: null;
if (StringUtils.hasText(expressionHandlerRef)) {
this.logger.info(
@@ -168,7 +168,7 @@ public final class ClientRegistrationsBeanDefinitionParser implements BeanDefini
private static ClientRegistration.Builder getBuilderFromIssuerIfPossible(String registrationId,
String configuredProviderId, Map<String, Map<String, String>> providers) {
String providerId = configuredProviderId != null ? configuredProviderId : registrationId;
String providerId = (configuredProviderId != null) ? configuredProviderId : registrationId;
if (providers.containsKey(providerId)) {
Map<String, String> provider = providers.get(providerId);
String issuer = provider.get(ATT_ISSUER_URI);
@@ -188,7 +188,7 @@ public final class ClientRegistrationsBeanDefinitionParser implements BeanDefini
if (provider == null && !providers.containsKey(providerId)) {
return null;
}
ClientRegistration.Builder builder = provider != null ? provider.getBuilder(registrationId)
ClientRegistration.Builder builder = (provider != null) ? provider.getBuilder(registrationId)
: ClientRegistration.withRegistrationId(registrationId);
if (providers.containsKey(providerId)) {
return getBuilder(builder, providers.get(providerId));
@@ -251,7 +251,7 @@ public final class ClientRegistrationsBeanDefinitionParser implements BeanDefini
}
private static String getErrorMessage(String configuredProviderId, String registrationId) {
return configuredProviderId != null ? "Unknown provider ID '" + configuredProviderId + "'"
return (configuredProviderId != null) ? "Unknown provider ID '" + configuredProviderId + "'"
: "Provider ID must be specified for client registration '" + registrationId + "'";
}
@@ -1467,8 +1467,8 @@ public class ServerHttpSecurity {
}
private WebFilter securityContextRepositoryWebFilter() {
ServerSecurityContextRepository repository = this.securityContextRepository == null
? new WebSessionServerSecurityContextRepository() : this.securityContextRepository;
ServerSecurityContextRepository repository = (this.securityContextRepository != null)
? this.securityContextRepository : new WebSessionServerSecurityContextRepository();
WebFilter result = new ReactorContextWebFilter(repository);
return new OrderedWebFilter(result, SecurityWebFiltersOrder.REACTOR_CONTEXT.getOrder());
}
@@ -121,7 +121,7 @@ public final class WebSocketMessageBrokerSecurityBeanDefinitionParser implements
String id = element.getAttribute(ID_ATTR);
Element expressionHandlerElt = DomUtils.getChildElementByTagName(element, Elements.EXPRESSION_HANDLER);
String expressionHandlerRef = expressionHandlerElt == null ? null : expressionHandlerElt.getAttribute("ref");
String expressionHandlerRef = (expressionHandlerElt != null) ? expressionHandlerElt.getAttribute("ref") : null;
boolean expressionHandlerDefined = StringUtils.hasText(expressionHandlerRef);
boolean sameOriginDisabled = Boolean.parseBoolean(element.getAttribute(DISABLED_ATTR));
@@ -252,7 +252,7 @@ public final class WebSocketMessageBrokerSecurityBeanDefinitionParser implements
if (!registry.containsBeanDefinition(PATH_MATCHER_BEAN_NAME)) {
PropertyValue pathMatcherProp = bd.getPropertyValues().getPropertyValue("pathMatcher");
Object pathMatcher = pathMatcherProp == null ? null : pathMatcherProp.getValue();
Object pathMatcher = (pathMatcherProp != null) ? pathMatcherProp.getValue() : null;
if (pathMatcher instanceof BeanReference) {
registry.registerAlias(((BeanReference) pathMatcher).getBeanName(), PATH_MATCHER_BEAN_NAME);
}
@@ -237,7 +237,7 @@ public class OAuth2ClientConfigurationTests {
@GetMapping("/authorized-client")
String authorizedClient(
@RegisteredOAuth2AuthorizedClient("client1") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
return (authorizedClient != null) ? "resolved" : "not-resolved";
}
}
@@ -405,7 +405,7 @@ public class OAuth2ClientConfigurationTests {
@GetMapping("/authorized-client")
String authorizedClient(
@RegisteredOAuth2AuthorizedClient("client1") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
return (authorizedClient != null) ? "resolved" : "not-resolved";
}
}
@@ -229,7 +229,7 @@ public class OAuth2ClientBeanDefinitionParserTests {
@GetMapping("/authorized-client")
String authorizedClient(Model model,
@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
return (authorizedClient != null) ? "resolved" : "not-resolved";
}
}
@@ -523,7 +523,7 @@ public class OAuth2LoginBeanDefinitionParserTests {
@GetMapping("/authorized-client")
String authorizedClient(Model model,
@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
return (authorizedClient != null) ? "resolved" : "not-resolved";
}
}
@@ -93,7 +93,7 @@ final class HtmlUnitWebTestClient {
contentType = encodingType.getName();
}
}
MediaType mediaType = contentType == null ? MediaType.ALL : MediaType.parseMediaType(contentType);
MediaType mediaType = (contentType != null) ? MediaType.parseMediaType(contentType) : MediaType.ALL;
request.contentType(mediaType);
}