@@ -212,6 +212,22 @@ private ValueRef getValueRef(ExpressionState state, AccessMode accessMode) throw
212
212
// At this point, we need a TypeDescriptor for a non-null target object
213
213
Assert .state (targetDescriptor != null , "No type descriptor" );
214
214
215
+ // Indexing into an array
216
+ if (target .getClass ().isArray ()) {
217
+ int intIndex = convertIndexToInt (state , index );
218
+ this .indexedType = IndexedType .ARRAY ;
219
+ return new ArrayIndexingValueRef (state .getTypeConverter (), target , intIndex , targetDescriptor );
220
+ }
221
+
222
+ // Indexing into a List
223
+ if (target instanceof List <?> list ) {
224
+ int intIndex = convertIndexToInt (state , index );
225
+ this .indexedType = IndexedType .LIST ;
226
+ return new CollectionIndexingValueRef (list , intIndex , targetDescriptor ,
227
+ state .getTypeConverter (), state .getConfiguration ().isAutoGrowCollections (),
228
+ state .getConfiguration ().getMaximumAutoGrowSize ());
229
+ }
230
+
215
231
// Indexing into a Map
216
232
if (target instanceof Map <?, ?> map ) {
217
233
Object key = index ;
@@ -223,26 +239,11 @@ private ValueRef getValueRef(ExpressionState state, AccessMode accessMode) throw
223
239
return new MapIndexingValueRef (state .getTypeConverter (), map , key , targetDescriptor );
224
240
}
225
241
226
- // If the object is something that looks indexable by an integer,
227
- // attempt to treat the index value as a number
228
- if (target .getClass ().isArray () || target instanceof Collection || target instanceof String ) {
229
- int idx = (Integer ) state .convertValue (index , TypeDescriptor .valueOf (Integer .class ));
230
- if (target .getClass ().isArray ()) {
231
- this .indexedType = IndexedType .ARRAY ;
232
- return new ArrayIndexingValueRef (state .getTypeConverter (), target , idx , targetDescriptor );
233
- }
234
- else if (target instanceof Collection <?> collection ) {
235
- if (target instanceof List ) {
236
- this .indexedType = IndexedType .LIST ;
237
- }
238
- return new CollectionIndexingValueRef (collection , idx , targetDescriptor ,
239
- state .getTypeConverter (), state .getConfiguration ().isAutoGrowCollections (),
240
- state .getConfiguration ().getMaximumAutoGrowSize ());
241
- }
242
- else {
243
- this .indexedType = IndexedType .STRING ;
244
- return new StringIndexingValueRef ((String ) target , idx , targetDescriptor );
245
- }
242
+ // Indexing into a String
243
+ if (target instanceof String string ) {
244
+ int intIndex = convertIndexToInt (state , index );
245
+ this .indexedType = IndexedType .STRING ;
246
+ return new StringIndexingValueRef (string , intIndex , targetDescriptor );
246
247
}
247
248
248
249
// Check for a custom IndexAccessor.
@@ -280,6 +281,14 @@ else if (target instanceof Collection<?> collection) {
280
281
}
281
282
}
282
283
284
+ // Fallback indexing support for collections
285
+ if (target instanceof Collection <?> collection ) {
286
+ int intIndex = convertIndexToInt (state , index );
287
+ return new CollectionIndexingValueRef (collection , intIndex , targetDescriptor ,
288
+ state .getTypeConverter (), state .getConfiguration ().isAutoGrowCollections (),
289
+ state .getConfiguration ().getMaximumAutoGrowSize ());
290
+ }
291
+
283
292
// As a last resort, try to treat the index value as a property of the context object.
284
293
TypeDescriptor valueType = indexValue .getTypeDescriptor ();
285
294
if (valueType != null && String .class == valueType .getType ()) {
@@ -458,6 +467,10 @@ private void setExitTypeDescriptor(String descriptor) {
458
467
}
459
468
}
460
469
470
+ private static int convertIndexToInt (ExpressionState state , Object index ) {
471
+ return (Integer ) state .convertValue (index , TypeDescriptor .valueOf (Integer .class ));
472
+ }
473
+
461
474
private static Class <?> getObjectType (Object obj ) {
462
475
return (obj instanceof Class <?> clazz ? clazz : obj .getClass ());
463
476
}
0 commit comments