|
1 | 1 | /* |
2 | | - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
@@ -225,6 +225,8 @@ public void generateCode(MethodVisitor mv, CodeFlow cf) { |
225 | 225 | cf.loadTarget(mv); |
226 | 226 | } |
227 | 227 |
|
| 228 | + SpelNodeImpl index = this.children[0]; |
| 229 | + |
228 | 230 | if (this.indexedType == IndexedType.ARRAY) { |
229 | 231 | int insn; |
230 | 232 | if ("D".equals(this.exitTypeDescriptor)) { |
@@ -261,32 +263,28 @@ else if ("C".equals(this.exitTypeDescriptor)) { |
261 | 263 | //depthPlusOne(exitTypeDescriptor)+"Ljava/lang/Object;"); |
262 | 264 | insn = AALOAD; |
263 | 265 | } |
264 | | - SpelNodeImpl index = this.children[0]; |
265 | | - cf.enterCompilationScope(); |
266 | | - index.generateCode(mv, cf); |
267 | | - cf.exitCompilationScope(); |
| 266 | + |
| 267 | + generateIndexCode(mv, cf, index, int.class); |
268 | 268 | mv.visitInsn(insn); |
269 | 269 | } |
270 | 270 |
|
271 | 271 | else if (this.indexedType == IndexedType.LIST) { |
272 | 272 | mv.visitTypeInsn(CHECKCAST, "java/util/List"); |
273 | | - cf.enterCompilationScope(); |
274 | | - this.children[0].generateCode(mv, cf); |
275 | | - cf.exitCompilationScope(); |
| 273 | + generateIndexCode(mv, cf, index, int.class); |
276 | 274 | mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true); |
277 | 275 | } |
278 | 276 |
|
279 | 277 | else if (this.indexedType == IndexedType.MAP) { |
280 | 278 | mv.visitTypeInsn(CHECKCAST, "java/util/Map"); |
281 | 279 | // Special case when the key is an unquoted string literal that will be parsed as |
282 | 280 | // a property/field reference |
283 | | - if ((this.children[0] instanceof PropertyOrFieldReference reference)) { |
| 281 | + if (index instanceof PropertyOrFieldReference reference) { |
284 | 282 | String mapKeyName = reference.getName(); |
285 | 283 | mv.visitLdcInsn(mapKeyName); |
286 | 284 | } |
287 | 285 | else { |
288 | 286 | cf.enterCompilationScope(); |
289 | | - this.children[0].generateCode(mv, cf); |
| 287 | + index.generateCode(mv, cf); |
290 | 288 | cf.exitCompilationScope(); |
291 | 289 | } |
292 | 290 | mv.visitMethodInsn( |
@@ -323,6 +321,11 @@ else if (this.indexedType == IndexedType.OBJECT) { |
323 | 321 | cf.pushDescriptor(this.exitTypeDescriptor); |
324 | 322 | } |
325 | 323 |
|
| 324 | + private void generateIndexCode(MethodVisitor mv, CodeFlow cf, SpelNodeImpl indexNode, Class<?> indexType) { |
| 325 | + String indexDesc = CodeFlow.toDescriptor(indexType); |
| 326 | + generateCodeForArgument(mv, cf, indexNode, indexDesc); |
| 327 | + } |
| 328 | + |
326 | 329 | @Override |
327 | 330 | public String toStringAST() { |
328 | 331 | StringJoiner sj = new StringJoiner(",", "[", "]"); |
|
0 commit comments