|
|||||||||||||||||||
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 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.lang.reflect.Method;
|
|
18 |
|
|
19 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Used to manage dynamic access to a property of a specific class.
|
|
23 |
*
|
|
24 |
* @author Howard Lewis Ship
|
|
25 |
*/
|
|
26 |
public class PropertyAdaptor |
|
27 |
{ |
|
28 |
private String _propertyName;
|
|
29 |
private Class _propertyType;
|
|
30 |
private Method _readMethod;
|
|
31 |
|
|
32 |
private Method _writeMethod;
|
|
33 |
|
|
34 | 4023 |
PropertyAdaptor(String propertyName, Class propertyType, Method readMethod, Method writeMethod) |
35 |
{ |
|
36 | 4023 |
_propertyName = propertyName; |
37 | 4023 |
_propertyType = propertyType; |
38 | 4023 |
_readMethod = readMethod; |
39 | 4023 |
_writeMethod = writeMethod; |
40 |
} |
|
41 |
|
|
42 |
/**
|
|
43 |
* Returns the name of the method used to read the property, or null if the property is
|
|
44 |
* not readable.
|
|
45 |
*/
|
|
46 | 2 |
public String getReadMethodName()
|
47 |
{ |
|
48 | 2 |
return _readMethod == null ? null : _readMethod.getName(); |
49 |
} |
|
50 |
|
|
51 |
/**
|
|
52 |
* Returns the name of the method used to write the property, or null if the property
|
|
53 |
* is not writable.
|
|
54 |
*/
|
|
55 | 2 |
public String getWriteMethodName()
|
56 |
{ |
|
57 | 2 |
return _writeMethod == null ? null : _writeMethod.getName(); |
58 |
} |
|
59 |
|
|
60 | 883 |
public String getPropertyName()
|
61 |
{ |
|
62 | 883 |
return _propertyName;
|
63 |
} |
|
64 |
|
|
65 | 6165 |
public Class getPropertyType()
|
66 |
{ |
|
67 | 6165 |
return _propertyType;
|
68 |
} |
|
69 |
|
|
70 |
/**
|
|
71 |
* Updates the property of the target object.
|
|
72 |
*
|
|
73 |
* @param target the object to update
|
|
74 |
* @param value the value to be stored into the target object property
|
|
75 |
*/
|
|
76 | 6103 |
public void write(Object target, Object value) |
77 |
{ |
|
78 | 6103 |
if (_writeMethod == null) |
79 | 1 |
throw new ApplicationRuntimeException( |
80 |
UtilMessages.noPropertyWriter(_propertyName, target), |
|
81 |
target, |
|
82 |
null,
|
|
83 |
null);
|
|
84 |
|
|
85 | 6102 |
try
|
86 |
{ |
|
87 | 6102 |
_writeMethod.invoke(target, new Object[] { value });
|
88 |
|
|
89 |
} |
|
90 |
catch (Exception ex)
|
|
91 |
{ |
|
92 | 2 |
throw new ApplicationRuntimeException( |
93 |
UtilMessages.writeFailure(_propertyName, target, ex), |
|
94 |
target, |
|
95 |
null,
|
|
96 |
ex); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/**
|
|
101 |
* Returns true if there's a write method for the property.
|
|
102 |
*/
|
|
103 | 1595 |
public boolean isWritable() |
104 |
{ |
|
105 | 1595 |
return _writeMethod != null; |
106 |
} |
|
107 |
|
|
108 |
/**
|
|
109 |
* Reads the property of the target object.
|
|
110 |
*
|
|
111 |
* @param target the object to read a property from
|
|
112 |
*/
|
|
113 | 8 |
public Object read(Object target)
|
114 |
{ |
|
115 | 8 |
if (_readMethod == null) |
116 | 1 |
throw new ApplicationRuntimeException( |
117 |
UtilMessages.noReader(_propertyName, target), |
|
118 |
target, |
|
119 |
null,
|
|
120 |
null);
|
|
121 |
|
|
122 | 7 |
try
|
123 |
{ |
|
124 | 7 |
return _readMethod.invoke(target, null); |
125 |
|
|
126 |
} |
|
127 |
catch (Exception ex)
|
|
128 |
{ |
|
129 | 1 |
throw new ApplicationRuntimeException( |
130 |
UtilMessages.readFailure(_propertyName, target, ex), |
|
131 |
target, |
|
132 |
null,
|
|
133 |
ex); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Returns true if there's a read method for the property.
|
|
139 |
*/
|
|
140 |
|
|
141 | 6 |
public boolean isReadable() |
142 |
{ |
|
143 | 6 |
return _readMethod != null; |
144 |
} |
|
145 |
} |
|
146 |
|
|