Skip to content

Commit 2e56361

Browse files
committed
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.
1 parent 9b0162d commit 2e56361

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

Diff for: spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,12 @@
2525
import org.springframework.lang.Nullable;
2626

2727
/**
28-
* Cache specific evaluation context that adds a method parameters as SpEL
29-
* variables, in a lazy manner. The lazy nature eliminates unneeded
30-
* parsing of classes byte code for parameter discovery.
28+
* Cache-specific evaluation context that adds method parameters as SpEL
29+
* variables, in a lazy manner. The lazy nature avoids unnecessary
30+
* parsing of a class's byte code for parameter discovery.
3131
*
32-
* <p>Also define a set of "unavailable variables" (i.e. variables that should
33-
* lead to an exception right the way when they are accessed). This can be useful
32+
* <p>Also defines a set of "unavailable variables" (i.e. variables that should
33+
* lead to an exception as soon as they are accessed). This can be useful
3434
* to verify a condition does not match even when not all potential variables
3535
* are present.
3636
*
@@ -55,10 +55,10 @@ class CacheEvaluationContext extends MethodBasedEvaluationContext {
5555

5656

5757
/**
58-
* Add the specified variable name as unavailable for that context.
59-
* Any expression trying to access this variable should lead to an exception.
60-
* <p>This permits the validation of expressions that could potentially a
61-
* variable even when such variable isn't available yet. Any expression
58+
* Add the specified variable name as unavailable for this context.
59+
* <p>Any expression trying to access this variable should lead to an exception.
60+
* <p>This permits the validation of expressions that could potentially access
61+
* a variable even when such a variable isn't available yet. Any expression
6262
* trying to use that variable should therefore fail to evaluate.
6363
*/
6464
public void addUnavailableVariable(String name) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,26 +19,17 @@
1919
import org.springframework.expression.EvaluationException;
2020

2121
/**
22-
* A specific {@link EvaluationException} to mention that a given variable
23-
* used in the expression is not available in the context.
22+
* An internal {@link EvaluationException} which signals that a given variable
23+
* used in an expression is not available in the context.
2424
*
2525
* @author Stephane Nicoll
2626
* @since 4.0.6
2727
*/
2828
@SuppressWarnings("serial")
2929
class VariableNotAvailableException extends EvaluationException {
3030

31-
private final String name;
32-
33-
3431
public VariableNotAvailableException(String name) {
35-
super("Variable not available");
36-
this.name = name;
37-
}
38-
39-
40-
public final String getName() {
41-
return this.name;
32+
super("Variable '" + name + "' not available");
4233
}
4334

4435
}

Diff for: spring-context/src/test/java/org/springframework/cache/interceptor/ExpressionEvaluatorTests.java renamed to spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4444

4545
/**
46+
* Tests for {@link CacheOperationExpressionEvaluator}.
47+
*
4648
* @author Costin Leau
4749
* @author Phillip Webb
4850
* @author Sam Brannen
4951
* @author Stephane Nicoll
5052
*/
51-
class ExpressionEvaluatorTests {
53+
class CacheOperationExpressionEvaluatorTests {
5254

5355
private final StandardEvaluationContext originalEvaluationContext = new StandardEvaluationContext();
5456

@@ -125,9 +127,9 @@ void withoutReturnValue() {
125127
@Test
126128
void unavailableReturnValue() {
127129
EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE);
128-
assertThatExceptionOfType(VariableNotAvailableException.class).isThrownBy(() ->
129-
new SpelExpressionParser().parseExpression("#result").getValue(context))
130-
.satisfies(ex -> assertThat(ex.getName()).isEqualTo("result"));
130+
assertThatExceptionOfType(VariableNotAvailableException.class)
131+
.isThrownBy(() -> new SpelExpressionParser().parseExpression("#result").getValue(context))
132+
.withMessage("Variable 'result' not available");
131133
}
132134

133135
@Test

0 commit comments

Comments
 (0)