|
17 | 17 | package org.springframework.expression.spel.ast; |
18 | 18 |
|
19 | 19 | import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Map; |
22 | 23 |
|
@@ -56,18 +57,50 @@ public PropertyOrFieldReference(boolean nullSafe, String propertyOrFieldName, in |
56 | 57 | @Override |
57 | 58 | public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { |
58 | 59 | TypedValue result = readProperty(state, this.name); |
59 | | - if (result.getValue()==null && state.configuredToCreateCollection() && result.getTypeDescriptor().getType().equals(List.class) && nextChildIs(Indexer.class)) { |
60 | | - // Create a new list ready for the indexer |
61 | | - try { |
62 | | - if (isWritable(state)) { |
63 | | - List newList = ArrayList.class.newInstance(); |
64 | | - writeProperty(state, name, newList); |
65 | | - result = readProperty(state, this.name); |
| 60 | + TypeDescriptor resultDescriptor = result.getTypeDescriptor(); |
| 61 | + // Dynamically create the objects if the user has requested that optional behaviour |
| 62 | + if (result.getValue()==null && state.configuredToCreateCollectionOrMap() && nextChildIs(Indexer.class,PropertyOrFieldReference.class)) { |
| 63 | + // Creating lists and maps |
| 64 | + if ((resultDescriptor.getType().equals(List.class) || resultDescriptor.getType().equals(Map.class))) { |
| 65 | + // Create a new collection or map ready for the indexer |
| 66 | + if (resultDescriptor.getType().equals(List.class)) { |
| 67 | + try { |
| 68 | + if (isWritable(state)) { |
| 69 | + List newList = ArrayList.class.newInstance(); |
| 70 | + writeProperty(state, name, newList); |
| 71 | + result = readProperty(state, this.name); |
| 72 | + } |
| 73 | + } catch (InstantiationException e) { |
| 74 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING); |
| 75 | + } catch (IllegalAccessException e) { |
| 76 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING); |
| 77 | + } |
| 78 | + } else { |
| 79 | + try { |
| 80 | + if (isWritable(state)) { |
| 81 | + Map newMap = HashMap.class.newInstance(); |
| 82 | + writeProperty(state, name, newMap); |
| 83 | + result = readProperty(state, this.name); |
| 84 | + } |
| 85 | + } catch (InstantiationException e) { |
| 86 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING); |
| 87 | + } catch (IllegalAccessException e) { |
| 88 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_MAP_FOR_INDEXING); |
| 89 | + } |
66 | 90 | } |
67 | | - } catch (InstantiationException e) { |
68 | | - throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING); |
69 | | - } catch (IllegalAccessException e) { |
70 | | - throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_CREATE_LIST_FOR_INDEXING); |
| 91 | + } else { |
| 92 | + // 'simple' object |
| 93 | + try { |
| 94 | + if (isWritable(state)) { |
| 95 | + Object newObject = result.getTypeDescriptor().getType().newInstance(); |
| 96 | + writeProperty(state, name, newObject); |
| 97 | + result = readProperty(state, this.name); |
| 98 | + } |
| 99 | + } catch (InstantiationException e) { |
| 100 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType()); |
| 101 | + } catch (IllegalAccessException e) { |
| 102 | + throw new SpelEvaluationException(getStartPosition(), e, SpelMessage.UNABLE_TO_DYNAMICALLY_CREATE_OBJECT,result.getTypeDescriptor().getType()); |
| 103 | + } |
71 | 104 | } |
72 | 105 | } |
73 | 106 | return result; |
|
0 commit comments