Polish and harmonize implementations of SpEL components in spring-context

This commit is contained in:
Sam Brannen
2024-09-03 17:16:20 +02:00
parent ea8f8f77c5
commit 529f311bd4
6 changed files with 33 additions and 34 deletions

View File

@@ -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};
}
}

View File

@@ -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

View File

@@ -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");

View File

@@ -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");
}
}

View File

@@ -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};