Browse Source

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.
pull/32150/head
Sam Brannen 2 years ago
parent
commit
2e56361fe4
  1. 20
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java
  2. 17
      spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java
  3. 10
      spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java

20
spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvaluationContext.java vendored

@ -1,5 +1,5 @@ @@ -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; @@ -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.
*
* <p>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
* <p>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 { @@ -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.
* <p>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.
* <p>Any expression trying to access this variable should lead to an exception.
* <p>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) {

17
spring-context/src/main/java/org/springframework/cache/interceptor/VariableNotAvailableException.java vendored

@ -1,5 +1,5 @@ @@ -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; @@ -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; @@ -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");
}
}

10
spring-context/src/test/java/org/springframework/cache/interceptor/ExpressionEvaluatorTests.java → spring-context/src/test/java/org/springframework/cache/interceptor/CacheOperationExpressionEvaluatorTests.java vendored

@ -43,12 +43,14 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -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 { @@ -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
Loading…
Cancel
Save