Skip to content

Commit cc0bd73

Browse files
committed
polishing
1 parent bc06ffb commit cc0bd73

File tree

9 files changed

+129
-105
lines changed

9 files changed

+129
-105
lines changed

org.springframework.aop/src/main/java/org/springframework/aop/framework/ReflectiveMethodInvocation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Clonea
102102
* but would complicate the code. And it would work only for static pointcuts.
103103
*/
104104
protected ReflectiveMethodInvocation(
105-
Object proxy, Object target, Method method, Object[] arguments,
106-
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
105+
Object proxy, Object target, Method method, Object[] arguments,
106+
Class targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {
107107

108108
this.proxy = proxy;
109109
this.target = target;

org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* @author Keith Donald
2525
* @since 3.0
2626
*/
27-
@SuppressWarnings("serial")
2827
public abstract class ConversionException extends NestedRuntimeException {
2928

3029
/**

org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
* @author Keith Donald
2323
* @since 3.0
2424
*/
25-
@SuppressWarnings("serial")
2625
public final class ConversionFailedException extends ConversionException {
2726

2827
private final TypeDescriptor sourceType;
2928

3029
private final TypeDescriptor targetType;
3130

31+
3232
/**
3333
* Create a new conversion exception.
3434
* @param value the value we tried to convert
@@ -37,11 +37,13 @@ public final class ConversionFailedException extends ConversionException {
3737
* @param cause the cause of the conversion failure
3838
*/
3939
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
40-
super(buildDefaultMessage(value, sourceType, targetType, cause), cause);
40+
super("Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type [" +
41+
targetType.getName() + "]; reason = '" + cause.getMessage() + "'", cause);
4142
this.sourceType = sourceType;
4243
this.targetType = targetType;
4344
}
4445

46+
4547
/**
4648
* Return the source type we tried to convert the value from.
4749
*/
@@ -56,10 +58,4 @@ public TypeDescriptor getTargetType() {
5658
return this.targetType;
5759
}
5860

59-
private static String buildDefaultMessage(Object value, TypeDescriptor sourceType, TypeDescriptor targetType,
60-
Throwable cause) {
61-
return "Unable to convert value " + value + " from type [" + sourceType.getName() + "] to type ["
62-
+ targetType.getName() + "]; reason = '" + cause.getMessage() + "'";
63-
}
64-
65-
}
61+
}

org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.MethodParameter;
2727
import org.springframework.util.Assert;
2828
import org.springframework.util.ClassUtils;
29+
import org.springframework.util.ObjectUtils;
2930

3031
/**
3132
* Context about a type to convert to.
@@ -42,6 +43,7 @@ public class TypeDescriptor {
4243
*/
4344
public static final TypeDescriptor NULL = new TypeDescriptor();
4445

46+
4547
private Class<?> type;
4648

4749
private TypeDescriptor elementType;
@@ -52,6 +54,7 @@ public class TypeDescriptor {
5254

5355
private Annotation[] cachedFieldAnnotations;
5456

57+
5558
/**
5659
* Create a new descriptor for the given type.
5760
* <p>Use this constructor when a conversion point comes from a source such as a Map
@@ -99,6 +102,7 @@ private TypeDescriptor(Class<?> collectionType, TypeDescriptor elementType) {
99102
this.elementType = elementType;
100103
}
101104

105+
102106
/**
103107
* Return the wrapped MethodParameter, if any.
104108
* <p>Note: Either MethodParameter or Field is available.
@@ -124,11 +128,14 @@ public Field getField() {
124128
public Class<?> getType() {
125129
if (this.type != null) {
126130
return this.type;
127-
} else if (this.field != null) {
131+
}
132+
else if (this.field != null) {
128133
return this.field.getType();
129-
} else if (this.methodParameter != null) {
134+
}
135+
else if (this.methodParameter != null) {
130136
return this.methodParameter.getParameterType();
131-
} else {
137+
}
138+
else {
132139
return null;
133140
}
134141
}
@@ -139,16 +146,15 @@ public Class<?> getType() {
139146
*/
140147
public Class<?> getObjectType() {
141148
Class<?> type = getType();
142-
return type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type;
149+
return (type != null ? ClassUtils.resolvePrimitiveIfNecessary(type) : type);
143150
}
144151

145152
/**
146153
* Does the underyling declared type equal the type provided?
147154
* @param type the type to test against
148155
*/
149156
public boolean typeEquals(Class<?> type) {
150-
Class<?> thisType = getType();
151-
return thisType != null ? thisType.equals(type) : false;
157+
return ObjectUtils.nullSafeEquals(getType(), type);
152158
}
153159

154160
/**
@@ -158,7 +164,8 @@ public String getName() {
158164
Class<?> type = getType();
159165
if (type != null) {
160166
return getType().getName();
161-
} else {
167+
}
168+
else {
162169
return null;
163170
}
164171
}
@@ -198,14 +205,17 @@ public Class<?> getElementType() {
198205
* Return the element type as a type descriptor.
199206
*/
200207
public TypeDescriptor getElementTypeDescriptor() {
201-
if (elementType != null) {
202-
return elementType;
203-
} else {
208+
if (this.elementType != null) {
209+
return this.elementType;
210+
}
211+
else {
204212
if (isArray()) {
205213
return TypeDescriptor.valueOf(getArrayComponentType());
206-
} else if (isCollection()) {
214+
}
215+
else if (isCollection()) {
207216
return TypeDescriptor.valueOf(getCollectionElementType());
208-
} else {
217+
}
218+
else {
209219
return TypeDescriptor.NULL;
210220
}
211221
}
@@ -232,9 +242,11 @@ public boolean isMapEntryTypeKnown() {
232242
public Class<?> getMapKeyType() {
233243
if (this.field != null) {
234244
return GenericCollectionTypeResolver.getMapKeyFieldType(field);
235-
} else if (this.methodParameter != null) {
245+
}
246+
else if (this.methodParameter != null) {
236247
return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter);
237-
} else {
248+
}
249+
else {
238250
return null;
239251
}
240252
}
@@ -246,9 +258,11 @@ public Class<?> getMapKeyType() {
246258
public Class<?> getMapValueType() {
247259
if (this.field != null) {
248260
return GenericCollectionTypeResolver.getMapValueFieldType(this.field);
249-
} else if (this.methodParameter != null) {
261+
}
262+
else if (this.methodParameter != null) {
250263
return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter);
251-
} else {
264+
}
265+
else {
252266
return null;
253267
}
254268
}
@@ -276,9 +290,11 @@ public Annotation[] getAnnotations() {
276290
this.cachedFieldAnnotations = this.field.getAnnotations();
277291
}
278292
return this.cachedFieldAnnotations;
279-
} else if (this.methodParameter != null) {
293+
}
294+
else if (this.methodParameter != null) {
280295
return this.methodParameter.getParameterAnnotations();
281-
} else {
296+
}
297+
else {
282298
return new Annotation[0];
283299
}
284300
}
@@ -324,6 +340,28 @@ public boolean isAssignableTo(TypeDescriptor targetType) {
324340
return type != null && ClassUtils.isAssignable(targetType.getType(), type);
325341
}
326342

343+
private Class<?> getArrayComponentType() {
344+
return getType().getComponentType();
345+
}
346+
347+
@SuppressWarnings("unchecked")
348+
private Class<?> getCollectionElementType() {
349+
if (this.type != null) {
350+
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
351+
}
352+
else if (this.field != null) {
353+
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
354+
}
355+
else {
356+
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
357+
}
358+
}
359+
360+
private boolean isTypeAssignableTo(Class<?> clazz) {
361+
Class<?> type = getType();
362+
return (type != null && ClassUtils.isAssignable(clazz, type));
363+
}
364+
327365
/**
328366
* @return a textual representation of the type descriptor (eg. Map<String,Foo>) for use in messages
329367
*/
@@ -332,7 +370,8 @@ public String asString() {
332370
if (isArray()) {
333371
// TODO should properly handle multi dimensional arrays
334372
stringValue.append(getArrayComponentType().getName()).append("[]");
335-
} else {
373+
}
374+
else {
336375
Class<?> clazz = getType();
337376
if (clazz == null) {
338377
return "null";
@@ -355,27 +394,23 @@ public String asString() {
355394
return stringValue.toString();
356395
}
357396

358-
// internal helpers
359-
360-
private Class<?> getArrayComponentType() {
361-
return getType().getComponentType();
362-
}
363-
364-
@SuppressWarnings("unchecked")
365-
private Class<?> getCollectionElementType() {
366-
if (this.type != null) {
367-
return GenericCollectionTypeResolver.getCollectionType((Class<? extends Collection>) this.type);
368-
} else if (this.field != null) {
369-
return GenericCollectionTypeResolver.getCollectionFieldType(this.field);
370-
} else {
371-
return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter);
397+
public String toString() {
398+
if (this == TypeDescriptor.NULL) {
399+
return "[TypeDescriptor.NULL]";
400+
}
401+
else {
402+
StringBuilder builder = new StringBuilder();
403+
builder.append("[TypeDescriptor ");
404+
Annotation[] anns = getAnnotations();
405+
for (Annotation ann : anns) {
406+
builder.append("@").append(ann.annotationType().getName()).append(' ');
407+
}
408+
builder.append(getType().getName());
409+
builder.append("]");
410+
return builder.toString();
372411
}
373412
}
374413

375-
private boolean isTypeAssignableTo(Class<?> clazz) {
376-
Class<?> type = getType();
377-
return (type != null && ClassUtils.isAssignable(clazz, type));
378-
}
379414

380415
// static factory methods
381416

@@ -406,21 +441,5 @@ public static TypeDescriptor forObject(Object object) {
406441
public static TypeDescriptor collection(Class<?> type, TypeDescriptor elementType) {
407442
return new TypeDescriptor(type, elementType);
408443
}
409-
410-
public String toString() {
411-
if (this == TypeDescriptor.NULL) {
412-
return "[TypeDescriptor.NULL]";
413-
} else {
414-
StringBuilder builder = new StringBuilder();
415-
builder.append("[TypeDescriptor ");
416-
Annotation[] anns = getAnnotations();
417-
for (Annotation ann : anns) {
418-
builder.append("@" + ann.annotationType().getName()).append(' ');
419-
}
420-
builder.append(getType().getName());
421-
builder.append("]");
422-
return builder.toString();
423-
}
424-
}
425-
444+
426445
}

0 commit comments

Comments
 (0)