Skip to content

Reuse parsed expression in SpelValueExpressionResolver #46253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package org.springframework.boot.observation.autoconfigure;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.micrometer.common.annotation.ValueExpressionResolver;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

Expand All @@ -30,17 +32,23 @@
*/
class SpelValueExpressionResolver implements ValueExpressionResolver {

private final Map<String, Expression> expressionMap = new ConcurrentHashMap<>();

@Override
public String resolve(String expression, Object parameter) {
try {
SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
ExpressionParser expressionParser = new SpelExpressionParser();
Expression expressionToEvaluate = expressionParser.parseExpression(expression);
return expressionToEvaluate.getValue(context, parameter, String.class);
Expression parsedExpression = this.expressionMap.computeIfAbsent(expression,
SpelValueExpressionResolver::parseExpression);
return parsedExpression.getValue(context, parameter, String.class);
}
catch (Exception ex) {
throw new IllegalStateException("Unable to evaluate SpEL expression '%s'".formatted(expression), ex);
}
}

private static Expression parseExpression(String expression) {
return new SpelExpressionParser().parseExpression(expression);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.junit.jupiter.api.Test;

import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;

Expand All @@ -44,6 +46,19 @@ void checkInvalidExpression() {
assertThatIllegalStateException().isThrownBy(() -> this.resolver.resolve("['bar'].first", value));
}

@Test
void checkParserReuse() {
var map = (Map<?, ?>) ReflectionTestUtils.getField(this.resolver, "expressionMap");

this.resolver.resolve("length", "foo");
this.resolver.resolve("length", "bar");

assertThat(map).hasSize(1);

this.resolver.resolve("isEmpty", "foo");
assertThat(map).hasSize(2);
}

record Pair(int first, int second) {

static Pair of(int first, int second) {
Expand Down