Skip to content

Commit

Permalink
Polish SpEL internals
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Nov 8, 2024
1 parent fb2afa6 commit ae62e33
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import org.springframework.util.ObjectUtils;

/**
* Utility methods for use in the AST classes.
* Utility methods for use with property and index accessors.
*
* @author Andy Clement
* @author Sam Brannen
* @since 3.0.2
*/
abstract class AstUtils {
abstract class AccessorUtils {

/**
* Determine the set of accessors that should be used to try to access an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private ValueRef getValueRef(ExpressionState state, AccessMode accessMode) throw
// Check for a custom IndexAccessor.
EvaluationContext evalContext = state.getEvaluationContext();
List<IndexAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(target, evalContext.getIndexAccessors());
AccessorUtils.getAccessorsToTry(target, evalContext.getIndexAccessors());
if (accessMode.supportsReads) {
try {
for (IndexAccessor indexAccessor : accessorsToTry) {
Expand Down Expand Up @@ -754,7 +754,7 @@ public TypedValue getValue() {
Indexer.this.cachedPropertyReadState = null;
}
List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
AccessorUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canRead(this.evaluationContext, this.targetObject, this.name)) {
if (accessor instanceof ReflectivePropertyAccessor reflectivePropertyAccessor) {
Expand Down Expand Up @@ -797,7 +797,7 @@ public void setValue(@Nullable Object newValue) {
Indexer.this.cachedPropertyWriteState = null;
}
List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
AccessorUtils.getAccessorsToTry(targetType, this.evaluationContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(this.evaluationContext, this.targetObject, this.name)) {
accessor.write(this.evaluationContext, this.targetObject, this.name, newValue);
Expand Down Expand Up @@ -1014,7 +1014,7 @@ public TypedValue getValue() {
Indexer.this.cachedIndexReadState = null;
}
List<IndexAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
AccessorUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
for (IndexAccessor indexAccessor : accessorsToTry) {
if (indexAccessor.canRead(this.evaluationContext, this.target, this.index)) {
TypedValue result = indexAccessor.read(this.evaluationContext, this.target, this.index);
Expand Down Expand Up @@ -1069,7 +1069,7 @@ public void setValue(@Nullable Object newValue) {
Indexer.this.cachedIndexWriteState = null;
}
List<IndexAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
AccessorUtils.getAccessorsToTry(this.target, this.evaluationContext.getIndexAccessors());
for (IndexAccessor indexAccessor : accessorsToTry) {
if (indexAccessor.canWrite(this.evaluationContext, this.target, this.index)) {
indexAccessor.write(this.evaluationContext, this.target, this.index, newValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private TypedValue readProperty(TypedValue contextObject, EvaluationContext eval
}

List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AccessorUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
// Go through the accessors that may be able to resolve it. If they are a cacheable accessor then
// get the accessor and use it. If they are not cacheable but report they can read the property
// then ask them to read it
Expand Down Expand Up @@ -259,7 +259,7 @@ private void writeProperty(
}

List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AccessorUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
try {
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(evalContext, targetObject, name)) {
Expand All @@ -284,7 +284,7 @@ public boolean isWritableProperty(String name, TypedValue contextObject, Evaluat
Object targetObject = contextObject.getValue();
if (targetObject != null) {
List<PropertyAccessor> accessorsToTry =
AstUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
AccessorUtils.getAccessorsToTry(targetObject, evalContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
try {
if (accessor.canWrite(evalContext, targetObject, name)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.expression.spel.ast;

import java.util.List;

import org.junit.jupiter.api.Test;

import org.springframework.expression.TargetedAccessor;
import org.springframework.lang.Nullable;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link AccessorUtils}.
*
* @author Sam Brannen
* @since 6.1.15
*/
class AccessorUtilsTests {

private final TargetedAccessor animal1Accessor = createAccessor("Animal1", Animal.class);

private final TargetedAccessor animal2Accessor = createAccessor("Animal2", Animal.class);

private final TargetedAccessor cat1Accessor = createAccessor("Cat1", Cat.class);

private final TargetedAccessor cat2Accessor = createAccessor("Cat2", Cat.class);

private final TargetedAccessor generic1Accessor = createAccessor("Generic1", null);

private final TargetedAccessor generic2Accessor = createAccessor("Generic2", null);

private final List<TargetedAccessor> accessors = List.of(
generic1Accessor,
cat1Accessor,
animal1Accessor,
animal2Accessor,
cat2Accessor,
generic2Accessor
);


@Test
void emptyAccessorsList() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry(new Cat(), List.of());
assertThat(accessorsToTry).isEmpty();
}

@Test
void noMatch() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry(new Dog(), List.of(cat1Accessor));
assertThat(accessorsToTry).isEmpty();
}

@Test
void singleExactTypeMatch() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry(new Cat(), List.of(cat1Accessor));
assertThat(accessorsToTry).containsExactly(cat1Accessor);
}

@Test
void exactTypeSupertypeAndGenericMatches() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry(new Cat(), accessors);
assertThat(accessorsToTry).containsExactly(
cat1Accessor, cat2Accessor, animal1Accessor, animal2Accessor, generic1Accessor, generic2Accessor);
}

@Test
void supertypeAndGenericMatches() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry(new Dog(), accessors);
assertThat(accessorsToTry).containsExactly(
animal1Accessor, animal2Accessor, generic1Accessor, generic2Accessor);
}

@Test
void genericMatches() {
List<TargetedAccessor> accessorsToTry = AccessorUtils.getAccessorsToTry("not an Animal", accessors);
assertThat(accessorsToTry).containsExactly(generic1Accessor, generic2Accessor);
}


private static TargetedAccessor createAccessor(String name, Class<?> type) {
return new DemoAccessor(name, (type != null ? new Class<?>[] { type } : null));
}


private record DemoAccessor(String name, Class<?>[] types) implements TargetedAccessor {

@Override
@Nullable
public Class<?>[] getSpecificTargetClasses() {
return this.types;
}

@Override
public final String toString() {
return this.name;
}
}

sealed interface Animal permits Cat, Dog {
}

static final class Cat implements Animal {
}

static final class Dog implements Animal {
}

}

This file was deleted.

0 comments on commit ae62e33

Please sign in to comment.