Skip to content

Commit ea2a1d3

Browse files
committed
Javadoc fixes plus additional polishing
1 parent ef781b6 commit ea2a1d3

File tree

3 files changed

+47
-65
lines changed

3 files changed

+47
-65
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,8 @@ public static void insertNewArrayCode(MethodVisitor mv, int size, String arrayty
945945
// is [[I then we want [I and not [I;
946946
if (CodeFlow.isReferenceTypeArray(arraytype)) {
947947
mv.visitTypeInsn(ANEWARRAY, arraytype+";");
948-
} else {
948+
}
949+
else {
949950
mv.visitTypeInsn(ANEWARRAY, arraytype);
950951
}
951952
}
@@ -956,19 +957,22 @@ public static void insertNewArrayCode(MethodVisitor mv, int size, String arrayty
956957
}
957958

958959
/**
959-
* For use in mathematical operators, handles converting from a (possibly boxed) number on the stack to a primitive numeric type.
960-
* For example, from a Integer to a double, just need to call 'Number.doubleValue()' but from an int to a double, need to use
961-
* the bytecode 'i2d'.
960+
* For use in mathematical operators, handles converting from a (possibly boxed)
961+
* number on the stack to a primitive numeric type.
962+
* <p>For example, from a Integer to a double, just need to call 'Number.doubleValue()'
963+
* but from an int to a double, need to use the bytecode 'i2d'.
962964
* @param mv the method visitor when instructions should be appended
963965
* @param stackDescriptor a descriptor of the operand on the stack
964966
* @param targetDescriptor a primitive type descriptor
965967
*/
966-
public static void insertNumericUnboxOrPrimitiveTypeCoercion(MethodVisitor mv,
967-
String stackDescriptor, char targetDecriptor) {
968+
public static void insertNumericUnboxOrPrimitiveTypeCoercion(
969+
MethodVisitor mv, String stackDescriptor, char targetDescriptor) {
970+
968971
if (!CodeFlow.isPrimitive(stackDescriptor)) {
969-
CodeFlow.insertUnboxNumberInsns(mv, targetDecriptor, stackDescriptor);
970-
} else {
971-
CodeFlow.insertAnyNecessaryTypeConversionBytecodes(mv, targetDecriptor, stackDescriptor);
972+
CodeFlow.insertUnboxNumberInsns(mv, targetDescriptor, stackDescriptor);
973+
}
974+
else {
975+
CodeFlow.insertAnyNecessaryTypeConversionBytecodes(mv, targetDescriptor, stackDescriptor);
972976
}
973977
}
974978

spring-test/src/main/java/org/springframework/test/util/ReflectionTestUtils.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ public class ReflectionTestUtils {
7272
/**
7373
* Set the {@linkplain Field field} with the given {@code name} on the
7474
* provided {@code targetObject} to the supplied {@code value}.
75-
*
7675
* <p>This method delegates to {@link #setField(Object, String, Object, Class)},
7776
* supplying {@code null} for the {@code type} argument.
78-
*
7977
* @param targetObject the target object on which to set the field; never {@code null}
8078
* @param name the name of the field to set; never {@code null}
8179
* @param value the value to set
@@ -87,10 +85,8 @@ public static void setField(Object targetObject, String name, Object value) {
8785
/**
8886
* Set the {@linkplain Field field} with the given {@code name}/{@code type}
8987
* on the provided {@code targetObject} to the supplied {@code value}.
90-
*
9188
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
9289
* supplying {@code null} for the {@code targetClass} argument.
93-
*
9490
* @param targetObject the target object on which to set the field; never {@code null}
9591
* @param name the name of the field to set; may be {@code null} if
9692
* {@code type} is specified
@@ -105,10 +101,8 @@ public static void setField(Object targetObject, String name, Object value, Clas
105101
/**
106102
* Set the static {@linkplain Field field} with the given {@code name} on
107103
* the provided {@code targetClass} to the supplied {@code value}.
108-
*
109104
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
110105
* supplying {@code null} for the {@code targetObject} and {@code type} arguments.
111-
*
112106
* @param targetClass the target class on which to set the static field;
113107
* never {@code null}
114108
* @param name the name of the field to set; never {@code null}
@@ -123,10 +117,8 @@ public static void setField(Class<?> targetClass, String name, Object value) {
123117
* Set the static {@linkplain Field field} with the given
124118
* {@code name}/{@code type} on the provided {@code targetClass} to
125119
* the supplied {@code value}.
126-
*
127120
* <p>This method delegates to {@link #setField(Object, Class, String, Object, Class)},
128121
* supplying {@code null} for the {@code targetObject} argument.
129-
*
130122
* @param targetClass the target class on which to set the static field;
131123
* never {@code null}
132124
* @param name the name of the field to set; may be {@code null} if
@@ -144,12 +136,10 @@ public static void setField(Class<?> targetClass, String name, Object value, Cla
144136
* Set the {@linkplain Field field} with the given {@code name}/{@code type}
145137
* on the provided {@code targetObject}/{@code targetClass} to the supplied
146138
* {@code value}.
147-
*
148139
* <p>This method traverses the class hierarchy in search of the desired
149140
* field. In addition, an attempt will be made to make non-{@code public}
150141
* fields <em>accessible</em>, thus allowing one to set {@code protected},
151142
* {@code private}, and <em>package-private</em> fields.
152-
*
153143
* @param targetObject the target object on which to set the field; may be
154144
* {@code null} if the field is static
155145
* @param targetClass the target class on which to set the field; may
@@ -171,19 +161,18 @@ public static void setField(Object targetObject, Class<?> targetClass, String na
171161
if (targetClass == null) {
172162
targetClass = targetObject.getClass();
173163
}
174-
Field field = ReflectionUtils.findField(targetClass, name, type);
175164

176-
// Inline Assert.notNull() to avoid invoking toString() on a non-null target.
165+
Field field = ReflectionUtils.findField(targetClass, name, type);
177166
if (field == null) {
178167
throw new IllegalArgumentException(String.format(
179-
"Could not find field [%s] of type [%s] on target object [%s] or target class [%s]", name, type,
180-
targetObject, targetClass));
168+
"Could not find field '%s' of type [%s] on target object [%s] or target class [%s]", name, type,
169+
targetObject, targetClass));
181170
}
182171

183172
if (logger.isDebugEnabled()) {
184173
logger.debug(String.format(
185-
"Setting field [%s] of type [%s] on target object [%s] or target class [%s] to value [%s]", name, type,
186-
targetObject, targetClass, value));
174+
"Setting field '%s' of type [%s] on target object [%s] or target class [%s] to value [%s]", name, type,
175+
targetObject, targetClass, value));
187176
}
188177
ReflectionUtils.makeAccessible(field);
189178
ReflectionUtils.setField(field, targetObject, value);
@@ -192,10 +181,8 @@ public static void setField(Object targetObject, Class<?> targetClass, String na
192181
/**
193182
* Get the value of the {@linkplain Field field} with the given {@code name}
194183
* from the provided {@code targetObject}.
195-
*
196184
* <p>This method delegates to {@link #getField(Object, Class, String)},
197185
* supplying {@code null} for the {@code targetClass} argument.
198-
*
199186
* @param targetObject the target object from which to get the field;
200187
* never {@code null}
201188
* @param name the name of the field to get; never {@code null}
@@ -209,16 +196,14 @@ public static Object getField(Object targetObject, String name) {
209196
/**
210197
* Get the value of the static {@linkplain Field field} with the given
211198
* {@code name} from the provided {@code targetClass}.
212-
*
213199
* <p>This method delegates to {@link #getField(Object, Class, String)},
214200
* supplying {@code null} for the {@code targetObject} argument.
215-
*
216201
* @param targetClass the target class from which to get the static field;
217202
* never {@code null}
218203
* @param name the name of the field to get; never {@code null}
219204
* @return the field's current value
220-
* @see #getField(Object, String)
221205
* @since 4.2
206+
* @see #getField(Object, String)
222207
*/
223208
public static Object getField(Class<?> targetClass, String name) {
224209
return getField(null, targetClass, name);
@@ -227,24 +212,22 @@ public static Object getField(Class<?> targetClass, String name) {
227212
/**
228213
* Get the value of the {@linkplain Field field} with the given {@code name}
229214
* from the provided {@code targetObject}/{@code targetClass}.
230-
*
231215
* <p>This method traverses the class hierarchy in search of the desired
232216
* field. In addition, an attempt will be made to make non-{@code public}
233217
* fields <em>accessible</em>, thus allowing one to get {@code protected},
234218
* {@code private}, and <em>package-private</em> fields.
235-
*
236219
* @param targetObject the target object from which to get the field; may be
237220
* {@code null} if the field is static
238221
* @param targetClass the target class from which to get the field; may
239222
* be {@code null} if the field is an instance field
240223
* @param name the name of the field to get; never {@code null}
241224
* @return the field's current value
225+
* @since 4.2
242226
* @see #getField(Object, String)
243227
* @see #getField(Class, String)
244228
* @see ReflectionUtils#findField(Class, String, Class)
245229
* @see ReflectionUtils#makeAccessible(Field)
246-
* @see ReflectionUtils#getField(Field, Object, Object)
247-
* @since 4.2
230+
* @see ReflectionUtils#getField(Field, Object)
248231
*/
249232
public static Object getField(Object targetObject, Class<?> targetClass, String name) {
250233
Assert.isTrue(targetObject != null || targetClass != null,
@@ -253,18 +236,17 @@ public static Object getField(Object targetObject, Class<?> targetClass, String
253236
if (targetClass == null) {
254237
targetClass = targetObject.getClass();
255238
}
256-
Field field = ReflectionUtils.findField(targetClass, name);
257239

258-
// Inline Assert.notNull() to avoid invoking toString() on a non-null target.
240+
Field field = ReflectionUtils.findField(targetClass, name);
259241
if (field == null) {
260242
throw new IllegalArgumentException(
261-
String.format("Could not find field [%s] on target object [%s] or target class [%s]", name,
262-
targetObject, targetClass));
243+
String.format("Could not find field '%s' on target object [%s] or target class [%s]", name,
244+
targetObject, targetClass));
263245
}
264246

265247
if (logger.isDebugEnabled()) {
266-
logger.debug(String.format("Getting field [%s] from target object [%s] or target class [%s]", name,
267-
targetObject, targetClass));
248+
logger.debug(String.format("Getting field '%s' from target object [%s] or target class [%s]", name,
249+
targetObject, targetClass));
268250
}
269251
ReflectionUtils.makeAccessible(field);
270252
return ReflectionUtils.getField(field, targetObject);
@@ -273,17 +255,14 @@ public static Object getField(Object targetObject, Class<?> targetClass, String
273255
/**
274256
* Invoke the setter method with the given {@code name} on the supplied
275257
* target object with the supplied {@code value}.
276-
*
277258
* <p>This method traverses the class hierarchy in search of the desired
278259
* method. In addition, an attempt will be made to make non-{@code public}
279260
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
280261
* {@code private}, and <em>package-private</em> setter methods.
281-
*
282262
* <p>In addition, this method supports JavaBean-style <em>property</em>
283263
* names. For example, if you wish to set the {@code name} property on the
284264
* target object, you may pass either &quot;name&quot; or
285265
* &quot;setName&quot; as the method name.
286-
*
287266
* @param target the target object on which to invoke the specified setter
288267
* method
289268
* @param name the name of the setter method to invoke or the corresponding
@@ -300,17 +279,14 @@ public static void invokeSetterMethod(Object target, String name, Object value)
300279
/**
301280
* Invoke the setter method with the given {@code name} on the supplied
302281
* target object with the supplied {@code value}.
303-
*
304282
* <p>This method traverses the class hierarchy in search of the desired
305283
* method. In addition, an attempt will be made to make non-{@code public}
306284
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
307285
* {@code private}, and <em>package-private</em> setter methods.
308-
*
309286
* <p>In addition, this method supports JavaBean-style <em>property</em>
310287
* names. For example, if you wish to set the {@code name} property on the
311288
* target object, you may pass either &quot;name&quot; or
312289
* &quot;setName&quot; as the method name.
313-
*
314290
* @param target the target object on which to invoke the specified setter
315291
* method
316292
* @param name the name of the setter method to invoke or the corresponding
@@ -324,41 +300,41 @@ public static void invokeSetterMethod(Object target, String name, Object value)
324300
public static void invokeSetterMethod(Object target, String name, Object value, Class<?> type) {
325301
Assert.notNull(target, "Target object must not be null");
326302
Assert.hasText(name, "Method name must not be empty");
327-
Class<?>[] paramTypes = (type != null ? new Class<?>[] { type } : null);
303+
Class<?>[] paramTypes = (type != null ? new Class<?>[] {type} : null);
328304

329305
String setterMethodName = name;
330306
if (!name.startsWith(SETTER_PREFIX)) {
331307
setterMethodName = SETTER_PREFIX + StringUtils.capitalize(name);
332308
}
309+
333310
Method method = ReflectionUtils.findMethod(target.getClass(), setterMethodName, paramTypes);
334311
if (method == null && !setterMethodName.equals(name)) {
335312
setterMethodName = name;
336313
method = ReflectionUtils.findMethod(target.getClass(), setterMethodName, paramTypes);
337314
}
338-
Assert.notNull(method, "Could not find setter method [" + setterMethodName + "] on target [" + target
339-
+ "] with parameter type [" + type + "]");
315+
if (method == null) {
316+
throw new IllegalArgumentException("Could not find setter method '" + setterMethodName +
317+
"' on target [" + target + "] with parameter type [" + type + "]");
318+
}
340319

341320
if (logger.isDebugEnabled()) {
342-
logger.debug("Invoking setter method [" + setterMethodName + "] on target [" + target + "]");
321+
logger.debug("Invoking setter method '" + setterMethodName + "' on target [" + target + "]");
343322
}
344323
ReflectionUtils.makeAccessible(method);
345-
ReflectionUtils.invokeMethod(method, target, new Object[] { value });
324+
ReflectionUtils.invokeMethod(method, target, value);
346325
}
347326

348327
/**
349328
* Invoke the getter method with the given {@code name} on the supplied
350329
* target object with the supplied {@code value}.
351-
*
352330
* <p>This method traverses the class hierarchy in search of the desired
353331
* method. In addition, an attempt will be made to make non-{@code public}
354332
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
355333
* {@code private}, and <em>package-private</em> getter methods.
356-
*
357334
* <p>In addition, this method supports JavaBean-style <em>property</em>
358335
* names. For example, if you wish to get the {@code name} property on the
359336
* target object, you may pass either &quot;name&quot; or
360337
* &quot;getName&quot; as the method name.
361-
*
362338
* @param target the target object on which to invoke the specified getter
363339
* method
364340
* @param name the name of the getter method to invoke or the corresponding
@@ -381,10 +357,13 @@ public static Object invokeGetterMethod(Object target, String name) {
381357
getterMethodName = name;
382358
method = ReflectionUtils.findMethod(target.getClass(), getterMethodName);
383359
}
384-
Assert.notNull(method, "Could not find getter method [" + getterMethodName + "] on target [" + target + "]");
360+
if (method == null) {
361+
throw new IllegalArgumentException("Could not find getter method '" + getterMethodName +
362+
"' on target [" + target + "]");
363+
}
385364

386365
if (logger.isDebugEnabled()) {
387-
logger.debug("Invoking getter method [" + getterMethodName + "] on target [" + target + "]");
366+
logger.debug("Invoking getter method '" + getterMethodName + "' on target [" + target + "]");
388367
}
389368
ReflectionUtils.makeAccessible(method);
390369
return ReflectionUtils.invokeMethod(method, target);
@@ -393,12 +372,10 @@ public static Object invokeGetterMethod(Object target, String name) {
393372
/**
394373
* Invoke the method with the given {@code name} on the supplied target
395374
* object with the supplied arguments.
396-
*
397375
* <p>This method traverses the class hierarchy in search of the desired
398376
* method. In addition, an attempt will be made to make non-{@code public}
399377
* methods <em>accessible</em>, thus allowing one to invoke {@code protected},
400378
* {@code private}, and <em>package-private</em> methods.
401-
*
402379
* @param target the target object on which to invoke the specified method
403380
* @param name the name of the method to invoke
404381
* @param args the arguments to provide to the method
@@ -421,17 +398,16 @@ public static <T> T invokeMethod(Object target, String name, Object... args) {
421398
methodInvoker.prepare();
422399

423400
if (logger.isDebugEnabled()) {
424-
logger.debug("Invoking method [" + name + "] on target [" + target + "] with arguments ["
425-
+ ObjectUtils.nullSafeToString(args) + "]");
401+
logger.debug("Invoking method '" + name + "' on target [" + target + "] with arguments [" +
402+
ObjectUtils.nullSafeToString(args) + "]");
426403
}
427404

428405
return (T) methodInvoker.invoke();
429406
}
430-
catch (Exception e) {
431-
ReflectionUtils.handleReflectionException(e);
407+
catch (Exception ex) {
408+
ReflectionUtils.handleReflectionException(ex);
409+
throw new IllegalStateException("Should never get here");
432410
}
433-
434-
throw new IllegalStateException("Should never get here");
435411
}
436412

437413
}

spring-test/src/test/java/org/springframework/test/util/ReflectionTestUtilsTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class ReflectionTestUtilsTests {
4242
private static final Float PI = new Float((float) 22 / 7);
4343

4444
private final Person person = new Person();
45+
4546
private final Component component = new Component();
4647

4748
@Rule
@@ -53,6 +54,7 @@ public void resetStaticFields() {
5354
StaticFields.reset();
5455
}
5556

57+
5658
@Test
5759
public void setFieldWithNullTargetObject() throws Exception {
5860
exception.expect(IllegalArgumentException.class);
@@ -91,7 +93,7 @@ public void setFieldWithNullNameAndNullType() throws Exception {
9193
@Test
9294
public void setFieldWithBogusName() throws Exception {
9395
exception.expect(IllegalArgumentException.class);
94-
exception.expectMessage(startsWith("Could not find field [bogus]"));
96+
exception.expectMessage(startsWith("Could not find field 'bogus'"));
9597
setField(person, "bogus", new Long(99), long.class);
9698
}
9799

0 commit comments

Comments
 (0)