From 529f311bd446b4a0d656b0cf3b6cbff9e0e58cdb Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:16:20 +0200 Subject: [PATCH] Polish and harmonize implementations of SpEL components in spring-context --- .../BeanExpressionContextAccessor.java | 16 ++++++++-------- .../expression/BeanFactoryAccessor.java | 6 +++--- .../expression/BeanFactoryResolver.java | 9 ++++----- .../expression/EnvironmentAccessor.java | 19 +++++++------------ .../context/expression/MapAccessor.java | 11 +++++++---- .../spel/CompilableMapAccessor.java | 6 ++++-- 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java b/spring-context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java index 451d81b271..961f79de3f 100644 --- a/spring-context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java +++ b/spring-context/src/main/java/org/springframework/context/expression/BeanExpressionContextAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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,8 +25,8 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * EL property accessor that knows how to traverse the beans and contextual objects - * of a Spring {@link org.springframework.beans.factory.config.BeanExpressionContext}. + * SpEL {@link PropertyAccessor} that knows how to access the beans and contextual + * objects of a Spring {@link BeanExpressionContext}. * * @author Juergen Hoeller * @author Andy Clement @@ -34,6 +34,11 @@ import org.springframework.util.Assert; */ public class BeanExpressionContextAccessor implements PropertyAccessor { + @Override + public Class[] getSpecificTargetClasses() { + return new Class[] {BeanExpressionContext.class}; + } + @Override public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException { return (target instanceof BeanExpressionContext bec && bec.containsObject(name)); @@ -57,9 +62,4 @@ public class BeanExpressionContextAccessor implements PropertyAccessor { throw new AccessException("Beans in a BeanFactory are read-only"); } - @Override - public Class[] getSpecificTargetClasses() { - return new Class[] {BeanExpressionContext.class}; - } - } diff --git a/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java b/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java index b8e90c56a7..2f7f162d10 100644 --- a/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java +++ b/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 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,8 +25,8 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * EL property accessor that knows how to traverse the beans of a - * Spring {@link org.springframework.beans.factory.BeanFactory}. + * SpEL {@link PropertyAccessor} that knows how to access the beans of a + * Spring {@link BeanFactory}. * * @author Juergen Hoeller * @author Andy Clement diff --git a/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryResolver.java b/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryResolver.java index 4dfaa20c60..e70819575b 100644 --- a/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryResolver.java +++ b/spring-context/src/main/java/org/springframework/context/expression/BeanFactoryResolver.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. @@ -24,8 +24,7 @@ import org.springframework.expression.EvaluationContext; import org.springframework.util.Assert; /** - * EL bean resolver that operates against a Spring - * {@link org.springframework.beans.factory.BeanFactory}. + * SpEL {@link BeanResolver} that operates against a Spring {@link BeanFactory}. * * @author Juergen Hoeller * @since 3.0.4 @@ -36,8 +35,8 @@ public class BeanFactoryResolver implements BeanResolver { /** - * Create a new {@link BeanFactoryResolver} for the given factory. - * @param beanFactory the {@link BeanFactory} to resolve bean names against + * Create a new {@code BeanFactoryResolver} for the given factory. + * @param beanFactory the {@code BeanFactory} to resolve bean names against */ public BeanFactoryResolver(BeanFactory beanFactory) { Assert.notNull(beanFactory, "BeanFactory must not be null"); diff --git a/spring-context/src/main/java/org/springframework/context/expression/EnvironmentAccessor.java b/spring-context/src/main/java/org/springframework/context/expression/EnvironmentAccessor.java index 8e5dd92719..7b7fda7452 100644 --- a/spring-context/src/main/java/org/springframework/context/expression/EnvironmentAccessor.java +++ b/spring-context/src/main/java/org/springframework/context/expression/EnvironmentAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 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,7 +25,7 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * Read-only EL property accessor that knows how to retrieve keys + * Read-only SpEL {@link PropertyAccessor} that knows how to retrieve properties * of a Spring {@link Environment} instance. * * @author Chris Beams @@ -38,18 +38,14 @@ public class EnvironmentAccessor implements PropertyAccessor { return new Class[] {Environment.class}; } - /** - * Can read any {@link Environment}, thus always returns true. - * @return true - */ @Override public boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException { - return true; + return (target instanceof Environment); } /** - * Access the given target object by resolving the given property name against the given target - * environment. + * Access the given target object by resolving the given property name against + * the given target environment. */ @Override public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException { @@ -65,12 +61,11 @@ public class EnvironmentAccessor implements PropertyAccessor { return false; } - /** - * Read-only: no-op. - */ @Override public void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) throws AccessException { + + throw new AccessException("The Environment is read-only"); } } diff --git a/spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java b/spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java index e02ae8ac7e..932dce44e6 100644 --- a/spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java +++ b/spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java @@ -21,6 +21,7 @@ import java.util.Map; import org.springframework.asm.MethodVisitor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; +import org.springframework.expression.PropertyAccessor; import org.springframework.expression.TypedValue; import org.springframework.expression.spel.CodeFlow; import org.springframework.expression.spel.CompilablePropertyAccessor; @@ -28,8 +29,8 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** - * EL property accessor that knows how to traverse the keys - * of a standard {@link java.util.Map}. + * SpEL {@link PropertyAccessor} that knows how to access the keys of a standard + * {@link java.util.Map}. * * @author Juergen Hoeller * @author Andy Clement @@ -39,8 +40,9 @@ public class MapAccessor implements CompilablePropertyAccessor { private final boolean allowWrite; + /** - * Create a new map accessor for reading as well as writing. + * Create a new {@code MapAccessor} for reading as well as writing. * @since 6.2 * @see #MapAccessor(boolean) */ @@ -49,7 +51,7 @@ public class MapAccessor implements CompilablePropertyAccessor { } /** - * Create a new map accessor for reading and possibly also writing. + * Create a new {@code MapAccessor} for reading and possibly also writing. * @param allowWrite whether to allow write operations on a target instance * @since 6.2 * @see #canWrite @@ -58,6 +60,7 @@ public class MapAccessor implements CompilablePropertyAccessor { this.allowWrite = allowWrite; } + @Override public Class[] getSpecificTargetClasses() { return new Class[] {Map.class}; diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java b/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java index 59371b2671..3bbb9411f8 100644 --- a/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java +++ b/spring-expression/src/test/java/org/springframework/expression/spel/CompilableMapAccessor.java @@ -36,8 +36,9 @@ public class CompilableMapAccessor implements CompilablePropertyAccessor { private final boolean allowWrite; + /** - * Create a new map accessor for reading as well as writing. + * Create a new {@code CompilableMapAccessor} for reading as well as writing. * @since 6.2 * @see #CompilableMapAccessor(boolean) */ @@ -46,7 +47,7 @@ public class CompilableMapAccessor implements CompilablePropertyAccessor { } /** - * Create a new map accessor for reading and possibly also writing. + * Create a new {@code CompilableMapAccessor} for reading and possibly also writing. * @param allowWrite whether to allow write operations on a target instance * @since 6.2 * @see #canWrite @@ -55,6 +56,7 @@ public class CompilableMapAccessor implements CompilablePropertyAccessor { this.allowWrite = allowWrite; } + @Override public Class[] getSpecificTargetClasses() { return new Class[] {Map.class};