|
|||||||||||||||||||
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% | 91.7% | 97.6% |
|
1 |
// Copyright 2004 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 |
import java.beans.BeanInfo;
|
|
17 |
import java.beans.Introspector;
|
|
18 |
import java.util.HashMap;
|
|
19 |
import java.util.List;
|
|
20 |
import java.util.Map;
|
|
21 |
|
|
22 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* A collection of static methods used to perform property-level access on arbitrary objects.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public class PropertyUtils |
|
30 |
{ |
|
31 |
private static final Map _classAdaptors = new HashMap(); |
|
32 |
|
|
33 |
// Prevent instantiation
|
|
34 | 0 |
private PropertyUtils()
|
35 |
{ |
|
36 |
} |
|
37 |
|
|
38 |
/**
|
|
39 |
* Updates the property of the target object.
|
|
40 |
*
|
|
41 |
* @param target the object to update
|
|
42 |
* @param propertyName the name of the property to be updated
|
|
43 |
* @param value the value to be stored into the target object property
|
|
44 |
*/
|
|
45 | 6103 |
public static void write(Object target, String propertyName, Object value) |
46 |
{ |
|
47 | 6103 |
ClassAdaptor a = getAdaptor(target); |
48 |
|
|
49 | 6103 |
a.write(target, propertyName, value); |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Returns true of the instance contains a writable property of the given type.
|
|
54 |
*
|
|
55 |
* @param target the object to inspect
|
|
56 |
* @param propertyName the name of the property to check
|
|
57 |
*/
|
|
58 |
|
|
59 | 2013 |
public static boolean isWritable(Object target, String propertyName) |
60 |
{ |
|
61 | 2013 |
return getAdaptor(target).isWritable(propertyName);
|
62 |
} |
|
63 |
|
|
64 | 4 |
public static boolean isReadable(Object target, String propertyName) |
65 |
{ |
|
66 | 4 |
return getAdaptor(target).isReadable(propertyName);
|
67 |
} |
|
68 |
|
|
69 |
/**
|
|
70 |
* Updates the property of the target object.
|
|
71 |
*
|
|
72 |
* @param target the object to update
|
|
73 |
* @param propertyName the name of a property toread
|
|
74 |
*/
|
|
75 |
|
|
76 | 11 |
public static Object read(Object target, String propertyName) |
77 |
{ |
|
78 | 11 |
ClassAdaptor a = getAdaptor(target); |
79 |
|
|
80 | 9 |
return a.read(target, propertyName);
|
81 |
} |
|
82 |
|
|
83 |
/**
|
|
84 |
* Returns the type of the named property.
|
|
85 |
*
|
|
86 |
* @param target the object to examine
|
|
87 |
* @param propertyName the name of the property to check
|
|
88 |
*/
|
|
89 | 6166 |
public static Class getPropertyType(Object target, String propertyName) |
90 |
{ |
|
91 | 6166 |
ClassAdaptor a = getAdaptor(target); |
92 |
|
|
93 | 6166 |
return a.getPropertyType(target, propertyName);
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns the {@link PropertyAdaptor} for the given target object and property name.
|
|
98 |
*
|
|
99 |
* @throws ApplicationRuntimeException if the property does not exist.
|
|
100 |
*/
|
|
101 | 2 |
public static PropertyAdaptor getPropertyAdaptor(Object target, String propertyName) |
102 |
{ |
|
103 | 2 |
ClassAdaptor a = getAdaptor(target); |
104 |
|
|
105 | 2 |
return a.getPropertyAdaptor(target, propertyName);
|
106 |
} |
|
107 |
|
|
108 |
/**
|
|
109 |
* Returns an unordered List of the names of all readable properties
|
|
110 |
* of the target.
|
|
111 |
*/
|
|
112 | 1 |
public static List getReadableProperties(Object target) |
113 |
{ |
|
114 | 1 |
return getAdaptor(target).getReadableProperties();
|
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Returns an unordered List of the names of all writable properties
|
|
119 |
* of the target.
|
|
120 |
*/
|
|
121 | 410 |
public static List getWriteableProperties(Object target) |
122 |
{ |
|
123 | 410 |
return getAdaptor(target).getWriteableProperties();
|
124 |
} |
|
125 |
|
|
126 | 14710 |
private static synchronized ClassAdaptor getAdaptor(Object target) |
127 |
{ |
|
128 | 14710 |
if (target == null) |
129 | 1 |
throw new ApplicationRuntimeException(UtilMessages.nullObject()); |
130 |
|
|
131 | 14709 |
Class targetClass = target.getClass(); |
132 |
|
|
133 | 14709 |
ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass); |
134 |
|
|
135 | 14709 |
if (result == null) |
136 |
{ |
|
137 | 974 |
result = buildClassAdaptor(target, targetClass); |
138 | 973 |
_classAdaptors.put(targetClass, result); |
139 |
} |
|
140 |
|
|
141 | 14708 |
return result;
|
142 |
} |
|
143 |
|
|
144 | 974 |
private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass) |
145 |
{ |
|
146 | 974 |
try
|
147 |
{ |
|
148 | 974 |
BeanInfo info = Introspector.getBeanInfo(targetClass); |
149 |
|
|
150 | 973 |
return new ClassAdaptor(info.getPropertyDescriptors()); |
151 |
} |
|
152 |
catch (Exception ex)
|
|
153 |
{ |
|
154 | 1 |
throw new ApplicationRuntimeException( |
155 |
UtilMessages.unableToIntrospect(targetClass, ex), |
|
156 |
target, |
|
157 |
null,
|
|
158 |
ex); |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/**
|
|
163 |
* Clears all cached information.
|
|
164 |
*/
|
|
165 | 438 |
public static synchronized void clearCache() |
166 |
{ |
|
167 | 438 |
_classAdaptors.clear(); |
168 |
} |
|
169 |
|
|
170 |
} |
|
171 |
|
|