Skip to content

Commit 301e2ea

Browse files
committed
Nullability fine-tuning and related polishing
Issue: SPR-15720
1 parent f46520e commit 301e2ea

File tree

7 files changed

+203
-185
lines changed

7 files changed

+203
-185
lines changed

spring-expression/src/main/java/org/springframework/expression/Expression.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
*/
3131
public interface Expression {
3232

33+
/**
34+
* Return the original string used to create this expression, unmodified.
35+
* @return the original expression string
36+
*/
37+
String getExpressionString();
38+
3339
/**
3440
* Evaluate this expression in the default standard context.
3541
* @return the evaluation result
@@ -39,23 +45,23 @@ public interface Expression {
3945
Object getValue() throws EvaluationException;
4046

4147
/**
42-
* Evaluate this expression against the specified root object
43-
* @param rootObject the root object against which properties/etc will be resolved
48+
* Evaluate the expression in the default context. If the result of the evaluation does not match (and
49+
* cannot be converted to) the expected result type then an exception will be returned.
50+
* @param desiredResultType the class the caller would like the result to be
4451
* @return the evaluation result
4552
* @throws EvaluationException if there is a problem during evaluation
4653
*/
4754
@Nullable
48-
Object getValue(Object rootObject) throws EvaluationException;
55+
<T> T getValue(@Nullable Class<T> desiredResultType) throws EvaluationException;
4956

5057
/**
51-
* Evaluate the expression in the default context. If the result of the evaluation does not match (and
52-
* cannot be converted to) the expected result type then an exception will be returned.
53-
* @param desiredResultType the class the caller would like the result to be
58+
* Evaluate this expression against the specified root object
59+
* @param rootObject the root object against which properties/etc will be resolved
5460
* @return the evaluation result
5561
* @throws EvaluationException if there is a problem during evaluation
5662
*/
5763
@Nullable
58-
<T> T getValue(@Nullable Class<T> desiredResultType) throws EvaluationException;
64+
Object getValue(Object rootObject) throws EvaluationException;
5965

6066
/**
6167
* Evaluate the expression in the default context against the specified root object. If the
@@ -198,46 +204,45 @@ <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T>
198204

199205
/**
200206
* Determine if an expression can be written to, i.e. setValue() can be called.
201-
* @param context the context in which the expression should be checked
207+
* @param rootObject the root object against which to evaluate the expression
202208
* @return true if the expression is writable
203209
* @throws EvaluationException if there is a problem determining if it is writable
204210
*/
205-
boolean isWritable(EvaluationContext context) throws EvaluationException;
211+
boolean isWritable(Object rootObject) throws EvaluationException;
206212

207213
/**
208214
* Determine if an expression can be written to, i.e. setValue() can be called.
209-
* The supplied root object overrides any specified in the context.
210215
* @param context the context in which the expression should be checked
211-
* @param rootObject the root object against which to evaluate the expression
212216
* @return true if the expression is writable
213217
* @throws EvaluationException if there is a problem determining if it is writable
214218
*/
215-
boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException;
219+
boolean isWritable(EvaluationContext context) throws EvaluationException;
216220

217221
/**
218222
* Determine if an expression can be written to, i.e. setValue() can be called.
223+
* The supplied root object overrides any specified in the context.
224+
* @param context the context in which the expression should be checked
219225
* @param rootObject the root object against which to evaluate the expression
220226
* @return true if the expression is writable
221227
* @throws EvaluationException if there is a problem determining if it is writable
222228
*/
223-
boolean isWritable(Object rootObject) throws EvaluationException;
229+
boolean isWritable(EvaluationContext context, Object rootObject) throws EvaluationException;
224230

225231
/**
226232
* Set this expression in the provided context to the value provided.
227-
*
228-
* @param context the context in which to set the value of the expression
233+
* @param rootObject the root object against which to evaluate the expression
229234
* @param value the new value
230235
* @throws EvaluationException if there is a problem during evaluation
231236
*/
232-
void setValue(EvaluationContext context, Object value) throws EvaluationException;
237+
void setValue(Object rootObject, @Nullable Object value) throws EvaluationException;
233238

234239
/**
235240
* Set this expression in the provided context to the value provided.
236-
* @param rootObject the root object against which to evaluate the expression
241+
* @param context the context in which to set the value of the expression
237242
* @param value the new value
238243
* @throws EvaluationException if there is a problem during evaluation
239244
*/
240-
void setValue(Object rootObject, Object value) throws EvaluationException;
245+
void setValue(EvaluationContext context, @Nullable Object value) throws EvaluationException;
241246

242247
/**
243248
* Set this expression in the provided context to the value provided.
@@ -247,12 +252,6 @@ <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T>
247252
* @param value the new value
248253
* @throws EvaluationException if there is a problem during evaluation
249254
*/
250-
void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException;
251-
252-
/**
253-
* Returns the original string used to create this expression, unmodified.
254-
* @return the original expression string
255-
*/
256-
String getExpressionString();
255+
void setValue(EvaluationContext context, Object rootObject, @Nullable Object value) throws EvaluationException;
257256

258257
}

spring-expression/src/main/java/org/springframework/expression/common/CompositeStringExpression.java

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.expression.EvaluationException;
2222
import org.springframework.expression.Expression;
2323
import org.springframework.expression.TypedValue;
24+
import org.springframework.lang.Nullable;
2425

2526
/**
2627
* Represents a template expression broken into pieces. Each piece will be an Expression
@@ -58,6 +59,10 @@ public final String getExpressionString() {
5859
return this.expressionString;
5960
}
6061

62+
public final Expression[] getExpressions() {
63+
return this.expressions;
64+
}
65+
6166
@Override
6267
public String getValue() throws EvaluationException {
6368
StringBuilder sb = new StringBuilder();
@@ -70,6 +75,12 @@ public String getValue() throws EvaluationException {
7075
return sb.toString();
7176
}
7277

78+
@Override
79+
public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException {
80+
Object value = getValue();
81+
return ExpressionUtils.convertTypedValue(null, new TypedValue(value), expectedResultType);
82+
}
83+
7384
@Override
7485
public String getValue(Object rootObject) throws EvaluationException {
7586
StringBuilder sb = new StringBuilder();
@@ -82,6 +93,12 @@ public String getValue(Object rootObject) throws EvaluationException {
8293
return sb.toString();
8394
}
8495

96+
@Override
97+
public <T> T getValue(Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException {
98+
Object value = getValue(rootObject);
99+
return ExpressionUtils.convertTypedValue(null, new TypedValue(value), desiredResultType);
100+
}
101+
85102
@Override
86103
public String getValue(EvaluationContext context) throws EvaluationException {
87104
StringBuilder sb = new StringBuilder();
@@ -94,6 +111,14 @@ public String getValue(EvaluationContext context) throws EvaluationException {
94111
return sb.toString();
95112
}
96113

114+
@Override
115+
public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType)
116+
throws EvaluationException {
117+
118+
Object value = getValue(context);
119+
return ExpressionUtils.convertTypedValue(context, new TypedValue(value), expectedResultType);
120+
}
121+
97122
@Override
98123
public String getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
99124
StringBuilder sb = new StringBuilder();
@@ -107,8 +132,11 @@ public String getValue(EvaluationContext context, Object rootObject) throws Eval
107132
}
108133

109134
@Override
110-
public Class<?> getValueType(EvaluationContext context) {
111-
return String.class;
135+
public <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T> desiredResultType)
136+
throws EvaluationException {
137+
138+
Object value = getValue(context,rootObject);
139+
return ExpressionUtils.convertTypedValue(context, new TypedValue(value), desiredResultType);
112140
}
113141

114142
@Override
@@ -117,73 +145,50 @@ public Class<?> getValueType() {
117145
}
118146

119147
@Override
120-
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
121-
return TypeDescriptor.valueOf(String.class);
122-
}
123-
124-
@Override
125-
public TypeDescriptor getValueTypeDescriptor() {
126-
return TypeDescriptor.valueOf(String.class);
148+
public Class<?> getValueType(EvaluationContext context) {
149+
return String.class;
127150
}
128151

129152
@Override
130-
public void setValue(EvaluationContext context, Object value) throws EvaluationException {
131-
throw new EvaluationException(this.expressionString, "Cannot call setValue on a composite expression");
153+
public Class<?> getValueType(Object rootObject) throws EvaluationException {
154+
return String.class;
132155
}
133156

134157
@Override
135-
public <T> T getValue(EvaluationContext context, Class<T> expectedResultType) throws EvaluationException {
136-
Object value = getValue(context);
137-
return ExpressionUtils.convertTypedValue(context, new TypedValue(value), expectedResultType);
158+
public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException {
159+
return String.class;
138160
}
139161

140162
@Override
141-
public <T> T getValue(Class<T> expectedResultType) throws EvaluationException {
142-
Object value = getValue();
143-
return ExpressionUtils.convertTypedValue(null, new TypedValue(value), expectedResultType);
163+
public TypeDescriptor getValueTypeDescriptor() {
164+
return TypeDescriptor.valueOf(String.class);
144165
}
145166

146167
@Override
147-
public boolean isWritable(EvaluationContext context) {
148-
return false;
149-
}
150-
151-
public Expression[] getExpressions() {
152-
return this.expressions;
168+
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
169+
return TypeDescriptor.valueOf(String.class);
153170
}
154171

155-
156172
@Override
157-
public <T> T getValue(Object rootObject, Class<T> desiredResultType) throws EvaluationException {
158-
Object value = getValue(rootObject);
159-
return ExpressionUtils.convertTypedValue(null, new TypedValue(value), desiredResultType);
173+
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
174+
return TypeDescriptor.valueOf(String.class);
160175
}
161176

162177
@Override
163-
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> desiredResultType)
178+
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
164179
throws EvaluationException {
165-
Object value = getValue(context,rootObject);
166-
return ExpressionUtils.convertTypedValue(context, new TypedValue(value), desiredResultType);
167-
}
168-
169-
@Override
170-
public Class<?> getValueType(Object rootObject) throws EvaluationException {
171-
return String.class;
172-
}
173180

174-
@Override
175-
public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException {
176-
return String.class;
181+
return TypeDescriptor.valueOf(String.class);
177182
}
178183

179184
@Override
180-
public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException {
181-
return TypeDescriptor.valueOf(String.class);
185+
public boolean isWritable(Object rootObject) throws EvaluationException {
186+
return false;
182187
}
183188

184189
@Override
185-
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException {
186-
return TypeDescriptor.valueOf(String.class);
190+
public boolean isWritable(EvaluationContext context) {
191+
return false;
187192
}
188193

189194
@Override
@@ -192,17 +197,17 @@ public boolean isWritable(EvaluationContext context, Object rootObject) throws E
192197
}
193198

194199
@Override
195-
public void setValue(EvaluationContext context, Object rootObject, Object value) throws EvaluationException {
200+
public void setValue(Object rootObject, @Nullable Object value) throws EvaluationException {
196201
throw new EvaluationException(this.expressionString, "Cannot call setValue on a composite expression");
197202
}
198203

199204
@Override
200-
public boolean isWritable(Object rootObject) throws EvaluationException {
201-
return false;
205+
public void setValue(EvaluationContext context, @Nullable Object value) throws EvaluationException {
206+
throw new EvaluationException(this.expressionString, "Cannot call setValue on a composite expression");
202207
}
203208

204209
@Override
205-
public void setValue(Object rootObject, Object value) throws EvaluationException {
210+
public void setValue(EvaluationContext context, Object rootObject, @Nullable Object value) throws EvaluationException {
206211
throw new EvaluationException(this.expressionString, "Cannot call setValue on a composite expression");
207212
}
208213

0 commit comments

Comments
 (0)