|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PropertyUtils.java | 100% | 100% | 92.9% | 98% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.util;
|
|
16 |
|
|
17 |
import java.beans.BeanInfo;
|
|
18 |
import java.beans.Introspector;
|
|
19 |
import java.util.HashMap;
|
|
20 |
import java.util.List;
|
|
21 |
import java.util.Map;
|
|
22 |
|
|
23 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
24 |
import org.apache.hivemind.HiveMind;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* A collection of static methods used to perform property-level access on arbitrary objects.
|
|
28 |
*
|
|
29 |
* @author Howard Lewis Ship
|
|
30 |
*/
|
|
31 |
public class PropertyUtils |
|
32 |
{ |
|
33 |
private static final Map _classAdaptors = new HashMap(); |
|
34 |
|
|
35 |
// Prevent instantiation
|
|
36 | 0 |
private PropertyUtils()
|
37 |
{ |
|
38 |
} |
|
39 |
|
|
40 |
/**
|
|
41 |
* Updates the property of the target object.
|
|
42 |
*
|
|
43 |
* @param target
|
|
44 |
* the object to update
|
|
45 |
* @param propertyName
|
|
46 |
* the name of the property to be updated
|
|
47 |
* @param value
|
|
48 |
* the value to be stored into the target object property
|
|
49 |
*/
|
|
50 | 6082 |
public static void write(Object target, String propertyName, Object value) |
51 |
{ |
|
52 | 6082 |
ClassAdaptor a = getAdaptor(target); |
53 |
|
|
54 | 6082 |
a.write(target, propertyName, value); |
55 |
} |
|
56 |
|
|
57 |
/**
|
|
58 |
* An improved version of {@link #write(Object, String, Object)}where the value starts as a
|
|
59 |
* string and is converted to the correct property type before being assigned.
|
|
60 |
*
|
|
61 |
* @since 1.1
|
|
62 |
*/
|
|
63 | 4 |
public static void smartWrite(Object target, String propertyName, String value) |
64 |
{ |
|
65 | 4 |
ClassAdaptor a = getAdaptor(target); |
66 |
|
|
67 | 4 |
a.smartWrite(target, propertyName, value); |
68 |
} |
|
69 |
|
|
70 |
/**
|
|
71 |
* Initializes the properties of an object from a string. The string is a comma-seperated
|
|
72 |
* sequence of property names and values. Property names are seperated from values be an equals
|
|
73 |
* sign. For boolean properties, the equals sign and value may be omitted (a value of true is
|
|
74 |
* assumed), or the property name may be prefixed with an exclamation point to indicated false
|
|
75 |
* value. Example: <code>validate,maxLength=10,displayName=User Id</code>.
|
|
76 |
*
|
|
77 |
* @param target
|
|
78 |
* the object to be configured
|
|
79 |
* @param initializer
|
|
80 |
* the string encoding the properties and values to be configured in the target
|
|
81 |
* object
|
|
82 |
* @since 1.1
|
|
83 |
*/
|
|
84 |
|
|
85 | 10 |
public static void configureProperties(Object target, String initializer) |
86 |
{ |
|
87 | 10 |
ClassAdaptor a = getAdaptor(target); |
88 |
|
|
89 | 10 |
a.configureProperties(target, initializer); |
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* Returns true of the instance contains a writable property of the given type.
|
|
94 |
*
|
|
95 |
* @param target
|
|
96 |
* the object to inspect
|
|
97 |
* @param propertyName
|
|
98 |
* the name of the property to check
|
|
99 |
*/
|
|
100 |
|
|
101 | 2731 |
public static boolean isWritable(Object target, String propertyName) |
102 |
{ |
|
103 | 2731 |
return getAdaptor(target).isWritable(propertyName);
|
104 |
} |
|
105 |
|
|
106 | 4 |
public static boolean isReadable(Object target, String propertyName) |
107 |
{ |
|
108 | 4 |
return getAdaptor(target).isReadable(propertyName);
|
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Updates the property of the target object.
|
|
113 |
*
|
|
114 |
* @param target
|
|
115 |
* the object to update
|
|
116 |
* @param propertyName
|
|
117 |
* the name of a property toread
|
|
118 |
*/
|
|
119 |
|
|
120 | 11 |
public static Object read(Object target, String propertyName) |
121 |
{ |
|
122 | 11 |
ClassAdaptor a = getAdaptor(target); |
123 |
|
|
124 | 9 |
return a.read(target, propertyName);
|
125 |
} |
|
126 |
|
|
127 |
/**
|
|
128 |
* Returns the type of the named property.
|
|
129 |
*
|
|
130 |
* @param target
|
|
131 |
* the object to examine
|
|
132 |
* @param propertyName
|
|
133 |
* the name of the property to check
|
|
134 |
*/
|
|
135 | 6213 |
public static Class getPropertyType(Object target, String propertyName) |
136 |
{ |
|
137 | 6213 |
ClassAdaptor a = getAdaptor(target); |
138 |
|
|
139 | 6213 |
return a.getPropertyType(target, propertyName);
|
140 |
} |
|
141 |
|
|
142 |
/**
|
|
143 |
* Returns the {@link PropertyAdaptor}for the given target object and property name.
|
|
144 |
*
|
|
145 |
* @throws ApplicationRuntimeException
|
|
146 |
* if the property does not exist.
|
|
147 |
*/
|
|
148 | 2 |
public static PropertyAdaptor getPropertyAdaptor(Object target, String propertyName) |
149 |
{ |
|
150 | 2 |
ClassAdaptor a = getAdaptor(target); |
151 |
|
|
152 | 2 |
return a.getPropertyAdaptor(target, propertyName);
|
153 |
} |
|
154 |
|
|
155 |
/**
|
|
156 |
* Returns an unordered List of the names of all readable properties of the target.
|
|
157 |
*/
|
|
158 | 1 |
public static List getReadableProperties(Object target) |
159 |
{ |
|
160 | 1 |
return getAdaptor(target).getReadableProperties();
|
161 |
} |
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns an unordered List of the names of all writable properties of the target.
|
|
165 |
*/
|
|
166 | 464 |
public static List getWriteableProperties(Object target) |
167 |
{ |
|
168 | 464 |
return getAdaptor(target).getWriteableProperties();
|
169 |
} |
|
170 |
|
|
171 | 15522 |
private static ClassAdaptor getAdaptor(Object target) |
172 |
{ |
|
173 | 15522 |
if (target == null) |
174 | 1 |
throw new ApplicationRuntimeException(UtilMessages.nullObject()); |
175 |
|
|
176 | 15521 |
Class targetClass = target.getClass(); |
177 |
|
|
178 | 15521 |
synchronized (HiveMind.INTROSPECTOR_MUTEX)
|
179 |
{ |
|
180 | 15521 |
ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass); |
181 |
|
|
182 | 15521 |
if (result == null) |
183 |
{ |
|
184 | 936 |
result = buildClassAdaptor(target, targetClass); |
185 | 935 |
_classAdaptors.put(targetClass, result); |
186 |
} |
|
187 |
|
|
188 | 15520 |
return result;
|
189 |
} |
|
190 |
} |
|
191 |
|
|
192 | 936 |
private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass) |
193 |
{ |
|
194 | 936 |
try
|
195 |
{ |
|
196 | 936 |
BeanInfo info = Introspector.getBeanInfo(targetClass); |
197 |
|
|
198 | 935 |
return new ClassAdaptor(info.getPropertyDescriptors()); |
199 |
} |
|
200 |
catch (Exception ex)
|
|
201 |
{ |
|
202 | 1 |
throw new ApplicationRuntimeException(UtilMessages.unableToIntrospect(targetClass, ex), |
203 |
target, null, ex);
|
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
/**
|
|
208 |
* Clears all cached information. Invokes {@link Introspector#flushCaches()}.
|
|
209 |
*/
|
|
210 | 613 |
public static void clearCache() |
211 |
{ |
|
212 | 613 |
synchronized (HiveMind.INTROSPECTOR_MUTEX)
|
213 |
{ |
|
214 | 613 |
_classAdaptors.clear(); |
215 | 613 |
Introspector.flushCaches(); |
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
} |
|