|
|||||||||||||||||||
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 | |||||||||||||||
PropertyAdaptor.java | 100% | 100% | 100% | 100% |
|
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.PropertyEditor;
|
|
18 |
import java.beans.PropertyEditorManager;
|
|
19 |
import java.lang.reflect.Constructor;
|
|
20 |
import java.lang.reflect.Method;
|
|
21 |
|
|
22 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Used to manage dynamic access to a property of a specific class.
|
|
26 |
*
|
|
27 |
* @author Howard Lewis Ship
|
|
28 |
*/
|
|
29 |
public class PropertyAdaptor |
|
30 |
{ |
|
31 |
private String _propertyName;
|
|
32 |
|
|
33 |
private Class _propertyType;
|
|
34 |
|
|
35 |
private Method _readMethod;
|
|
36 |
|
|
37 |
private Method _writeMethod;
|
|
38 |
|
|
39 | 3920 |
PropertyAdaptor(String propertyName, Class propertyType, Method readMethod, Method writeMethod) |
40 |
{ |
|
41 | 3920 |
_propertyName = propertyName; |
42 | 3920 |
_propertyType = propertyType; |
43 | 3920 |
_readMethod = readMethod; |
44 | 3920 |
_writeMethod = writeMethod; |
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* Returns the name of the method used to read the property, or null if the property is not
|
|
49 |
* readable.
|
|
50 |
*/
|
|
51 | 2 |
public String getReadMethodName()
|
52 |
{ |
|
53 | 2 |
return _readMethod == null ? null : _readMethod.getName(); |
54 |
} |
|
55 |
|
|
56 |
/**
|
|
57 |
* Returns the name of the method used to write the property, or null if the property is not
|
|
58 |
* writable.
|
|
59 |
*/
|
|
60 | 2 |
public String getWriteMethodName()
|
61 |
{ |
|
62 | 2 |
return _writeMethod == null ? null : _writeMethod.getName(); |
63 |
} |
|
64 |
|
|
65 | 899 |
public String getPropertyName()
|
66 |
{ |
|
67 | 899 |
return _propertyName;
|
68 |
} |
|
69 |
|
|
70 | 6597 |
public Class getPropertyType()
|
71 |
{ |
|
72 | 6597 |
return _propertyType;
|
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Updates the property of the target object.
|
|
77 |
*
|
|
78 |
* @param target
|
|
79 |
* the object to update
|
|
80 |
* @param value
|
|
81 |
* the value to be stored into the target object property
|
|
82 |
*/
|
|
83 | 6471 |
public void write(Object target, Object value) |
84 |
{ |
|
85 | 6471 |
if (_writeMethod == null) |
86 | 1 |
throw new ApplicationRuntimeException(UtilMessages.noPropertyWriter( |
87 |
_propertyName, |
|
88 |
target), target, null, null); |
|
89 |
|
|
90 | 6470 |
try
|
91 |
{ |
|
92 | 6470 |
_writeMethod.invoke(target, new Object[]
|
93 |
{ value }); |
|
94 |
|
|
95 |
} |
|
96 |
catch (Exception ex)
|
|
97 |
{ |
|
98 | 2 |
throw new ApplicationRuntimeException(UtilMessages.writeFailure( |
99 |
_propertyName, |
|
100 |
target, |
|
101 |
ex), target, null, ex);
|
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 | 13 |
public void smartWrite(Object target, String value) |
106 |
{ |
|
107 | 13 |
Object convertedValue = convertValueForAssignment(target, value); |
108 |
|
|
109 | 10 |
write(target, convertedValue); |
110 |
} |
|
111 |
|
|
112 |
/** @since 1.1 */
|
|
113 | 13 |
private Object convertValueForAssignment(Object target, String value)
|
114 |
{ |
|
115 | 13 |
if (value == null || _propertyType.isInstance(value)) |
116 | 3 |
return value;
|
117 |
|
|
118 | 10 |
PropertyEditor e = PropertyEditorManager.findEditor(_propertyType); |
119 |
|
|
120 | 10 |
if (e == null) |
121 |
{ |
|
122 | 3 |
Object convertedValue = instantiateViaStringConstructor(target, value); |
123 |
|
|
124 | 3 |
if (convertedValue != null) |
125 | 2 |
return convertedValue;
|
126 |
|
|
127 | 1 |
throw new ApplicationRuntimeException(UtilMessages.noPropertyEditor( |
128 |
_propertyName, |
|
129 |
target.getClass())); |
|
130 |
} |
|
131 |
|
|
132 | 7 |
try
|
133 |
{ |
|
134 | 7 |
e.setAsText(value); |
135 |
|
|
136 | 5 |
return e.getValue();
|
137 |
} |
|
138 |
catch (Exception ex)
|
|
139 |
{ |
|
140 | 2 |
throw new ApplicationRuntimeException(UtilMessages.unableToConvert( |
141 |
value, |
|
142 |
_propertyType, |
|
143 |
_propertyName, |
|
144 |
target, |
|
145 |
ex), null, ex);
|
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* Checks to see if this adaptor's property type has a public constructor that takes a single
|
|
151 |
* String argument.
|
|
152 |
*/
|
|
153 |
|
|
154 | 3 |
private Object instantiateViaStringConstructor(Object target, String value)
|
155 |
{ |
|
156 | 3 |
try
|
157 |
{ |
|
158 | 3 |
Constructor c = _propertyType.getConstructor(new Class[]
|
159 |
{ String.class });
|
|
160 |
|
|
161 | 2 |
return c.newInstance(new Object[] |
162 |
{ value }); |
|
163 |
} |
|
164 |
catch (Exception ex)
|
|
165 |
{ |
|
166 | 1 |
return null; |
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* Returns true if there's a write method for the property.
|
|
172 |
*/
|
|
173 | 1624 |
public boolean isWritable() |
174 |
{ |
|
175 | 1624 |
return _writeMethod != null; |
176 |
} |
|
177 |
|
|
178 |
/**
|
|
179 |
* Reads the property of the target object.
|
|
180 |
*
|
|
181 |
* @param target
|
|
182 |
* the object to read a property from
|
|
183 |
*/
|
|
184 | 8 |
public Object read(Object target)
|
185 |
{ |
|
186 | 8 |
if (_readMethod == null) |
187 | 1 |
throw new ApplicationRuntimeException(UtilMessages.noReader(_propertyName, target), |
188 |
target, null, null); |
|
189 |
|
|
190 | 7 |
try
|
191 |
{ |
|
192 | 7 |
return _readMethod.invoke(target, null); |
193 |
|
|
194 |
} |
|
195 |
catch (Exception ex)
|
|
196 |
{ |
|
197 | 1 |
throw new ApplicationRuntimeException(UtilMessages.readFailure( |
198 |
_propertyName, |
|
199 |
target, |
|
200 |
ex), target, null, ex);
|
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
/**
|
|
205 |
* Returns true if there's a read method for the property.
|
|
206 |
*/
|
|
207 |
|
|
208 | 6 |
public boolean isReadable() |
209 |
{ |
|
210 | 6 |
return _readMethod != null; |
211 |
} |
|
212 |
|
|
213 |
} |
|