|
|||||||||||||||||||
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% | 90% | 97.2% |
|
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 | 3797 |
public static void write(Object target, String propertyName, Object value) |
46 |
{ |
|
47 | 3797 |
ClassAdaptor a = getAdaptor(target); |
48 |
|
|
49 | 3797 |
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 | 1142 |
public static boolean isWritable(Object target, String propertyName) |
60 |
{ |
|
61 | 1142 |
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 | 3853 |
public static Class getPropertyType(Object target, String propertyName) |
90 |
{ |
|
91 | 3853 |
ClassAdaptor a = getAdaptor(target); |
92 |
|
|
93 | 3853 |
return a.getPropertyType(target, propertyName);
|
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* Returns an unordered List of the names of all readable properties
|
|
98 |
* of the target.
|
|
99 |
*/
|
|
100 | 1 |
public static List getReadableProperties(Object target) |
101 |
{ |
|
102 | 1 |
return getAdaptor(target).getReadableProperties();
|
103 |
} |
|
104 |
|
|
105 |
/**
|
|
106 |
* Returns an unordered List of the names of all writable properties
|
|
107 |
* of the target.
|
|
108 |
*/
|
|
109 | 236 |
public static List getWriteableProperties(Object target) |
110 |
{ |
|
111 | 236 |
return getAdaptor(target).getWriteableProperties();
|
112 |
} |
|
113 |
|
|
114 | 9044 |
private static synchronized ClassAdaptor getAdaptor(Object target) |
115 |
{ |
|
116 | 9044 |
if (target == null) |
117 | 1 |
throw new ApplicationRuntimeException(UtilMessages.nullObject()); |
118 |
|
|
119 | 9043 |
Class targetClass = target.getClass(); |
120 |
|
|
121 | 9043 |
ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass); |
122 |
|
|
123 | 9043 |
if (result == null) |
124 |
{ |
|
125 | 63 |
result = buildClassAdaptor(target, targetClass); |
126 | 62 |
_classAdaptors.put(targetClass, result); |
127 |
} |
|
128 |
|
|
129 | 9042 |
return result;
|
130 |
} |
|
131 |
|
|
132 | 63 |
private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass) |
133 |
{ |
|
134 | 63 |
try
|
135 |
{ |
|
136 | 63 |
BeanInfo info = Introspector.getBeanInfo(targetClass); |
137 |
|
|
138 | 62 |
return new ClassAdaptor(info.getPropertyDescriptors()); |
139 |
} |
|
140 |
catch (Exception ex)
|
|
141 |
{ |
|
142 | 1 |
throw new ApplicationRuntimeException( |
143 |
UtilMessages.unableToIntrospect(targetClass, ex), |
|
144 |
target, |
|
145 |
null,
|
|
146 |
ex); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
} |
|
151 |
|
|