|
|||||||||||||||||||
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% | 87.5% | 96.9% |
|
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.Map;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* A collection of static methods used to perform property-level access on arbitrary objects.
|
|
25 |
*
|
|
26 |
* @author Howard Lewis Ship
|
|
27 |
*/
|
|
28 |
public class PropertyUtils |
|
29 |
{ |
|
30 |
private static final Map _classAdaptors = new HashMap(); |
|
31 |
|
|
32 |
// Prevent instantiation
|
|
33 | 0 |
private PropertyUtils()
|
34 |
{ |
|
35 |
} |
|
36 |
|
|
37 |
/**
|
|
38 |
* Updates the property of the target object.
|
|
39 |
*
|
|
40 |
* @param target the object to update
|
|
41 |
* @param propertyName the name of the property to be updated
|
|
42 |
* @param value the value to be stored into the target object property
|
|
43 |
*/
|
|
44 | 3509 |
public static void write(Object target, String propertyName, Object value) |
45 |
{ |
|
46 | 3509 |
ClassAdaptor a = getAdaptor(target); |
47 |
|
|
48 | 3509 |
a.write(target, propertyName, value); |
49 |
} |
|
50 |
|
|
51 |
/**
|
|
52 |
* Returns true of the instance contains a writable property of the given type.
|
|
53 |
*
|
|
54 |
* @param target the object to inspect
|
|
55 |
* @param propertyName the name of the property to check
|
|
56 |
*/
|
|
57 |
|
|
58 | 1060 |
public static boolean isWritable(Object target, String propertyName) |
59 |
{ |
|
60 | 1060 |
return getAdaptor(target).isWritable(propertyName);
|
61 |
} |
|
62 |
|
|
63 | 4 |
public static boolean isReadable(Object target, String propertyName) |
64 |
{ |
|
65 | 4 |
return getAdaptor(target).isReadable(propertyName);
|
66 |
} |
|
67 |
|
|
68 |
/**
|
|
69 |
* Updates the property of the target object.
|
|
70 |
*
|
|
71 |
* @param target the object to update
|
|
72 |
* @param propertyName the name of a property toread
|
|
73 |
*/
|
|
74 |
|
|
75 | 10 |
public static Object read(Object target, String propertyName) |
76 |
{ |
|
77 | 10 |
ClassAdaptor a = getAdaptor(target); |
78 |
|
|
79 | 8 |
return a.read(target, propertyName);
|
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* Returns the type of the named property.
|
|
84 |
*
|
|
85 |
* @param target the object to examine
|
|
86 |
* @param propertyName the name of the property to check
|
|
87 |
*/
|
|
88 | 3459 |
public static Class getPropertyType(Object target, String propertyName) |
89 |
{ |
|
90 | 3459 |
ClassAdaptor a = getAdaptor(target); |
91 |
|
|
92 | 3459 |
return a.getPropertyType(target, propertyName);
|
93 |
} |
|
94 |
|
|
95 | 8042 |
private static synchronized ClassAdaptor getAdaptor(Object target) |
96 |
{ |
|
97 | 8042 |
if (target == null) |
98 | 1 |
throw new ApplicationRuntimeException(UtilMessages.nullObject()); |
99 |
|
|
100 | 8041 |
Class targetClass = target.getClass(); |
101 |
|
|
102 | 8041 |
ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass); |
103 |
|
|
104 | 8041 |
if (result == null) |
105 |
{ |
|
106 | 185 |
result = buildClassAdaptor(target, targetClass); |
107 | 184 |
_classAdaptors.put(targetClass, result); |
108 |
} |
|
109 |
|
|
110 | 8040 |
return result;
|
111 |
} |
|
112 |
|
|
113 | 185 |
private static ClassAdaptor buildClassAdaptor(Object target, Class targetClass) |
114 |
{ |
|
115 | 185 |
try
|
116 |
{ |
|
117 | 185 |
BeanInfo info = Introspector.getBeanInfo(targetClass); |
118 |
|
|
119 | 184 |
return new ClassAdaptor(info.getPropertyDescriptors()); |
120 |
} |
|
121 |
catch (Exception ex)
|
|
122 |
{ |
|
123 | 1 |
throw new ApplicationRuntimeException( |
124 |
UtilMessages.unableToIntrospect(targetClass, ex), |
|
125 |
target, |
|
126 |
null,
|
|
127 |
ex); |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
} |
|
132 |
|
|