From 2e56361fe4cb93cb8c16deabe3bde82a2ba9991c Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Sun, 28 Jan 2024 17:03:24 +0100 Subject: [PATCH] Simplify implementation of internal VariableNotAvailableException Since VariableNotAvailableException is not a public type, there is no need to store the variable name in a field/property. --- .../interceptor/CacheEvaluationContext.java | 20 +++++++++---------- .../VariableNotAvailableException.java | 17 ++++------------ ...cheOperationExpressionEvaluatorTests.java} | 10 ++++++---- 3 files changed, 20 insertions(+), 27 deletions(-) rename spring-context/src/test/java/org/springframework/cache/interceptor/{ExpressionEvaluatorTests.java => CacheOperationExpressionEvaluatorTests.java} (96%) diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java index 25d4282313e..29604d91b64 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2024 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. @@ -25,12 +25,12 @@ import org.springframework.core.ParameterNameDiscoverer; import org.springframework.lang.Nullable; /** - * Cache specific evaluation context that adds a method parameters as SpEL - * variables, in a lazy manner. The lazy nature eliminates unneeded - * parsing of classes byte code for parameter discovery. + * Cache-specific evaluation context that adds method parameters as SpEL + * variables, in a lazy manner. The lazy nature avoids unnecessary + * parsing of a class's byte code for parameter discovery. * - *

Also define a set of "unavailable variables" (i.e. variables that should - * lead to an exception right the way when they are accessed). This can be useful + *

Also defines a set of "unavailable variables" (i.e. variables that should + * lead to an exception as soon as they are accessed). This can be useful * to verify a condition does not match even when not all potential variables * are present. * @@ -55,10 +55,10 @@ class CacheEvaluationContext extends MethodBasedEvaluationContext { /** - * Add the specified variable name as unavailable for that context. - * Any expression trying to access this variable should lead to an exception. - *

This permits the validation of expressions that could potentially a - * variable even when such variable isn't available yet. Any expression + * Add the specified variable name as unavailable for this context. + *

Any expression trying to access this variable should lead to an exception. + *

This permits the validation of expressions that could potentially access + * a variable even when such a variable isn't available yet. Any expression * trying to use that variable should therefore fail to evaluate. */ public void addUnavailableVariable(String name) { diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java b/spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java index b4ce3c2b0e2..48a30ae1f8b 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2024 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. @@ -19,8 +19,8 @@ package org.springframework.cache.interceptor; import org.springframework.expression.EvaluationException; /** - * A specific {@link EvaluationException} to mention that a given variable - * used in the expression is not available in the context. + * An internal {@link EvaluationException} which signals that a given variable + * used in an expression is not available in the context. * * @author Stephane Nicoll * @since 4.0.6 @@ -28,17 +28,8 @@ import org.springframework.expression.EvaluationException; @SuppressWarnings("serial") class VariableNotAvailableException extends EvaluationException { - private final String name; - - public VariableNotAvailableException(String name) { - super("Variable not available"); - this.name = name; - } - - - public final String getName() { - return this.name; + super("Variable '" + name + "' not available"); } } diff --git a/spring-context/src/test/java/org/springframework/cache/interceptor/ExpressionEvaluatorTests.java b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java similarity index 96% rename from spring-context/src/test/java/org/springframework/cache/interceptor/ExpressionEvaluatorTests.java rename to spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java index dadfe64eda3..11625dce481 100644 --- a/spring-context/src/test/java/org/springframework/cache/interceptor/ExpressionEvaluatorTests.java +++ b/spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java @@ -43,12 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** + * Tests for {@link CacheOperationExpressionEvaluator}. + * * @author Costin Leau * @author Phillip Webb * @author Sam Brannen * @author Stephane Nicoll */ -class ExpressionEvaluatorTests { +class CacheOperationExpressionEvaluatorTests { private final StandardEvaluationContext originalEvaluationContext = new StandardEvaluationContext(); @@ -125,9 +127,9 @@ class ExpressionEvaluatorTests { @Test void unavailableReturnValue() { EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE); - assertThatExceptionOfType(VariableNotAvailableException.class).isThrownBy(() -> - new SpelExpressionParser().parseExpression("#result").getValue(context)) - .satisfies(ex -> assertThat(ex.getName()).isEqualTo("result")); + assertThatExceptionOfType(VariableNotAvailableException.class) + .isThrownBy(() -> new SpelExpressionParser().parseExpression("#result").getValue(context)) + .withMessage("Variable 'result' not available"); } @Test