1 | |
package org.apache.maven.shared.tools.test; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.lang.reflect.Field; |
23 | |
import java.lang.reflect.InvocationTargetException; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.codehaus.plexus.util.ReflectionUtils; |
29 | |
import org.codehaus.plexus.util.StringUtils; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ReflectiveSetter |
35 | |
{ |
36 | 0 | private Map cachedPropertySetters = new HashMap(); |
37 | |
|
38 | |
private final Class targetClass; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public ReflectiveSetter( Class targetClass ) |
44 | 0 | { |
45 | 0 | this.targetClass = targetClass; |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public void setProperty( String propertyName, Object value, Object target ) |
55 | |
throws Throwable |
56 | |
{ |
57 | |
|
58 | 0 | String preferredMethodName = "set" + StringUtils.capitalizeFirstLetter( propertyName ); |
59 | |
|
60 | 0 | Setter setter = null; |
61 | |
|
62 | 0 | Method method = ReflectionUtils.getSetter( preferredMethodName, targetClass ); |
63 | |
|
64 | 0 | if ( method != null ) |
65 | |
{ |
66 | 0 | setter = new MethodSetter( propertyName, method ); |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 0 | Field field = ReflectionUtils.getFieldByNameIncludingSuperclasses( propertyName, targetClass ); |
71 | |
|
72 | 0 | setter = new FieldSetter( propertyName, field ); |
73 | |
} |
74 | |
|
75 | 0 | if ( setter == null ) |
76 | |
{ |
77 | 0 | throw new IllegalArgumentException( "No such property: " + propertyName + " in: " + targetClass |
78 | |
+ ". Searched for: {method:" + preferredMethodName + ", method:" + propertyName + ", field:" |
79 | |
+ propertyName + "}" ); |
80 | |
} |
81 | |
|
82 | 0 | cachedPropertySetters.put( setter.getProperty(), setter ); |
83 | |
|
84 | |
try |
85 | |
{ |
86 | 0 | setter.set( value, target ); |
87 | |
} |
88 | 0 | catch ( InvocationTargetException e ) |
89 | |
{ |
90 | 0 | throw e.getTargetException(); |
91 | 0 | } |
92 | 0 | } |
93 | |
|
94 | |
private interface Setter |
95 | |
{ |
96 | |
void set( Object value, Object target ) |
97 | |
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; |
98 | |
|
99 | |
String getProperty(); |
100 | |
} |
101 | |
|
102 | |
private static class MethodSetter |
103 | |
implements Setter |
104 | |
{ |
105 | |
private Method method; |
106 | |
|
107 | |
private String name; |
108 | |
|
109 | |
MethodSetter( String name, Method method ) |
110 | 0 | { |
111 | 0 | this.name = name; |
112 | 0 | this.method = method; |
113 | 0 | } |
114 | |
|
115 | |
|
116 | |
public String getProperty() |
117 | |
{ |
118 | 0 | return name; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
public void set( Object value, Object target ) |
123 | |
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException |
124 | |
{ |
125 | 0 | boolean wasAccessible = method.isAccessible(); |
126 | |
|
127 | 0 | method.setAccessible( true ); |
128 | |
try |
129 | |
{ |
130 | 0 | method.invoke( target, new Object[] { value } ); |
131 | |
} |
132 | |
finally |
133 | |
{ |
134 | 0 | method.setAccessible( wasAccessible ); |
135 | 0 | } |
136 | 0 | } |
137 | |
} |
138 | |
|
139 | |
private static class FieldSetter |
140 | |
implements Setter |
141 | |
{ |
142 | |
private Field field; |
143 | |
|
144 | |
private String name; |
145 | |
|
146 | |
FieldSetter( String name, Field field ) |
147 | 0 | { |
148 | 0 | this.name = name; |
149 | 0 | this.field = field; |
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
public String getProperty() |
154 | |
{ |
155 | 0 | return name; |
156 | |
} |
157 | |
|
158 | |
|
159 | |
public void set( Object value, Object target ) |
160 | |
throws IllegalArgumentException, IllegalAccessException, InvocationTargetException |
161 | |
{ |
162 | 0 | boolean wasAccessible = field.isAccessible(); |
163 | |
|
164 | 0 | field.setAccessible( true ); |
165 | |
try |
166 | |
{ |
167 | 0 | field.set( target, value ); |
168 | |
} |
169 | |
finally |
170 | |
{ |
171 | 0 | field.setAccessible( wasAccessible ); |
172 | 0 | } |
173 | 0 | } |
174 | |
} |
175 | |
} |