From 1d80e9ff05f29a3a6b93837bf3246e190af579a5 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 2 Jan 2020 10:43:22 -0500 Subject: [PATCH] Fix new Sonar smells * Optimize logic in the `ExpressionEvaluatingParameterSourceFactory` do not compute object if they are present in maps. Plus extract some common code to the method --- .../http/inbound/BaseHttpInboundEndpoint.java | 4 +- ...ssionEvaluatingParameterSourceFactory.java | 90 ++++++++----------- 2 files changed, 39 insertions(+), 55 deletions(-) diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/BaseHttpInboundEndpoint.java b/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/BaseHttpInboundEndpoint.java index f1a7604c33..166d1a3afc 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/BaseHttpInboundEndpoint.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/inbound/BaseHttpInboundEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -365,7 +365,7 @@ public class BaseHttpInboundEndpoint extends MessagingGatewaySupport implements * @return true or false if HTTP request can contain the body */ protected static boolean isReadable(@Nullable HttpMethod httpMethod) { - return !(CollectionUtils.containsInstance(NON_READABLE_BODY_HTTP_METHODS, httpMethod)); + return httpMethod != null && !(CollectionUtils.containsInstance(NON_READABLE_BODY_HTTP_METHODS, httpMethod)); } } diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactory.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactory.java index 85894ad729..f118d51e7d 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactory.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,69 +111,53 @@ public class ExpressionEvaluatingParameterSourceFactory implements ParameterSour @Nullable public Object getValueByPosition(int position) { Assert.isTrue(position >= 0, "The position must be non-negative."); - if (position <= this.parameters.size()) { - - final JpaParameter parameter = this.parameters.get(position); - - if (parameter.getValue() != null) { - return parameter.getValue(); + JpaParameter parameter = this.parameters.get(position); + String parameterName = parameter.getName(); + if (parameterName != null) { + return getValue(parameterName); } - - if (parameter.getExpression() != null) { - Expression expression; - - if (this.input instanceof Collection) { - expression = parameter.getProjectionExpression(); - } - else { - expression = parameter.getSpelExpression(); - } - - final Object value = this.expressionEvaluator.evaluateExpression(expression, this.input); - if (parameter.getName() != null) { - this.values.put(parameter.getName(), value); - } - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Resolved expression " + expression + " to " + value); - } - return value; - + else { + return obtainParameterValue(parameter); } - } - return null; } @Override @Nullable public Object getValue(String paramName) { - if (this.values.containsKey(paramName)) { - return this.values.get(paramName); + return this.values.computeIfAbsent(paramName, + (key) -> { + JpaParameter jpaParameter = + this.parametersMap.computeIfAbsent(paramName, + (name) -> { + JpaParameter parameter = new JpaParameter(paramName, null, paramName); + ExpressionEvaluatingParameterSourceFactory.this.parameters.add(parameter); + return parameter; + }); + return obtainParameterValue(jpaParameter); + }); + } + + @Nullable + private Object obtainParameterValue(JpaParameter jpaParameter) { + Object value = null; + if (jpaParameter.getValue() != null) { + value = jpaParameter.getValue(); } - - if (!this.parametersMap.containsKey(paramName)) { - JpaParameter parameter = new JpaParameter(paramName, null, paramName); - ExpressionEvaluatingParameterSourceFactory.this.parameters.add(parameter); - this.parametersMap.put(paramName, parameter); - } - - JpaParameter jpaParameter = this.parametersMap.get(paramName); - - Expression expression; - - if (this.input instanceof Collection) { - expression = jpaParameter.getProjectionExpression(); - } - else { - expression = jpaParameter.getSpelExpression(); - } - - final Object value = this.expressionEvaluator.evaluateExpression(expression, this.input); - this.values.put(paramName, value); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Resolved expression " + expression + " to " + value); + if (jpaParameter.getExpression() != null) { + Expression expression; + if (this.input instanceof Collection) { + expression = jpaParameter.getProjectionExpression(); + } + else { + expression = jpaParameter.getSpelExpression(); + } + value = this.expressionEvaluator.evaluateExpression(expression, this.input); // NOSONAR + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Resolved expression " + expression + " to " + value); + } } return value; }