/
MethodPropertyImpl.java
218 lines (197 loc) · 7.02 KB
/
MethodPropertyImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
*
*/
package org.jboss.weld.extensions.properties;
import static org.jboss.weld.extensions.reflection.Reflections.invokeMethod;
import java.beans.Introspector;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
/**
* A bean property based on the value represented by a getter/setter method pair
*
* @author Pete Muir
* @author Shane Bryzak
* @author Dan Allen
*/
class MethodPropertyImpl<V> implements MethodProperty<V>
{
private static final String GETTER_METHOD_PREFIX = "get";
private static final String SETTER_METHOD_PREFIX = "set";
private static final String BOOLEAN_GETTER_METHOD_PREFIX = "is";
private static final int GETTER_METHOD_PREFIX_LENGTH = GETTER_METHOD_PREFIX.length();
private static final int SETTER_METHOD_PREFIX_LENGTH = SETTER_METHOD_PREFIX.length();
private static final int BOOLEAN_GETTER_METHOD_PREFIX_LENGTH = BOOLEAN_GETTER_METHOD_PREFIX.length();
private final Method getterMethod;
private final String propertyName;
private final Method setterMethod;
public MethodPropertyImpl(Method method)
{
final String accessorMethodPrefix;
final String propertyNameInAccessorMethod;
if (method.getName().startsWith(GETTER_METHOD_PREFIX))
{
if (method.getReturnType() == Void.TYPE)
{
throw new IllegalArgumentException("Invalid accessor method, must have return value if starts with 'get'. Method: " + method);
}
else if (method.getParameterTypes().length > 0)
{
throw new IllegalArgumentException("Invalid accessor method, must have zero arguments if starts with 'get'. Method: " + method);
}
propertyNameInAccessorMethod = method.getName().substring(GETTER_METHOD_PREFIX_LENGTH);
accessorMethodPrefix = GETTER_METHOD_PREFIX;
}
else if (method.getName().startsWith(SETTER_METHOD_PREFIX))
{
if (method.getReturnType() != Void.TYPE)
{
throw new IllegalArgumentException("Invalid accessor method, must not have return value if starts with 'set'. Method: " + method);
}
else if (method.getParameterTypes().length != 1)
{
throw new IllegalArgumentException("Invalid accessor method, must have one argument if starts with 'set'. Method: " + method);
}
propertyNameInAccessorMethod = method.getName().substring(SETTER_METHOD_PREFIX_LENGTH);
accessorMethodPrefix = SETTER_METHOD_PREFIX;
}
else if (method.getName().startsWith(BOOLEAN_GETTER_METHOD_PREFIX))
{
if (method.getReturnType() != Boolean.class || !method.getReturnType().isPrimitive())
{
throw new IllegalArgumentException("Invalid accessor method, must return boolean primitive if starts with 'is'. Method: " + method);
}
propertyNameInAccessorMethod = method.getName().substring(BOOLEAN_GETTER_METHOD_PREFIX_LENGTH);
accessorMethodPrefix = BOOLEAN_GETTER_METHOD_PREFIX;
}
else
{
throw new IllegalArgumentException("Invalid accessor method, must start with 'get', 'set' or 'is'. " + "Method: " + method);
}
if (propertyNameInAccessorMethod.length() == 0 || !Character.isUpperCase(propertyNameInAccessorMethod.charAt(0)))
{
throw new IllegalArgumentException("Invalid accessor method, prefix '" + accessorMethodPrefix +
"' must be followed a non-empty property name, capitalized. Method: " + method);
}
this.propertyName = Introspector.decapitalize(propertyNameInAccessorMethod);
this.getterMethod = getGetterMethod(method.getDeclaringClass(), propertyName);
this.setterMethod = getSetterMethod(method.getDeclaringClass(), propertyName);
}
public String getName()
{
return propertyName;
}
@SuppressWarnings("unchecked")
public Class<V> getJavaClass()
{
return (Class<V>) getterMethod.getReturnType();
}
public Type getBaseType()
{
return getterMethod.getGenericReturnType();
}
public Method getAnnotatedElement()
{
return getterMethod;
}
public Member getMember()
{
return getterMethod;
}
public V getValue(Object instance)
{
return getJavaClass().cast(invokeMethod(getterMethod, instance));
}
public void setValue(Object instance, V value)
{
invokeMethod(setterMethod, instance, value);
}
private static Method getSetterMethod(Class<?> clazz, String name)
{
Method[] methods = clazz.getMethods();
for (Method method : methods)
{
String methodName = method.getName();
if (methodName.startsWith(SETTER_METHOD_PREFIX) && method.getParameterTypes().length == 1)
{
if (Introspector.decapitalize(methodName.substring(SETTER_METHOD_PREFIX_LENGTH)).equals(name))
{
return method;
}
}
}
return null;
}
private static Method getGetterMethod(Class<?> clazz, String name)
{
for (Method method : clazz.getDeclaredMethods())
{
String methodName = method.getName();
if (method.getParameterTypes().length == 0)
{
if (methodName.startsWith(GETTER_METHOD_PREFIX))
{
if (Introspector.decapitalize(methodName.substring(GETTER_METHOD_PREFIX_LENGTH)).equals(name))
{
return method;
}
}
else if (methodName.startsWith(BOOLEAN_GETTER_METHOD_PREFIX))
{
if (Introspector.decapitalize(methodName.substring(BOOLEAN_GETTER_METHOD_PREFIX_LENGTH)).equals(name))
{
return method;
}
}
}
}
throw new IllegalArgumentException("no such getter method: " + clazz.getName() + '.' + name);
}
public Class<?> getDeclaringClass()
{
return getterMethod.getDeclaringClass();
}
public boolean isReadOnly()
{
return setterMethod == null;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
if (isReadOnly())
{
builder.append("read-only ").append(setterMethod.toString()).append("; ");
}
builder.append(getterMethod.toString());
return builder.toString();
}
@Override
public int hashCode()
{
int hash = 1;
hash = hash * 31 + (setterMethod == null ? 0 : setterMethod.hashCode());
hash = hash * 31 + getterMethod.hashCode();
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof MethodPropertyImpl<?>)
{
MethodPropertyImpl<?> that = (MethodPropertyImpl<?>) obj;
if (this.setterMethod == null)
{
return that.setterMethod == null && this.getterMethod.equals(that.getterMethod);
}
else
{
return this.setterMethod.equals(that.setterMethod) && this.getterMethod.equals(that.getterMethod);
}
}
else
{
return false;
}
}
}