|
|||||||||||||||||||
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 |
class PropertyAdaptor
|
|
27 |
{ |
|
28 |
private String _propertyName;
|
|
29 |
private Class _propertyType;
|
|
30 |
private Method _readMethod;
|
|
31 |
|
|
32 |
private Method _writeMethod;
|
|
33 |
|
|
34 | 679 |
PropertyAdaptor(String propertyName, Class propertyType, Method readMethod, Method writeMethod) |
35 |
{ |
|
36 | 679 |
_propertyName = propertyName; |
37 | 679 |
_propertyType = propertyType; |
38 | 679 |
_readMethod = readMethod; |
39 | 679 |
_writeMethod = writeMethod; |
40 |
} |
|
41 |
|
|
42 | 3458 |
public Class getPropertyType()
|
43 |
{ |
|
44 | 3458 |
return _propertyType;
|
45 |
} |
|
46 |
|
|
47 |
/**
|
|
48 |
* Updates the property of the target object.
|
|
49 |
*
|
|
50 |
* @param target the object to update
|
|
51 |
* @param value the value to be stored into the target object property
|
|
52 |
*/
|
|
53 | 3509 |
public void write(Object target, Object value) |
54 |
{ |
|
55 | 3509 |
if (_writeMethod == null) |
56 | 1 |
throw new ApplicationRuntimeException( |
57 |
UtilMessages.noPropertyWriter(_propertyName, target), |
|
58 |
target, |
|
59 |
null,
|
|
60 |
null);
|
|
61 |
|
|
62 | 3508 |
try
|
63 |
{ |
|
64 | 3508 |
_writeMethod.invoke(target, new Object[] { value });
|
65 |
|
|
66 |
} |
|
67 |
catch (Exception ex)
|
|
68 |
{ |
|
69 | 2 |
throw new ApplicationRuntimeException( |
70 |
UtilMessages.writeFailure(_propertyName, target, ex), |
|
71 |
target, |
|
72 |
null,
|
|
73 |
ex); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
/**
|
|
78 |
* Returns true if there's a write method for the property.
|
|
79 |
*/
|
|
80 | 23 |
public boolean isWritable() |
81 |
{ |
|
82 | 23 |
return _writeMethod != null; |
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Reads the property of the target object.
|
|
87 |
*
|
|
88 |
* @param target the object to read a property from
|
|
89 |
*/
|
|
90 | 7 |
public Object read(Object target)
|
91 |
{ |
|
92 | 7 |
if (_readMethod == null) |
93 | 1 |
throw new ApplicationRuntimeException( |
94 |
UtilMessages.noReader(_propertyName, target), |
|
95 |
target, |
|
96 |
null,
|
|
97 |
null);
|
|
98 |
|
|
99 | 6 |
try
|
100 |
{ |
|
101 | 6 |
return _readMethod.invoke(target, null); |
102 |
|
|
103 |
} |
|
104 |
catch (Exception ex)
|
|
105 |
{ |
|
106 | 1 |
throw new ApplicationRuntimeException( |
107 |
UtilMessages.readFailure(_propertyName, target, ex), |
|
108 |
target, |
|
109 |
null,
|
|
110 |
ex); |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Returns true if there's a read method for the property.
|
|
116 |
*/
|
|
117 |
|
|
118 | 3 |
public boolean isReadable() |
119 |
{ |
|
120 | 3 |
return _readMethod != null; |
121 |
} |
|
122 |
} |
|
123 |
|
|