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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; import org.springframework.lang.Nullable;
/** /**
* Cache specific evaluation context that adds a method parameters as SpEL * Cache-specific evaluation context that adds method parameters as SpEL
* variables, in a lazy manner. The lazy nature eliminates unneeded * variables, in a lazy manner. The lazy nature avoids unnecessary
* parsing of classes byte code for parameter discovery. * parsing of a class's byte code for parameter discovery.
* *
* <p>Also define a set of "unavailable variables" (i.e. variables that should * <p>Also defines 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 * 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 * to verify a condition does not match even when not all potential variables
* are present. * are present.
* *
@ -55,10 +55,10 @@ class CacheEvaluationContext extends MethodBasedEvaluationContext {
/** /**
* Add the specified variable name as unavailable for that context. * Add the specified variable name as unavailable for this context.
* Any expression trying to access this variable should lead to an exception. * <p>Any expression trying to access this variable should lead to an exception.
* <p>This permits the validation of expressions that could potentially a * <p>This permits the validation of expressions that could potentially access
* variable even when such variable isn't available yet. Any expression * a variable even when such a variable isn't available yet. Any expression
* trying to use that variable should therefore fail to evaluate. * trying to use that variable should therefore fail to evaluate.
*/ */
public void addUnavailableVariable(String name) { public void addUnavailableVariable(String name) {

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; import org.springframework.expression.EvaluationException;
/** /**
* A specific {@link EvaluationException} to mention that a given variable * An internal {@link EvaluationException} which signals that a given variable
* used in the expression is not available in the context. * used in an expression is not available in the context.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 4.0.6 * @since 4.0.6
@ -28,17 +28,8 @@ import org.springframework.expression.EvaluationException;
@SuppressWarnings("serial") @SuppressWarnings("serial")
class VariableNotAvailableException extends EvaluationException { class VariableNotAvailableException extends EvaluationException {
private final String name;
public VariableNotAvailableException(String name) { public VariableNotAvailableException(String name) {
super("Variable not available"); super("Variable '" + name + "' not available");
this.name = name;
}
public final String getName() {
return this.name;
} }
} }

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;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/** /**
* Tests for {@link CacheOperationExpressionEvaluator}.
*
* @author Costin Leau * @author Costin Leau
* @author Phillip Webb * @author Phillip Webb
* @author Sam Brannen * @author Sam Brannen
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
class ExpressionEvaluatorTests { class CacheOperationExpressionEvaluatorTests {
private final StandardEvaluationContext originalEvaluationContext = new StandardEvaluationContext(); private final StandardEvaluationContext originalEvaluationContext = new StandardEvaluationContext();
@ -125,9 +127,9 @@ class ExpressionEvaluatorTests {
@Test @Test
void unavailableReturnValue() { void unavailableReturnValue() {
EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE); EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE);
assertThatExceptionOfType(VariableNotAvailableException.class).isThrownBy(() -> assertThatExceptionOfType(VariableNotAvailableException.class)
new SpelExpressionParser().parseExpression("#result").getValue(context)) .isThrownBy(() -> new SpelExpressionParser().parseExpression("#result").getValue(context))
.satisfies(ex -> assertThat(ex.getName()).isEqualTo("result")); .withMessage("Variable 'result' not available");
} }
@Test @Test
Loading…
Cancel
Save