Skip to content

Commit

Permalink
Use consistent ternary expression style
Browse files Browse the repository at this point in the history
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 spring-projectsgh-8945
  • Loading branch information
philwebb authored and rwinch committed Aug 24, 2020
1 parent 8d3f039 commit 834dcf5
Show file tree
Hide file tree
Showing 131 changed files with 277 additions and 265 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public boolean equals(Object arg0) {
@Override
public int hashCode() {
int result = this.permission.hashCode();
result = 31 * result + (this.id != null ? this.id.hashCode() : 0);
result = 31 * result + ((this.id != null) ? this.id.hashCode() : 0);
result = 31 * result + (this.sid.hashCode());
result = 31 * result + (this.auditFailure ? 1 : 0);
result = 31 * result + (this.auditSuccess ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
int result = this.parentAcl != null ? this.parentAcl.hashCode() : 0;
int result = (this.parentAcl != null) ? this.parentAcl.hashCode() : 0;
result = 31 * result + this.aclAuthorizationStrategy.hashCode();
result = 31 * result
+ (this.permissionGrantingStrategy != null ? this.permissionGrantingStrategy.hashCode() : 0);
result = 31 * result + (this.aces != null ? this.aces.hashCode() : 0);
+ ((this.permissionGrantingStrategy != null) ? this.permissionGrantingStrategy.hashCode() : 0);
result = 31 * result + ((this.aces != null) ? this.aces.hashCode() : 0);
result = 31 * result + this.objectIdentity.hashCode();
result = 31 * result + this.id.hashCode();
result = 31 * result + (this.owner != null ? this.owner.hashCode() : 0);
result = 31 * result + (this.loadedSids != null ? this.loadedSids.hashCode() : 0);
result = 31 * result + ((this.owner != null) ? this.owner.hashCode() : 0);
result = 31 * result + ((this.loadedSids != null) ? this.loadedSids.hashCode() : 0);
result = 31 * result + (this.entriesInheriting ? 1 : 0);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
* Represents a successful CAS <code>Authentication</code>.
Expand Down Expand Up @@ -141,7 +142,7 @@ public int hashCode() {
result = 31 * result + this.principal.hashCode();
result = 31 * result + this.userDetails.hashCode();
result = 31 * result + this.keyHash;
result = 31 * result + (this.assertion != null ? this.assertion.hashCode() : 0);
result = 31 * result + ObjectUtils.nullSafeHashCode(this.assertion);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CasAuthenticationToken getByTicketId(final String serviceTicket) {
logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket);
}

return element == null ? null : (CasAuthenticationToken) element.getValue();
return (element != null) ? (CasAuthenticationToken) element.getValue() : null;
}

public Ehcache getCache() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public SpringCacheBasedTicketCache(Cache cache) {

@Override
public CasAuthenticationToken getByTicketId(final String serviceTicket) {
final Cache.ValueWrapper element = serviceTicket != null ? this.cache.get(serviceTicket) : null;
final Cache.ValueWrapper element = (serviceTicket != null) ? this.cache.get(serviceTicket) : null;

if (logger.isDebugEnabled()) {
logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket);
}

return element == null ? null : (CasAuthenticationToken) element.get();
return (element != null) ? (CasAuthenticationToken) element.get() : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition,

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ protected GrantedAuthoritiesMapper getAuthoritiesMapper() throws Exception {
* @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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private RequestMatchers() {
* @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));
Expand Down Expand Up @@ -275,7 +275,7 @@ static List<RequestMatcher> antMatchers(String... antPatterns) {
* @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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public WebInvocationPrivilegeEvaluator getPrivilegeEvaluator() {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static int lookupOrder(Object obj) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private J2eePreAuthenticatedProcessingFilter getFilter(AuthenticationManager aut
* @return the {@link AuthenticationUserDetailsService} to use
*/
private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> getUserDetailsService() {
return this.authenticationUserDetailsService == null
return (this.authenticationUserDetailsService != null)
? new PreAuthenticatedGrantedAuthoritiesUserDetailsService() : this.authenticationUserDetailsService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ private RememberMeServices getRememberMeServices(H http, String key) throws Exce
* @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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void configure(H http) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public ServletApiConfigurer<H> rolePrefix(String rolePrefix) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ SessionCreationPolicy getSessionCreationPolicy() {
}

SessionCreationPolicy sessionPolicy = getBuilder().getSharedObject(SessionCreationPolicy.class);
return sessionPolicy == null ? SessionCreationPolicy.IF_REQUIRED : sessionPolicy;
return (sessionPolicy != null) ? sessionPolicy : SessionCreationPolicy.IF_REQUIRED;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void configure(B http) {
}

private String getAuthorizationRequestBaseUri() {
return this.authorizationRequestBaseUri != null ? this.authorizationRequestBaseUri
return (this.authorizationRequestBaseUri != null) ? this.authorizationRequestBaseUri
: OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ private Map<String, String> getLoginLinks() {
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<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ static RootBeanDefinition createSecurityMetadataSource(List<Element> interceptUr

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public BeanDefinition parse(Element elt, ParserContext pc) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ private String resolveAttribute(ParserContext pc, Element element, String attrib
}

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;
Expand All @@ -195,7 +195,7 @@ private void addCacheControl() {
}

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);
}
Expand Down Expand Up @@ -244,7 +244,7 @@ private void addHsts(boolean addIfNotPresent, Element hstsElement, ParserContext
}

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);
}
Expand Down Expand Up @@ -342,8 +342,8 @@ private void addContentSecurityPolicy(Element contentSecurityPolicyElement, Pars
}

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);
}
Expand All @@ -361,8 +361,8 @@ private void addReferrerPolicy(Element referrerPolicyElement, ParserContext cont
}

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);
}
Expand Down Expand Up @@ -390,8 +390,8 @@ private void attrNotAllowed(ParserContext context, String attrName, String other
}

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)) {
Expand All @@ -407,8 +407,8 @@ private void parseHeaderElements(Element element) {
}

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;
Expand Down Expand Up @@ -504,7 +504,7 @@ private BeanDefinitionBuilder getAllowFromStrategy(String strategy, String value
}

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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {

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);
Expand Down Expand Up @@ -112,7 +112,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {

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);
Expand All @@ -127,7 +127,7 @@ private BeanMetadataElement getAuthorizationRequestRepository(Element element) {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ public BeanDefinition parse(Element element, ParserContext pc) {
}
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(
Expand Down
Loading

0 comments on commit 834dcf5

Please sign in to comment.