1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 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.
16
16
17
17
package org .springframework .core .env ;
18
18
19
- import static java .lang .String .format ;
20
-
21
19
import org .springframework .core .convert .ConversionException ;
22
20
import org .springframework .util .ClassUtils ;
23
21
@@ -35,6 +33,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
35
33
36
34
private final PropertySources propertySources ;
37
35
36
+
38
37
/**
39
38
* Create a new resolver against the given property sources.
40
39
* @param propertySources the set of {@link PropertySource} objects to use
@@ -43,57 +42,56 @@ public PropertySourcesPropertyResolver(PropertySources propertySources) {
43
42
this .propertySources = propertySources ;
44
43
}
45
44
45
+
46
46
@ Override
47
47
public boolean containsProperty (String key ) {
48
- for (PropertySource <?> propertySource : this .propertySources ) {
49
- if (propertySource .containsProperty (key )) {
50
- return true ;
48
+ if (this .propertySources != null ) {
49
+ for (PropertySource <?> propertySource : this .propertySources ) {
50
+ if (propertySource .containsProperty (key )) {
51
+ return true ;
52
+ }
51
53
}
52
54
}
53
55
return false ;
54
56
}
55
57
56
58
@ Override
57
59
public String getProperty (String key ) {
58
- if (logger .isTraceEnabled ()) {
59
- logger .trace (format ("getProperty(\" %s\" ) (implicit targetType [String])" , key ));
60
- }
61
- return this .getProperty (key , String .class );
60
+ return getProperty (key , String .class );
62
61
}
63
62
64
63
@ Override
65
64
public <T > T getProperty (String key , Class <T > targetValueType ) {
66
65
boolean debugEnabled = logger .isDebugEnabled ();
67
66
if (logger .isTraceEnabled ()) {
68
- logger .trace (format ("getProperty(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
67
+ logger .trace (String . format ("getProperty(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
69
68
}
70
-
71
- for (PropertySource <?> propertySource : this .propertySources ) {
72
- if (debugEnabled ) {
73
- logger .debug (format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
74
- }
75
- Object value ;
76
- if ((value = propertySource .getProperty (key )) != null ) {
77
- Class <?> valueType = value .getClass ();
78
- if (String .class .equals (valueType )) {
79
- value = this .resolveNestedPlaceholders ((String ) value );
80
- }
69
+ if (this .propertySources != null ) {
70
+ for (PropertySource <?> propertySource : this .propertySources ) {
81
71
if (debugEnabled ) {
82
- logger .debug (
83
- format ("Found key '%s' in [%s] with type [%s] and value '%s'" ,
84
- key , propertySource .getName (), valueType .getSimpleName (), value ));
72
+ logger .debug (String .format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
85
73
}
86
- if (!this .conversionService .canConvert (valueType , targetValueType )) {
87
- throw new IllegalArgumentException (
88
- format ("Cannot convert value [%s] from source type [%s] to target type [%s]" ,
89
- value , valueType .getSimpleName (), targetValueType .getSimpleName ()));
74
+ Object value ;
75
+ if ((value = propertySource .getProperty (key )) != null ) {
76
+ Class <?> valueType = value .getClass ();
77
+ if (String .class .equals (valueType )) {
78
+ value = this .resolveNestedPlaceholders ((String ) value );
79
+ }
80
+ if (debugEnabled ) {
81
+ logger .debug (String .format ("Found key '%s' in [%s] with type [%s] and value '%s'" ,
82
+ key , propertySource .getName (), valueType .getSimpleName (), value ));
83
+ }
84
+ if (!this .conversionService .canConvert (valueType , targetValueType )) {
85
+ throw new IllegalArgumentException (String .format (
86
+ "Cannot convert value [%s] from source type [%s] to target type [%s]" ,
87
+ value , valueType .getSimpleName (), targetValueType .getSimpleName ()));
88
+ }
89
+ return conversionService .convert (value , targetValueType );
90
90
}
91
- return conversionService .convert (value , targetValueType );
92
91
}
93
92
}
94
-
95
93
if (debugEnabled ) {
96
- logger .debug (format ("Could not find key '%s' in any property source. Returning [null]" , key ));
94
+ logger .debug (String . format ("Could not find key '%s' in any property source. Returning [null]" , key ));
97
95
}
98
96
return null ;
99
97
}
@@ -102,51 +100,52 @@ public <T> T getProperty(String key, Class<T> targetValueType) {
102
100
public <T > Class <T > getPropertyAsClass (String key , Class <T > targetValueType ) {
103
101
boolean debugEnabled = logger .isDebugEnabled ();
104
102
if (logger .isTraceEnabled ()) {
105
- logger .trace (format ("getPropertyAsClass(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
103
+ logger .trace (String . format ("getPropertyAsClass(\" %s\" , %s)" , key , targetValueType .getSimpleName ()));
106
104
}
107
-
108
- for (PropertySource <?> propertySource : this .propertySources ) {
109
- if (debugEnabled ) {
110
- logger .debug (format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
111
- }
112
- Object value ;
113
- if ((value = propertySource .getProperty (key )) != null ) {
105
+ if (this .propertySources != null ) {
106
+ for (PropertySource <?> propertySource : this .propertySources ) {
114
107
if (debugEnabled ) {
115
- logger .debug (
116
- format ("Found key '%s' in [%s] with value '%s'" , key , propertySource .getName (), value ));
108
+ logger .debug (String .format ("Searching for key '%s' in [%s]" , key , propertySource .getName ()));
117
109
}
118
-
119
- Class <?> clazz ;
120
- if (value instanceof String ) {
121
- try {
122
- clazz = ClassUtils .forName ((String )value , null );
123
- } catch (Exception ex ) {
124
- throw new ClassConversionException ((String )value , targetValueType , ex );
110
+ Object value = propertySource .getProperty (key );
111
+ if (value != null ) {
112
+ if (debugEnabled ) {
113
+ logger .debug (String .format ("Found key '%s' in [%s] with value '%s'" , key , propertySource .getName (), value ));
125
114
}
115
+ Class <?> clazz ;
116
+ if (value instanceof String ) {
117
+ try {
118
+ clazz = ClassUtils .forName ((String )value , null );
119
+ }
120
+ catch (Exception ex ) {
121
+ throw new ClassConversionException ((String )value , targetValueType , ex );
122
+ }
123
+ }
124
+ else if (value instanceof Class ) {
125
+ clazz = (Class <?>)value ;
126
+ }
127
+ else {
128
+ clazz = value .getClass ();
129
+ }
130
+ if (!targetValueType .isAssignableFrom (clazz )) {
131
+ throw new ClassConversionException (clazz , targetValueType );
132
+ }
133
+ @ SuppressWarnings ("unchecked" )
134
+ Class <T > targetClass = (Class <T >) clazz ;
135
+ return targetClass ;
126
136
}
127
- else if (value instanceof Class ) {
128
- clazz = (Class <?>)value ;
129
- } else {
130
- clazz = value .getClass ();
131
- }
132
-
133
- if (!targetValueType .isAssignableFrom (clazz )) {
134
- throw new ClassConversionException (clazz , targetValueType );
135
- }
136
- @ SuppressWarnings ("unchecked" )
137
- Class <T > targetClass = (Class <T >)clazz ;
138
- return targetClass ;
139
137
}
140
138
}
141
-
142
139
if (debugEnabled ) {
143
- logger .debug (format ("Could not find key '%s' in any property source. Returning [null]" , key ));
140
+ logger .debug (String . format ("Could not find key '%s' in any property source. Returning [null]" , key ));
144
141
}
145
142
return null ;
146
143
}
147
144
145
+
148
146
@ SuppressWarnings ("serial" )
149
- static class ClassConversionException extends ConversionException {
147
+ private static class ClassConversionException extends ConversionException {
148
+
150
149
public ClassConversionException (Class <?> actual , Class <?> expected ) {
151
150
super (String .format ("Actual type %s is not assignable to expected type %s" , actual .getName (), expected .getName ()));
152
151
}
@@ -155,4 +154,5 @@ public ClassConversionException(String actual, Class<?> expected, Exception ex)
155
154
super (String .format ("Could not find/load class %s during attempt to convert to %s" , actual , expected .getName ()), ex );
156
155
}
157
156
}
157
+
158
158
}
0 commit comments