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