Skip to content

Commit f4c1ad7

Browse files
committed
Polishing
See gh-29857
1 parent 20be9e1 commit f4c1ad7

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/CompilablePropertyAccessor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.asm.MethodVisitor;
2020
import org.springframework.asm.Opcodes;
2121
import org.springframework.expression.PropertyAccessor;
22-
import org.springframework.lang.Nullable;
2322

2423
/**
2524
* A compilable {@link PropertyAccessor} is able to generate bytecode that represents
@@ -45,14 +44,10 @@ public interface CompilablePropertyAccessor extends PropertyAccessor, Opcodes {
4544
* Generate the bytecode that performs the access operation into the specified
4645
* {@link MethodVisitor} using context information from the {@link CodeFlow}
4746
* where necessary.
48-
* <p>Concrete implementations of {@code CompilablePropertyAccessor} typically
49-
* have access to the property name via other means (for example, supplied as
50-
* an argument when they were instantiated). Thus, the {@code propertyName}
51-
* supplied to this method may be {@code null}.
52-
* @param propertyName the name of the property, or {@code null} if not available
47+
* @param propertyName the name of the property
5348
* @param methodVisitor the ASM method visitor into which code should be generated
5449
* @param codeFlow the current state of the expression compiler
5550
*/
56-
void generateCode(@Nullable String propertyName, MethodVisitor methodVisitor, CodeFlow codeFlow);
51+
void generateCode(String propertyName, MethodVisitor methodVisitor, CodeFlow codeFlow);
5752

5853
}

spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,17 @@ public boolean isCompilable() {
213213
if (this.indexedType == IndexedType.ARRAY) {
214214
return (this.exitTypeDescriptor != null);
215215
}
216-
else if (this.indexedType == IndexedType.LIST) {
217-
return this.children[0].isCompilable();
216+
SpelNodeImpl index = this.children[0];
217+
if (this.indexedType == IndexedType.LIST) {
218+
return index.isCompilable();
218219
}
219220
else if (this.indexedType == IndexedType.MAP) {
220-
return (this.children[0] instanceof PropertyOrFieldReference || this.children[0].isCompilable());
221+
return (index instanceof PropertyOrFieldReference || index.isCompilable());
221222
}
222223
else if (this.indexedType == IndexedType.OBJECT) {
223224
// If the string name is changing, the accessor is clearly going to change.
224225
// So compilation is only possible if the index expression is a StringLiteral.
225-
return (getChild(0) instanceof StringLiteral &&
226+
return (index instanceof StringLiteral &&
226227
this.cachedReadAccessor instanceof CompilablePropertyAccessor compilablePropertyAccessor &&
227228
compilablePropertyAccessor.isCompilable());
228229
}
@@ -238,6 +239,7 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) {
238239
}
239240

240241
SpelNodeImpl index = this.children[0];
242+
241243
if (this.indexedType == IndexedType.ARRAY) {
242244
int insn = switch (this.exitTypeDescriptor) {
243245
case "D" -> {
@@ -313,9 +315,14 @@ else if (this.indexedType == IndexedType.MAP) {
313315
}
314316

315317
else if (this.indexedType == IndexedType.OBJECT) {
318+
if (!(index instanceof StringLiteral stringLiteral)) {
319+
throw new IllegalStateException(
320+
"Index expression must be a StringLiteral, but was: " + index.getClass().getName());
321+
}
316322
CompilablePropertyAccessor compilablePropertyAccessor = (CompilablePropertyAccessor) this.cachedReadAccessor;
317323
Assert.state(compilablePropertyAccessor != null, "No cached read accessor");
318-
compilablePropertyAccessor.generateCode(null, mv, cf);
324+
String propertyName = (String) stringLiteral.getLiteralValue().getValue();
325+
compilablePropertyAccessor.generateCode(propertyName, mv, cf);
319326
}
320327

321328
cf.pushDescriptor(this.exitTypeDescriptor);

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectivePropertyAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ public Class<?> getPropertyType() {
712712
}
713713

714714
@Override
715-
public void generateCode(@Nullable String propertyName, MethodVisitor mv, CodeFlow cf) {
715+
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
716716
Class<?> publicDeclaringClass = this.member.getDeclaringClass();
717717
if (!Modifier.isPublic(publicDeclaringClass.getModifiers()) && this.originalMethod != null) {
718718
publicDeclaringClass = ReflectionHelper.findPublicDeclaringClass(this.originalMethod);

0 commit comments

Comments
 (0)