author | emcmanus |
Fri, 08 Aug 2008 15:08:57 +0200 | |
changeset 1018 | 9f07e65e9653 |
parent 832 | 5484c7a35278 |
child 1221 | e3dc70e4e610 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.jmx.mbeanserver; |
|
27 |
||
28 |
import static com.sun.jmx.mbeanserver.Util.*; |
|
687 | 29 |
import java.lang.annotation.ElementType; |
30 |
import javax.management.openmbean.MXBeanMappingClass; |
|
2 | 31 |
|
32 |
import static javax.management.openmbean.SimpleType.*; |
|
33 |
||
34 |
import com.sun.jmx.remote.util.EnvHelp; |
|
35 |
||
36 |
import java.beans.ConstructorProperties; |
|
37 |
import java.io.InvalidObjectException; |
|
38 |
import java.lang.annotation.ElementType; |
|
39 |
import java.lang.ref.WeakReference; |
|
40 |
import java.lang.reflect.Array; |
|
41 |
import java.lang.reflect.Constructor; |
|
42 |
import java.lang.reflect.Field; |
|
43 |
import java.lang.reflect.GenericArrayType; |
|
44 |
import java.lang.reflect.Method; |
|
45 |
import java.lang.reflect.Modifier; |
|
46 |
import java.lang.reflect.ParameterizedType; |
|
47 |
import java.lang.reflect.Proxy; |
|
48 |
import java.lang.reflect.Type; |
|
49 |
import java.util.ArrayList; |
|
50 |
import java.util.Arrays; |
|
51 |
import java.util.BitSet; |
|
52 |
import java.util.Collection; |
|
53 |
import java.util.Comparator; |
|
54 |
import java.util.HashSet; |
|
55 |
import java.util.List; |
|
56 |
import java.util.Map; |
|
57 |
import java.util.Set; |
|
58 |
import java.util.SortedMap; |
|
59 |
import java.util.SortedSet; |
|
60 |
import java.util.TreeSet; |
|
61 |
import java.util.WeakHashMap; |
|
62 |
||
63 |
import javax.management.JMX; |
|
64 |
import javax.management.ObjectName; |
|
65 |
import javax.management.openmbean.ArrayType; |
|
66 |
import javax.management.openmbean.CompositeData; |
|
67 |
import javax.management.openmbean.CompositeDataInvocationHandler; |
|
68 |
import javax.management.openmbean.CompositeDataSupport; |
|
69 |
import javax.management.openmbean.CompositeDataView; |
|
70 |
import javax.management.openmbean.CompositeType; |
|
687 | 71 |
import javax.management.openmbean.MXBeanMapping; |
72 |
import javax.management.openmbean.MXBeanMappingFactory; |
|
2 | 73 |
import javax.management.openmbean.OpenDataException; |
74 |
import javax.management.openmbean.OpenType; |
|
75 |
import javax.management.openmbean.SimpleType; |
|
76 |
import javax.management.openmbean.TabularData; |
|
77 |
import javax.management.openmbean.TabularDataSupport; |
|
78 |
import javax.management.openmbean.TabularType; |
|
79 |
||
80 |
/** |
|
687 | 81 |
* <p>A converter between Java types and the limited set of classes |
82 |
* defined by Open MBeans.</p> |
|
83 |
* |
|
84 |
* <p>A Java type is an instance of java.lang.reflect.Type. For our |
|
85 |
* purposes, it is either a Class, such as String.class or int.class; |
|
86 |
* or a ParameterizedType, such as List<String> or Map<Integer, |
|
87 |
* String[]>. On J2SE 1.4 and earlier, it can only be a Class.</p> |
|
88 |
* |
|
89 |
* <p>Each Type is associated with an DefaultMXBeanMappingFactory. The |
|
90 |
* DefaultMXBeanMappingFactory defines an OpenType corresponding to the Type, plus a |
|
91 |
* Java class corresponding to the OpenType. For example:</p> |
|
92 |
* |
|
93 |
* <pre> |
|
94 |
* Type Open class OpenType |
|
95 |
* ---- ---------- -------- |
|
96 |
* Integer Integer SimpleType.INTEGER |
|
97 |
* int int SimpleType.INTEGER |
|
98 |
* Integer[] Integer[] ArrayType(1, SimpleType.INTEGER) |
|
99 |
* int[] Integer[] ArrayType(SimpleType.INTEGER, true) |
|
100 |
* String[][] String[][] ArrayType(2, SimpleType.STRING) |
|
101 |
* List<String> String[] ArrayType(1, SimpleType.STRING) |
|
102 |
* ThreadState (an Enum) String SimpleType.STRING |
|
103 |
* Map<Integer, String[]> TabularData TabularType( |
|
104 |
* CompositeType( |
|
105 |
* {"key", SimpleType.INTEGER}, |
|
106 |
* {"value", |
|
107 |
* ArrayType(1, |
|
108 |
* SimpleType.STRING)}), |
|
109 |
* indexNames={"key"}) |
|
110 |
* </pre> |
|
111 |
* |
|
112 |
* <p>Apart from simple types, arrays, and collections, Java types are |
|
113 |
* converted through introspection into CompositeType. The Java type |
|
114 |
* must have at least one getter (method such as "int getSize()" or |
|
115 |
* "boolean isBig()"), and we must be able to deduce how to |
|
116 |
* reconstruct an instance of the Java class from the values of the |
|
117 |
* getters using one of various heuristics.</p> |
|
118 |
* |
|
119 |
* @since 1.6 |
|
120 |
*/ |
|
121 |
public class DefaultMXBeanMappingFactory extends MXBeanMappingFactory { |
|
122 |
static abstract class NonNullMXBeanMapping extends MXBeanMapping { |
|
123 |
NonNullMXBeanMapping(Type javaType, OpenType openType) { |
|
124 |
super(javaType, openType); |
|
125 |
} |
|
2 | 126 |
|
687 | 127 |
@Override |
128 |
public final Object fromOpenValue(Object openValue) |
|
129 |
throws InvalidObjectException { |
|
130 |
if (openValue == null) |
|
131 |
return null; |
|
132 |
else |
|
133 |
return fromNonNullOpenValue(openValue); |
|
134 |
} |
|
2 | 135 |
|
687 | 136 |
@Override |
137 |
public final Object toOpenValue(Object javaValue) throws OpenDataException { |
|
138 |
if (javaValue == null) |
|
139 |
return null; |
|
140 |
else |
|
141 |
return toNonNullOpenValue(javaValue); |
|
142 |
} |
|
143 |
||
144 |
abstract Object fromNonNullOpenValue(Object openValue) |
|
145 |
throws InvalidObjectException; |
|
2 | 146 |
|
687 | 147 |
abstract Object toNonNullOpenValue(Object javaValue) |
148 |
throws OpenDataException; |
|
2 | 149 |
|
687 | 150 |
/** |
151 |
* <p>True if and only if this MXBeanMapping's toOpenValue and |
|
152 |
* fromOpenValue methods are the identity function.</p> |
|
153 |
*/ |
|
154 |
boolean isIdentity() { |
|
155 |
return false; |
|
156 |
} |
|
2 | 157 |
} |
158 |
||
687 | 159 |
static boolean isIdentity(MXBeanMapping mapping) { |
160 |
return (mapping instanceof NonNullMXBeanMapping && |
|
161 |
((NonNullMXBeanMapping) mapping).isIdentity()); |
|
2 | 162 |
} |
163 |
||
687 | 164 |
private static final class Mappings |
165 |
extends WeakHashMap<Type, WeakReference<MXBeanMapping>> {} |
|
2 | 166 |
|
687 | 167 |
private static final Map<MXBeanMappingFactory, Mappings> factoryMappings = |
168 |
new WeakHashMap<MXBeanMappingFactory, Mappings>(); |
|
169 |
||
170 |
private static final Map<Type, MXBeanMapping> permanentMappings = newMap(); |
|
2 | 171 |
|
687 | 172 |
private static synchronized MXBeanMapping getMapping( |
173 |
Type type, MXBeanMappingFactory factory) { |
|
174 |
Mappings mappings = factoryMappings.get(factory); |
|
175 |
if (mappings == null) { |
|
176 |
mappings = new Mappings(); |
|
177 |
factoryMappings.put(factory, mappings); |
|
178 |
} |
|
179 |
WeakReference<MXBeanMapping> wr = mappings.get(type); |
|
2 | 180 |
return (wr == null) ? null : wr.get(); |
181 |
} |
|
182 |
||
687 | 183 |
private static synchronized void putMapping( |
184 |
Type type, MXBeanMapping mapping, MXBeanMappingFactory factory) { |
|
185 |
Mappings mappings = factoryMappings.get(factory); |
|
186 |
if (mappings == null) { |
|
187 |
mappings = new Mappings(); |
|
188 |
factoryMappings.put(factory, mappings); |
|
189 |
} |
|
190 |
WeakReference<MXBeanMapping> wr = |
|
191 |
new WeakReference<MXBeanMapping>(mapping); |
|
192 |
mappings.put(type, wr); |
|
2 | 193 |
} |
194 |
||
195 |
static { |
|
196 |
/* Set up the mappings for Java types that map to SimpleType. */ |
|
197 |
||
198 |
final OpenType[] simpleTypes = { |
|
199 |
BIGDECIMAL, BIGINTEGER, BOOLEAN, BYTE, CHARACTER, DATE, |
|
200 |
DOUBLE, FLOAT, INTEGER, LONG, OBJECTNAME, SHORT, STRING, |
|
201 |
VOID, |
|
202 |
}; |
|
203 |
||
204 |
for (int i = 0; i < simpleTypes.length; i++) { |
|
205 |
final OpenType t = simpleTypes[i]; |
|
206 |
Class c; |
|
207 |
try { |
|
208 |
c = Class.forName(t.getClassName(), false, |
|
209 |
ObjectName.class.getClassLoader()); |
|
210 |
} catch (ClassNotFoundException e) { |
|
211 |
// the classes that these predefined types declare must exist! |
|
212 |
throw new Error(e); |
|
213 |
} |
|
687 | 214 |
final MXBeanMapping mapping = new IdentityMapping(c, t); |
215 |
permanentMappings.put(c, mapping); |
|
2 | 216 |
|
217 |
if (c.getName().startsWith("java.lang.")) { |
|
218 |
try { |
|
219 |
final Field typeField = c.getField("TYPE"); |
|
687 | 220 |
final Class<?> primitiveType = (Class<?>) typeField.get(null); |
221 |
final MXBeanMapping primitiveMapping = |
|
222 |
new IdentityMapping(primitiveType, t); |
|
223 |
permanentMappings.put(primitiveType, primitiveMapping); |
|
2 | 224 |
if (primitiveType != void.class) { |
225 |
final Class<?> primitiveArrayType = |
|
226 |
Array.newInstance(primitiveType, 0).getClass(); |
|
227 |
final OpenType primitiveArrayOpenType = |
|
228 |
ArrayType.getPrimitiveArrayType(primitiveArrayType); |
|
687 | 229 |
final MXBeanMapping primitiveArrayMapping = |
230 |
new IdentityMapping(primitiveArrayType, |
|
231 |
primitiveArrayOpenType); |
|
232 |
permanentMappings.put(primitiveArrayType, |
|
233 |
primitiveArrayMapping); |
|
2 | 234 |
} |
235 |
} catch (NoSuchFieldException e) { |
|
236 |
// OK: must not be a primitive wrapper |
|
237 |
} catch (IllegalAccessException e) { |
|
238 |
// Should not reach here |
|
239 |
assert(false); |
|
240 |
} |
|
241 |
} |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/** Get the converter for the given Java type, creating it if necessary. */ |
|
687 | 246 |
@Override |
247 |
public synchronized MXBeanMapping mappingForType(Type objType, |
|
248 |
MXBeanMappingFactory factory) |
|
2 | 249 |
throws OpenDataException { |
250 |
if (inProgress.containsKey(objType)) |
|
251 |
throw new OpenDataException("Recursive data structure"); |
|
252 |
||
687 | 253 |
MXBeanMapping mapping; |
2 | 254 |
|
687 | 255 |
mapping = getMapping(objType, null); |
256 |
if (mapping != null) |
|
257 |
return mapping; |
|
2 | 258 |
|
259 |
inProgress.put(objType, objType); |
|
260 |
try { |
|
687 | 261 |
mapping = makeMapping(objType, factory); |
2 | 262 |
} finally { |
263 |
inProgress.remove(objType); |
|
264 |
} |
|
265 |
||
687 | 266 |
putMapping(objType, mapping, factory); |
267 |
return mapping; |
|
2 | 268 |
} |
269 |
||
687 | 270 |
private MXBeanMapping makeMapping(Type objType, MXBeanMappingFactory factory) |
271 |
throws OpenDataException { |
|
2 | 272 |
|
273 |
/* It's not yet worth formalizing these tests by having for example |
|
274 |
an array of factory classes, each of which says whether it |
|
275 |
recognizes the Type (Chain of Responsibility pattern). */ |
|
687 | 276 |
MXBeanMapping mapping = permanentMappings.get(objType); |
277 |
if (mapping != null) |
|
278 |
return mapping; |
|
279 |
Class<?> erasure = erasure(objType); |
|
280 |
MXBeanMappingClass mappingClass = |
|
281 |
erasure.getAnnotation(MXBeanMappingClass.class); |
|
282 |
if (mappingClass != null) |
|
283 |
return makeAnnotationMapping(mappingClass, objType, factory); |
|
2 | 284 |
if (objType instanceof GenericArrayType) { |
285 |
Type componentType = |
|
286 |
((GenericArrayType) objType).getGenericComponentType(); |
|
687 | 287 |
return makeArrayOrCollectionMapping(objType, componentType, factory); |
2 | 288 |
} else if (objType instanceof Class) { |
289 |
Class<?> objClass = (Class<?>) objType; |
|
290 |
if (objClass.isEnum()) { |
|
291 |
// Huge hack to avoid compiler warnings here. The ElementType |
|
292 |
// parameter is ignored but allows us to obtain a type variable |
|
293 |
// T that matches <T extends Enum<T>>. |
|
687 | 294 |
return makeEnumMapping((Class) objClass, ElementType.class); |
2 | 295 |
} else if (objClass.isArray()) { |
296 |
Type componentType = objClass.getComponentType(); |
|
687 | 297 |
return makeArrayOrCollectionMapping(objClass, componentType, |
298 |
factory); |
|
2 | 299 |
} else if (JMX.isMXBeanInterface(objClass)) { |
687 | 300 |
return makeMXBeanRefMapping(objClass); |
2 | 301 |
} else { |
687 | 302 |
return makeCompositeMapping(objClass, factory); |
2 | 303 |
} |
304 |
} else if (objType instanceof ParameterizedType) { |
|
687 | 305 |
return makeParameterizedTypeMapping((ParameterizedType) objType, |
306 |
factory); |
|
2 | 307 |
} else |
308 |
throw new OpenDataException("Cannot map type: " + objType); |
|
309 |
} |
|
310 |
||
687 | 311 |
private static MXBeanMapping |
312 |
makeAnnotationMapping(MXBeanMappingClass mappingClass, |
|
313 |
Type objType, |
|
314 |
MXBeanMappingFactory factory) |
|
315 |
throws OpenDataException { |
|
316 |
Class<? extends MXBeanMapping> c = mappingClass.value(); |
|
317 |
Constructor<? extends MXBeanMapping> cons; |
|
318 |
try { |
|
319 |
cons = c.getConstructor(Type.class); |
|
320 |
} catch (NoSuchMethodException e) { |
|
321 |
final String msg = |
|
322 |
"Annotation @" + MXBeanMappingClass.class.getName() + |
|
323 |
" must name a class with a public constructor that has a " + |
|
324 |
"single " + Type.class.getName() + " argument"; |
|
325 |
OpenDataException ode = new OpenDataException(msg); |
|
326 |
ode.initCause(e); |
|
327 |
throw ode; |
|
328 |
} |
|
329 |
try { |
|
330 |
return cons.newInstance(objType); |
|
331 |
} catch (Exception e) { |
|
332 |
final String msg = |
|
333 |
"Could not construct a " + c.getName() + " for @" + |
|
334 |
MXBeanMappingClass.class.getName(); |
|
335 |
OpenDataException ode = new OpenDataException(msg); |
|
336 |
ode.initCause(e); |
|
337 |
throw ode; |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
private static Class<?> erasure(Type t) { |
|
342 |
if (t instanceof Class<?>) |
|
343 |
return (Class<?>) t; |
|
344 |
if (t instanceof ParameterizedType) |
|
345 |
return erasure(((ParameterizedType) t).getRawType()); |
|
346 |
/* Other cases: GenericArrayType, TypeVariable, WildcardType. |
|
347 |
* Returning the erasure of GenericArrayType is not necessary because |
|
348 |
* anyway we will be recursing on the element type, and we'll erase |
|
349 |
* then. Returning the erasure of the other two would mean returning |
|
350 |
* the type bound (e.g. Foo in <T extends Foo> or <? extends Foo>) |
|
351 |
* and since we don't treat this as Foo elsewhere we shouldn't here. |
|
352 |
*/ |
|
353 |
return Object.class; |
|
354 |
} |
|
355 |
||
356 |
private static <T extends Enum<T>> MXBeanMapping |
|
357 |
makeEnumMapping(Class enumClass, Class<T> fake) { |
|
358 |
return new EnumMapping<T>(Util.<Class<T>>cast(enumClass)); |
|
2 | 359 |
} |
360 |
||
361 |
/* Make the converter for an array type, or a collection such as |
|
362 |
* List<String> or Set<Integer>. We never see one-dimensional |
|
363 |
* primitive arrays (e.g. int[]) here because they use the identity |
|
364 |
* converter and are registered as such in the static initializer. |
|
365 |
*/ |
|
687 | 366 |
private MXBeanMapping |
367 |
makeArrayOrCollectionMapping(Type collectionType, Type elementType, |
|
368 |
MXBeanMappingFactory factory) |
|
2 | 369 |
throws OpenDataException { |
370 |
||
687 | 371 |
final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory); |
372 |
final OpenType<?> elementOpenType = elementMapping.getOpenType(); |
|
2 | 373 |
final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType); |
687 | 374 |
final Class<?> elementOpenClass = elementMapping.getOpenClass(); |
2 | 375 |
|
376 |
final Class<?> openArrayClass; |
|
377 |
final String openArrayClassName; |
|
378 |
if (elementOpenClass.isArray()) |
|
379 |
openArrayClassName = "[" + elementOpenClass.getName(); |
|
380 |
else |
|
381 |
openArrayClassName = "[L" + elementOpenClass.getName() + ";"; |
|
382 |
try { |
|
383 |
openArrayClass = Class.forName(openArrayClassName); |
|
384 |
} catch (ClassNotFoundException e) { |
|
385 |
throw openDataException("Cannot obtain array class", e); |
|
386 |
} |
|
387 |
||
388 |
if (collectionType instanceof ParameterizedType) { |
|
687 | 389 |
return new CollectionMapping(collectionType, |
390 |
openType, openArrayClass, |
|
391 |
elementMapping); |
|
2 | 392 |
} else { |
687 | 393 |
if (isIdentity(elementMapping)) { |
394 |
return new IdentityMapping(collectionType, |
|
395 |
openType); |
|
2 | 396 |
} else { |
687 | 397 |
return new ArrayMapping(collectionType, |
2 | 398 |
openType, |
399 |
openArrayClass, |
|
687 | 400 |
elementMapping); |
2 | 401 |
} |
402 |
} |
|
403 |
} |
|
404 |
||
405 |
private static final String[] keyArray = {"key"}; |
|
406 |
private static final String[] keyValueArray = {"key", "value"}; |
|
407 |
||
687 | 408 |
private MXBeanMapping |
409 |
makeTabularMapping(Type objType, boolean sortedMap, |
|
410 |
Type keyType, Type valueType, |
|
411 |
MXBeanMappingFactory factory) |
|
2 | 412 |
throws OpenDataException { |
413 |
||
414 |
final String objTypeName = objType.toString(); |
|
687 | 415 |
final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory); |
416 |
final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory); |
|
417 |
final OpenType keyOpenType = keyMapping.getOpenType(); |
|
418 |
final OpenType valueOpenType = valueMapping.getOpenType(); |
|
2 | 419 |
final CompositeType rowType = |
420 |
new CompositeType(objTypeName, |
|
421 |
objTypeName, |
|
422 |
keyValueArray, |
|
423 |
keyValueArray, |
|
424 |
new OpenType[] {keyOpenType, valueOpenType}); |
|
425 |
final TabularType tabularType = |
|
426 |
new TabularType(objTypeName, objTypeName, rowType, keyArray); |
|
687 | 427 |
return new TabularMapping(objType, sortedMap, tabularType, |
428 |
keyMapping, valueMapping); |
|
2 | 429 |
} |
430 |
||
431 |
/* We know how to translate List<E>, Set<E>, SortedSet<E>, |
|
432 |
Map<K,V>, SortedMap<K,V>, and that's it. We don't accept |
|
433 |
subtypes of those because we wouldn't know how to deserialize |
|
434 |
them. We don't accept Queue<E> because it is unlikely people |
|
435 |
would use that as a parameter or return type in an MBean. */ |
|
687 | 436 |
private MXBeanMapping |
437 |
makeParameterizedTypeMapping(ParameterizedType objType, |
|
438 |
MXBeanMappingFactory factory) |
|
439 |
throws OpenDataException { |
|
2 | 440 |
|
441 |
final Type rawType = objType.getRawType(); |
|
442 |
||
443 |
if (rawType instanceof Class) { |
|
444 |
Class c = (Class<?>) rawType; |
|
445 |
if (c == List.class || c == Set.class || c == SortedSet.class) { |
|
446 |
Type[] actuals = objType.getActualTypeArguments(); |
|
447 |
assert(actuals.length == 1); |
|
448 |
if (c == SortedSet.class) |
|
449 |
mustBeComparable(c, actuals[0]); |
|
687 | 450 |
return makeArrayOrCollectionMapping(objType, actuals[0], factory); |
2 | 451 |
} else { |
452 |
boolean sortedMap = (c == SortedMap.class); |
|
453 |
if (c == Map.class || sortedMap) { |
|
454 |
Type[] actuals = objType.getActualTypeArguments(); |
|
455 |
assert(actuals.length == 2); |
|
456 |
if (sortedMap) |
|
457 |
mustBeComparable(c, actuals[0]); |
|
687 | 458 |
return makeTabularMapping(objType, sortedMap, |
459 |
actuals[0], actuals[1], factory); |
|
2 | 460 |
} |
461 |
} |
|
462 |
} |
|
463 |
throw new OpenDataException("Cannot convert type: " + objType); |
|
464 |
} |
|
465 |
||
687 | 466 |
private static MXBeanMapping makeMXBeanRefMapping(Type t) |
2 | 467 |
throws OpenDataException { |
687 | 468 |
return new MXBeanRefMapping(t); |
2 | 469 |
} |
470 |
||
687 | 471 |
private MXBeanMapping makeCompositeMapping(Class c, |
472 |
MXBeanMappingFactory factory) |
|
2 | 473 |
throws OpenDataException { |
474 |
||
475 |
// For historical reasons GcInfo implements CompositeData but we |
|
476 |
// shouldn't count its CompositeData.getCompositeType() field as |
|
477 |
// an item in the computed CompositeType. |
|
478 |
final boolean gcInfoHack = |
|
479 |
(c.getName().equals("com.sun.management.GcInfo") && |
|
480 |
c.getClassLoader() == null); |
|
481 |
||
482 |
final List<Method> methods = |
|
287 | 483 |
MBeanAnalyzer.eliminateCovariantMethods(Arrays.asList(c.getMethods())); |
2 | 484 |
final SortedMap<String,Method> getterMap = newSortedMap(); |
485 |
||
486 |
/* Select public methods that look like "T getX()" or "boolean |
|
487 |
isX()", where T is not void and X is not the empty |
|
488 |
string. Exclude "Class getClass()" inherited from Object. */ |
|
489 |
for (Method method : methods) { |
|
490 |
final String propertyName = propertyName(method); |
|
491 |
||
492 |
if (propertyName == null) |
|
493 |
continue; |
|
494 |
if (gcInfoHack && propertyName.equals("CompositeType")) |
|
495 |
continue; |
|
496 |
||
497 |
Method old = |
|
498 |
getterMap.put(decapitalize(propertyName), |
|
499 |
method); |
|
500 |
if (old != null) { |
|
501 |
final String msg = |
|
502 |
"Class " + c.getName() + " has method name clash: " + |
|
503 |
old.getName() + ", " + method.getName(); |
|
504 |
throw new OpenDataException(msg); |
|
505 |
} |
|
506 |
} |
|
507 |
||
508 |
final int nitems = getterMap.size(); |
|
509 |
||
510 |
if (nitems == 0) { |
|
511 |
throw new OpenDataException("Can't map " + c.getName() + |
|
512 |
" to an open data type"); |
|
513 |
} |
|
514 |
||
515 |
final Method[] getters = new Method[nitems]; |
|
516 |
final String[] itemNames = new String[nitems]; |
|
517 |
final OpenType[] openTypes = new OpenType[nitems]; |
|
518 |
int i = 0; |
|
519 |
for (Map.Entry<String,Method> entry : getterMap.entrySet()) { |
|
520 |
itemNames[i] = entry.getKey(); |
|
521 |
final Method getter = entry.getValue(); |
|
522 |
getters[i] = getter; |
|
523 |
final Type retType = getter.getGenericReturnType(); |
|
687 | 524 |
openTypes[i] = factory.mappingForType(retType, factory).getOpenType(); |
2 | 525 |
i++; |
526 |
} |
|
527 |
||
528 |
CompositeType compositeType = |
|
529 |
new CompositeType(c.getName(), |
|
530 |
c.getName(), |
|
531 |
itemNames, // field names |
|
532 |
itemNames, // field descriptions |
|
533 |
openTypes); |
|
534 |
||
687 | 535 |
return new CompositeMapping(c, |
536 |
compositeType, |
|
537 |
itemNames, |
|
538 |
getters, |
|
539 |
factory); |
|
2 | 540 |
} |
541 |
||
542 |
/* Converter for classes where the open data is identical to the |
|
543 |
original data. This is true for any of the SimpleType types, |
|
544 |
and for an any-dimension array of those. It is also true for |
|
687 | 545 |
primitive types as of JMX 1.3, since an int[] |
2 | 546 |
can be directly represented by an ArrayType, and an int needs no mapping |
547 |
because reflection takes care of it. */ |
|
687 | 548 |
private static final class IdentityMapping extends NonNullMXBeanMapping { |
549 |
IdentityMapping(Type targetType, OpenType openType) { |
|
550 |
super(targetType, openType); |
|
2 | 551 |
} |
552 |
||
553 |
boolean isIdentity() { |
|
554 |
return true; |
|
555 |
} |
|
556 |
||
687 | 557 |
@Override |
558 |
Object fromNonNullOpenValue(Object openValue) |
|
559 |
throws InvalidObjectException { |
|
560 |
return openValue; |
|
2 | 561 |
} |
562 |
||
687 | 563 |
@Override |
564 |
Object toNonNullOpenValue(Object javaValue) throws OpenDataException { |
|
565 |
return javaValue; |
|
2 | 566 |
} |
567 |
} |
|
568 |
||
687 | 569 |
private static final class EnumMapping<T extends Enum<T>> |
570 |
extends NonNullMXBeanMapping { |
|
2 | 571 |
|
687 | 572 |
EnumMapping(Class<T> enumClass) { |
573 |
super(enumClass, SimpleType.STRING); |
|
2 | 574 |
this.enumClass = enumClass; |
575 |
} |
|
576 |
||
687 | 577 |
@Override |
578 |
final Object toNonNullOpenValue(Object value) { |
|
2 | 579 |
return ((Enum) value).name(); |
580 |
} |
|
581 |
||
687 | 582 |
@Override |
583 |
final T fromNonNullOpenValue(Object value) |
|
2 | 584 |
throws InvalidObjectException { |
585 |
try { |
|
586 |
return Enum.valueOf(enumClass, (String) value); |
|
587 |
} catch (Exception e) { |
|
588 |
throw invalidObjectException("Cannot convert to enum: " + |
|
589 |
value, e); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
private final Class<T> enumClass; |
|
594 |
} |
|
595 |
||
687 | 596 |
private static final class ArrayMapping extends NonNullMXBeanMapping { |
597 |
ArrayMapping(Type targetType, |
|
598 |
ArrayType openArrayType, Class openArrayClass, |
|
599 |
MXBeanMapping elementMapping) { |
|
600 |
super(targetType, openArrayType); |
|
601 |
this.elementMapping = elementMapping; |
|
2 | 602 |
} |
603 |
||
687 | 604 |
@Override |
605 |
final Object toNonNullOpenValue(Object value) |
|
2 | 606 |
throws OpenDataException { |
607 |
Object[] valueArray = (Object[]) value; |
|
608 |
final int len = valueArray.length; |
|
609 |
final Object[] openArray = (Object[]) |
|
610 |
Array.newInstance(getOpenClass().getComponentType(), len); |
|
687 | 611 |
for (int i = 0; i < len; i++) |
612 |
openArray[i] = elementMapping.toOpenValue(valueArray[i]); |
|
2 | 613 |
return openArray; |
614 |
} |
|
615 |
||
687 | 616 |
@Override |
617 |
final Object fromNonNullOpenValue(Object openValue) |
|
2 | 618 |
throws InvalidObjectException { |
619 |
final Object[] openArray = (Object[]) openValue; |
|
687 | 620 |
final Type javaType = getJavaType(); |
2 | 621 |
final Object[] valueArray; |
622 |
final Type componentType; |
|
687 | 623 |
if (javaType instanceof GenericArrayType) { |
2 | 624 |
componentType = |
687 | 625 |
((GenericArrayType) javaType).getGenericComponentType(); |
626 |
} else if (javaType instanceof Class && |
|
627 |
((Class<?>) javaType).isArray()) { |
|
628 |
componentType = ((Class<?>) javaType).getComponentType(); |
|
2 | 629 |
} else { |
630 |
throw new IllegalArgumentException("Not an array: " + |
|
687 | 631 |
javaType); |
2 | 632 |
} |
633 |
valueArray = (Object[]) Array.newInstance((Class<?>) componentType, |
|
634 |
openArray.length); |
|
687 | 635 |
for (int i = 0; i < openArray.length; i++) |
636 |
valueArray[i] = elementMapping.fromOpenValue(openArray[i]); |
|
2 | 637 |
return valueArray; |
638 |
} |
|
639 |
||
687 | 640 |
public void checkReconstructible() throws InvalidObjectException { |
641 |
elementMapping.checkReconstructible(); |
|
2 | 642 |
} |
643 |
||
687 | 644 |
/** |
645 |
* DefaultMXBeanMappingFactory for the elements of this array. If this is an |
|
646 |
* array of arrays, the converter converts the second-level arrays, |
|
647 |
* not the deepest elements. |
|
648 |
*/ |
|
649 |
private final MXBeanMapping elementMapping; |
|
2 | 650 |
} |
651 |
||
687 | 652 |
private static final class CollectionMapping extends NonNullMXBeanMapping { |
653 |
CollectionMapping(Type targetType, |
|
654 |
ArrayType openArrayType, |
|
655 |
Class openArrayClass, |
|
656 |
MXBeanMapping elementMapping) { |
|
657 |
super(targetType, openArrayType); |
|
658 |
this.elementMapping = elementMapping; |
|
2 | 659 |
|
660 |
/* Determine the concrete class to be used when converting |
|
661 |
back to this Java type. We convert all Lists to ArrayList |
|
662 |
and all Sets to TreeSet. (TreeSet because it is a SortedSet, |
|
663 |
so works for both Set and SortedSet.) */ |
|
664 |
Type raw = ((ParameterizedType) targetType).getRawType(); |
|
665 |
Class c = (Class<?>) raw; |
|
666 |
if (c == List.class) |
|
667 |
collectionClass = ArrayList.class; |
|
668 |
else if (c == Set.class) |
|
669 |
collectionClass = HashSet.class; |
|
670 |
else if (c == SortedSet.class) |
|
671 |
collectionClass = TreeSet.class; |
|
672 |
else { // can't happen |
|
673 |
assert(false); |
|
674 |
collectionClass = null; |
|
675 |
} |
|
676 |
} |
|
677 |
||
687 | 678 |
@Override |
679 |
final Object toNonNullOpenValue(Object value) |
|
2 | 680 |
throws OpenDataException { |
681 |
final Collection valueCollection = (Collection) value; |
|
682 |
if (valueCollection instanceof SortedSet) { |
|
683 |
Comparator comparator = |
|
684 |
((SortedSet) valueCollection).comparator(); |
|
685 |
if (comparator != null) { |
|
686 |
final String msg = |
|
687 |
"Cannot convert SortedSet with non-null comparator: " + |
|
688 |
comparator; |
|
831
50f701930577
6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents:
687
diff
changeset
|
689 |
throw openDataException(msg, new IllegalArgumentException(msg)); |
2 | 690 |
} |
691 |
} |
|
692 |
final Object[] openArray = (Object[]) |
|
693 |
Array.newInstance(getOpenClass().getComponentType(), |
|
694 |
valueCollection.size()); |
|
695 |
int i = 0; |
|
696 |
for (Object o : valueCollection) |
|
687 | 697 |
openArray[i++] = elementMapping.toOpenValue(o); |
2 | 698 |
return openArray; |
699 |
} |
|
700 |
||
687 | 701 |
@Override |
702 |
final Object fromNonNullOpenValue(Object openValue) |
|
2 | 703 |
throws InvalidObjectException { |
704 |
final Object[] openArray = (Object[]) openValue; |
|
705 |
final Collection<Object> valueCollection; |
|
706 |
try { |
|
687 | 707 |
valueCollection = cast(collectionClass.newInstance()); |
2 | 708 |
} catch (Exception e) { |
709 |
throw invalidObjectException("Cannot create collection", e); |
|
710 |
} |
|
711 |
for (Object o : openArray) { |
|
687 | 712 |
Object value = elementMapping.fromOpenValue(o); |
2 | 713 |
if (!valueCollection.add(value)) { |
714 |
final String msg = |
|
715 |
"Could not add " + o + " to " + |
|
716 |
collectionClass.getName() + |
|
717 |
" (duplicate set element?)"; |
|
718 |
throw new InvalidObjectException(msg); |
|
719 |
} |
|
720 |
} |
|
721 |
return valueCollection; |
|
722 |
} |
|
723 |
||
687 | 724 |
public void checkReconstructible() throws InvalidObjectException { |
725 |
elementMapping.checkReconstructible(); |
|
2 | 726 |
} |
727 |
||
728 |
private final Class<? extends Collection> collectionClass; |
|
687 | 729 |
private final MXBeanMapping elementMapping; |
2 | 730 |
} |
731 |
||
687 | 732 |
private static final class MXBeanRefMapping extends NonNullMXBeanMapping { |
733 |
MXBeanRefMapping(Type intf) { |
|
734 |
super(intf, SimpleType.OBJECTNAME); |
|
2 | 735 |
} |
736 |
||
687 | 737 |
@Override |
738 |
final Object toNonNullOpenValue(Object javaValue) |
|
2 | 739 |
throws OpenDataException { |
687 | 740 |
MXBeanLookup lookup = lookupNotNull(OpenDataException.class); |
741 |
ObjectName name = lookup.mxbeanToObjectName(javaValue); |
|
2 | 742 |
if (name == null) |
687 | 743 |
throw new OpenDataException("No name for object: " + javaValue); |
2 | 744 |
return name; |
745 |
} |
|
746 |
||
687 | 747 |
@Override |
748 |
final Object fromNonNullOpenValue(Object openValue) |
|
2 | 749 |
throws InvalidObjectException { |
687 | 750 |
MXBeanLookup lookup = lookupNotNull(InvalidObjectException.class); |
751 |
ObjectName name = (ObjectName) openValue; |
|
2 | 752 |
Object mxbean = |
687 | 753 |
lookup.objectNameToMXBean(name, (Class<?>) getJavaType()); |
2 | 754 |
if (mxbean == null) { |
755 |
final String msg = |
|
756 |
"No MXBean for name: " + name; |
|
757 |
throw new InvalidObjectException(msg); |
|
758 |
} |
|
759 |
return mxbean; |
|
760 |
} |
|
761 |
||
687 | 762 |
private <T extends Exception> MXBeanLookup |
763 |
lookupNotNull(Class<T> excClass) |
|
2 | 764 |
throws T { |
687 | 765 |
MXBeanLookup lookup = MXBeanLookup.getLookup(); |
2 | 766 |
if (lookup == null) { |
767 |
final String msg = |
|
768 |
"Cannot convert MXBean interface in this context"; |
|
769 |
T exc; |
|
770 |
try { |
|
771 |
Constructor<T> con = excClass.getConstructor(String.class); |
|
772 |
exc = con.newInstance(msg); |
|
773 |
} catch (Exception e) { |
|
774 |
throw new RuntimeException(e); |
|
775 |
} |
|
776 |
throw exc; |
|
777 |
} |
|
687 | 778 |
return lookup; |
2 | 779 |
} |
780 |
} |
|
781 |
||
687 | 782 |
private static final class TabularMapping extends NonNullMXBeanMapping { |
783 |
TabularMapping(Type targetType, |
|
784 |
boolean sortedMap, |
|
785 |
TabularType tabularType, |
|
786 |
MXBeanMapping keyConverter, |
|
787 |
MXBeanMapping valueConverter) { |
|
788 |
super(targetType, tabularType); |
|
2 | 789 |
this.sortedMap = sortedMap; |
687 | 790 |
this.keyMapping = keyConverter; |
791 |
this.valueMapping = valueConverter; |
|
2 | 792 |
} |
793 |
||
687 | 794 |
@Override |
795 |
final Object toNonNullOpenValue(Object value) throws OpenDataException { |
|
796 |
final Map<Object, Object> valueMap = cast(value); |
|
2 | 797 |
if (valueMap instanceof SortedMap) { |
798 |
Comparator comparator = ((SortedMap) valueMap).comparator(); |
|
799 |
if (comparator != null) { |
|
800 |
final String msg = |
|
801 |
"Cannot convert SortedMap with non-null comparator: " + |
|
802 |
comparator; |
|
831
50f701930577
6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
emcmanus
parents:
687
diff
changeset
|
803 |
throw openDataException(msg, new IllegalArgumentException(msg)); |
2 | 804 |
} |
805 |
} |
|
806 |
final TabularType tabularType = (TabularType) getOpenType(); |
|
807 |
final TabularData table = new TabularDataSupport(tabularType); |
|
808 |
final CompositeType rowType = tabularType.getRowType(); |
|
809 |
for (Map.Entry entry : valueMap.entrySet()) { |
|
687 | 810 |
final Object openKey = keyMapping.toOpenValue(entry.getKey()); |
811 |
final Object openValue = valueMapping.toOpenValue(entry.getValue()); |
|
2 | 812 |
final CompositeData row; |
813 |
row = |
|
814 |
new CompositeDataSupport(rowType, keyValueArray, |
|
815 |
new Object[] {openKey, |
|
816 |
openValue}); |
|
817 |
table.put(row); |
|
818 |
} |
|
819 |
return table; |
|
820 |
} |
|
821 |
||
687 | 822 |
@Override |
823 |
final Object fromNonNullOpenValue(Object openValue) |
|
2 | 824 |
throws InvalidObjectException { |
825 |
final TabularData table = (TabularData) openValue; |
|
687 | 826 |
final Collection<CompositeData> rows = cast(table.values()); |
2 | 827 |
final Map<Object, Object> valueMap = |
1018
9f07e65e9653
6334663: TabularDataSupport should be able to return values in the insertion order
emcmanus
parents:
832
diff
changeset
|
828 |
sortedMap ? newSortedMap() : newInsertionOrderMap(); |
2 | 829 |
for (CompositeData row : rows) { |
830 |
final Object key = |
|
687 | 831 |
keyMapping.fromOpenValue(row.get("key")); |
2 | 832 |
final Object value = |
687 | 833 |
valueMapping.fromOpenValue(row.get("value")); |
2 | 834 |
if (valueMap.put(key, value) != null) { |
835 |
final String msg = |
|
836 |
"Duplicate entry in TabularData: key=" + key; |
|
837 |
throw new InvalidObjectException(msg); |
|
838 |
} |
|
839 |
} |
|
840 |
return valueMap; |
|
841 |
} |
|
842 |
||
687 | 843 |
@Override |
844 |
public void checkReconstructible() throws InvalidObjectException { |
|
845 |
keyMapping.checkReconstructible(); |
|
846 |
valueMapping.checkReconstructible(); |
|
2 | 847 |
} |
848 |
||
849 |
private final boolean sortedMap; |
|
687 | 850 |
private final MXBeanMapping keyMapping; |
851 |
private final MXBeanMapping valueMapping; |
|
2 | 852 |
} |
853 |
||
687 | 854 |
private final class CompositeMapping extends NonNullMXBeanMapping { |
855 |
CompositeMapping(Class targetClass, |
|
856 |
CompositeType compositeType, |
|
857 |
String[] itemNames, |
|
858 |
Method[] getters, |
|
859 |
MXBeanMappingFactory factory) throws OpenDataException { |
|
860 |
super(targetClass, compositeType); |
|
2 | 861 |
|
862 |
assert(itemNames.length == getters.length); |
|
863 |
||
864 |
this.itemNames = itemNames; |
|
865 |
this.getters = getters; |
|
687 | 866 |
this.getterMappings = new MXBeanMapping[getters.length]; |
2 | 867 |
for (int i = 0; i < getters.length; i++) { |
868 |
Type retType = getters[i].getGenericReturnType(); |
|
687 | 869 |
getterMappings[i] = factory.mappingForType(retType, factory); |
2 | 870 |
} |
871 |
} |
|
872 |
||
687 | 873 |
@Override |
874 |
final Object toNonNullOpenValue(Object value) |
|
2 | 875 |
throws OpenDataException { |
876 |
CompositeType ct = (CompositeType) getOpenType(); |
|
877 |
if (value instanceof CompositeDataView) |
|
878 |
return ((CompositeDataView) value).toCompositeData(ct); |
|
879 |
if (value == null) |
|
880 |
return null; |
|
881 |
||
882 |
Object[] values = new Object[getters.length]; |
|
883 |
for (int i = 0; i < getters.length; i++) { |
|
884 |
try { |
|
885 |
Object got = getters[i].invoke(value, (Object[]) null); |
|
687 | 886 |
values[i] = getterMappings[i].toOpenValue(got); |
2 | 887 |
} catch (Exception e) { |
888 |
throw openDataException("Error calling getter for " + |
|
889 |
itemNames[i] + ": " + e, e); |
|
890 |
} |
|
891 |
} |
|
892 |
return new CompositeDataSupport(ct, itemNames, values); |
|
893 |
} |
|
894 |
||
895 |
/** Determine how to convert back from the CompositeData into |
|
896 |
the original Java type. For a type that is not reconstructible, |
|
897 |
this method will fail every time, and will throw the right |
|
898 |
exception. */ |
|
899 |
private synchronized void makeCompositeBuilder() |
|
900 |
throws InvalidObjectException { |
|
901 |
if (compositeBuilder != null) |
|
902 |
return; |
|
903 |
||
687 | 904 |
Class targetClass = (Class<?>) getJavaType(); |
2 | 905 |
/* In this 2D array, each subarray is a set of builders where |
906 |
there is no point in consulting the ones after the first if |
|
907 |
the first refuses. */ |
|
908 |
CompositeBuilder[][] builders = { |
|
909 |
{ |
|
910 |
new CompositeBuilderViaFrom(targetClass, itemNames), |
|
911 |
}, |
|
912 |
{ |
|
913 |
new CompositeBuilderViaConstructor(targetClass, itemNames), |
|
914 |
}, |
|
915 |
{ |
|
916 |
new CompositeBuilderCheckGetters(targetClass, itemNames, |
|
687 | 917 |
getterMappings), |
2 | 918 |
new CompositeBuilderViaSetters(targetClass, itemNames), |
919 |
new CompositeBuilderViaProxy(targetClass, itemNames), |
|
920 |
}, |
|
921 |
}; |
|
922 |
CompositeBuilder foundBuilder = null; |
|
923 |
/* We try to make a meaningful exception message by |
|
924 |
concatenating each Builder's explanation of why it |
|
925 |
isn't applicable. */ |
|
926 |
final StringBuilder whyNots = new StringBuilder(); |
|
927 |
find: |
|
928 |
for (CompositeBuilder[] relatedBuilders : builders) { |
|
929 |
for (int i = 0; i < relatedBuilders.length; i++) { |
|
930 |
CompositeBuilder builder = relatedBuilders[i]; |
|
931 |
String whyNot = builder.applicable(getters); |
|
932 |
if (whyNot == null) { |
|
933 |
foundBuilder = builder; |
|
934 |
break find; |
|
935 |
} |
|
936 |
if (whyNot.length() > 0) { |
|
937 |
if (whyNots.length() > 0) |
|
938 |
whyNots.append("; "); |
|
939 |
whyNots.append(whyNot); |
|
940 |
if (i == 0) |
|
941 |
break; // skip other builders in this group |
|
942 |
} |
|
943 |
} |
|
944 |
} |
|
945 |
if (foundBuilder == null) { |
|
946 |
final String msg = |
|
947 |
"Do not know how to make a " + targetClass.getName() + |
|
948 |
" from a CompositeData: " + whyNots; |
|
949 |
throw new InvalidObjectException(msg); |
|
950 |
} |
|
951 |
compositeBuilder = foundBuilder; |
|
952 |
} |
|
953 |
||
687 | 954 |
@Override |
955 |
public void checkReconstructible() throws InvalidObjectException { |
|
2 | 956 |
makeCompositeBuilder(); |
957 |
} |
|
958 |
||
687 | 959 |
@Override |
960 |
final Object fromNonNullOpenValue(Object value) |
|
2 | 961 |
throws InvalidObjectException { |
962 |
makeCompositeBuilder(); |
|
687 | 963 |
return compositeBuilder.fromCompositeData((CompositeData) value, |
2 | 964 |
itemNames, |
687 | 965 |
getterMappings); |
2 | 966 |
} |
967 |
||
968 |
private final String[] itemNames; |
|
969 |
private final Method[] getters; |
|
687 | 970 |
private final MXBeanMapping[] getterMappings; |
2 | 971 |
private CompositeBuilder compositeBuilder; |
972 |
} |
|
973 |
||
974 |
/** Converts from a CompositeData to an instance of the targetClass. */ |
|
975 |
private static abstract class CompositeBuilder { |
|
976 |
CompositeBuilder(Class targetClass, String[] itemNames) { |
|
977 |
this.targetClass = targetClass; |
|
978 |
this.itemNames = itemNames; |
|
979 |
} |
|
980 |
||
981 |
Class<?> getTargetClass() { |
|
982 |
return targetClass; |
|
983 |
} |
|
984 |
||
985 |
String[] getItemNames() { |
|
986 |
return itemNames; |
|
987 |
} |
|
988 |
||
989 |
/** If the subclass is appropriate for targetClass, then the |
|
990 |
method returns null. If the subclass is not appropriate, |
|
991 |
then the method returns an explanation of why not. If the |
|
992 |
subclass should be appropriate but there is a problem, |
|
993 |
then the method throws InvalidObjectException. */ |
|
994 |
abstract String applicable(Method[] getters) |
|
995 |
throws InvalidObjectException; |
|
996 |
||
687 | 997 |
abstract Object fromCompositeData(CompositeData cd, |
2 | 998 |
String[] itemNames, |
687 | 999 |
MXBeanMapping[] converters) |
2 | 1000 |
throws InvalidObjectException; |
1001 |
||
1002 |
private final Class<?> targetClass; |
|
1003 |
private final String[] itemNames; |
|
1004 |
} |
|
1005 |
||
1006 |
/** Builder for when the target class has a method "public static |
|
1007 |
from(CompositeData)". */ |
|
1008 |
private static final class CompositeBuilderViaFrom |
|
1009 |
extends CompositeBuilder { |
|
1010 |
||
1011 |
CompositeBuilderViaFrom(Class targetClass, String[] itemNames) { |
|
1012 |
super(targetClass, itemNames); |
|
1013 |
} |
|
1014 |
||
1015 |
String applicable(Method[] getters) throws InvalidObjectException { |
|
1016 |
// See if it has a method "T from(CompositeData)" |
|
1017 |
// as is conventional for a CompositeDataView |
|
1018 |
Class<?> targetClass = getTargetClass(); |
|
1019 |
try { |
|
1020 |
Method fromMethod = |
|
1021 |
targetClass.getMethod("from", |
|
1022 |
new Class[] {CompositeData.class}); |
|
1023 |
||
1024 |
if (!Modifier.isStatic(fromMethod.getModifiers())) { |
|
1025 |
final String msg = |
|
1026 |
"Method from(CompositeData) is not static"; |
|
1027 |
throw new InvalidObjectException(msg); |
|
1028 |
} |
|
1029 |
||
1030 |
if (fromMethod.getReturnType() != getTargetClass()) { |
|
1031 |
final String msg = |
|
1032 |
"Method from(CompositeData) returns " + |
|
1033 |
fromMethod.getReturnType().getName() + |
|
1034 |
" not " + targetClass.getName(); |
|
1035 |
throw new InvalidObjectException(msg); |
|
1036 |
} |
|
1037 |
||
1038 |
this.fromMethod = fromMethod; |
|
1039 |
return null; // success! |
|
1040 |
} catch (InvalidObjectException e) { |
|
1041 |
throw e; |
|
1042 |
} catch (Exception e) { |
|
1043 |
// OK: it doesn't have the method |
|
1044 |
return "no method from(CompositeData)"; |
|
1045 |
} |
|
1046 |
} |
|
1047 |
||
687 | 1048 |
final Object fromCompositeData(CompositeData cd, |
1049 |
String[] itemNames, |
|
1050 |
MXBeanMapping[] converters) |
|
2 | 1051 |
throws InvalidObjectException { |
1052 |
try { |
|
1053 |
return fromMethod.invoke(null, cd); |
|
1054 |
} catch (Exception e) { |
|
1055 |
final String msg = "Failed to invoke from(CompositeData)"; |
|
1056 |
throw invalidObjectException(msg, e); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
private Method fromMethod; |
|
1061 |
} |
|
1062 |
||
1063 |
/** This builder never actually returns success. It simply serves |
|
1064 |
to check whether the other builders in the same group have any |
|
1065 |
chance of success. If any getter in the targetClass returns |
|
1066 |
a type that we don't know how to reconstruct, then we will |
|
1067 |
not be able to make a builder, and there is no point in repeating |
|
1068 |
the error about the problematic getter as many times as there are |
|
1069 |
candidate builders. Instead, the "applicable" method will return |
|
1070 |
an explanatory string, and the other builders will be skipped. |
|
1071 |
If all the getters are OK, then the "applicable" method will return |
|
1072 |
an empty string and the other builders will be tried. */ |
|
1073 |
private static class CompositeBuilderCheckGetters extends CompositeBuilder { |
|
1074 |
CompositeBuilderCheckGetters(Class targetClass, String[] itemNames, |
|
687 | 1075 |
MXBeanMapping[] getterConverters) { |
2 | 1076 |
super(targetClass, itemNames); |
1077 |
this.getterConverters = getterConverters; |
|
1078 |
} |
|
1079 |
||
1080 |
String applicable(Method[] getters) { |
|
1081 |
for (int i = 0; i < getters.length; i++) { |
|
1082 |
try { |
|
1083 |
getterConverters[i].checkReconstructible(); |
|
1084 |
} catch (InvalidObjectException e) { |
|
1085 |
return "method " + getters[i].getName() + " returns type " + |
|
1086 |
"that cannot be mapped back from OpenData"; |
|
1087 |
} |
|
1088 |
} |
|
1089 |
return ""; |
|
1090 |
} |
|
1091 |
||
687 | 1092 |
final Object fromCompositeData(CompositeData cd, |
2 | 1093 |
String[] itemNames, |
687 | 1094 |
MXBeanMapping[] converters) { |
2 | 1095 |
throw new Error(); |
1096 |
} |
|
1097 |
||
687 | 1098 |
private final MXBeanMapping[] getterConverters; |
2 | 1099 |
} |
1100 |
||
1101 |
/** Builder for when the target class has a setter for every getter. */ |
|
1102 |
private static class CompositeBuilderViaSetters extends CompositeBuilder { |
|
1103 |
||
687 | 1104 |
CompositeBuilderViaSetters(Class<?> targetClass, String[] itemNames) { |
2 | 1105 |
super(targetClass, itemNames); |
1106 |
} |
|
1107 |
||
1108 |
String applicable(Method[] getters) { |
|
1109 |
try { |
|
687 | 1110 |
Constructor<?> c = getTargetClass().getConstructor(); |
2 | 1111 |
} catch (Exception e) { |
1112 |
return "does not have a public no-arg constructor"; |
|
1113 |
} |
|
1114 |
||
1115 |
Method[] setters = new Method[getters.length]; |
|
1116 |
for (int i = 0; i < getters.length; i++) { |
|
1117 |
Method getter = getters[i]; |
|
1118 |
Class returnType = getter.getReturnType(); |
|
1119 |
String name = propertyName(getter); |
|
1120 |
String setterName = "set" + name; |
|
1121 |
Method setter; |
|
1122 |
try { |
|
1123 |
setter = getTargetClass().getMethod(setterName, returnType); |
|
1124 |
if (setter.getReturnType() != void.class) |
|
1125 |
throw new Exception(); |
|
1126 |
} catch (Exception e) { |
|
1127 |
return "not all getters have corresponding setters " + |
|
1128 |
"(" + getter + ")"; |
|
1129 |
} |
|
1130 |
setters[i] = setter; |
|
1131 |
} |
|
1132 |
this.setters = setters; |
|
1133 |
return null; |
|
1134 |
} |
|
1135 |
||
687 | 1136 |
Object fromCompositeData(CompositeData cd, |
2 | 1137 |
String[] itemNames, |
687 | 1138 |
MXBeanMapping[] converters) |
2 | 1139 |
throws InvalidObjectException { |
1140 |
Object o; |
|
1141 |
try { |
|
1142 |
o = getTargetClass().newInstance(); |
|
1143 |
for (int i = 0; i < itemNames.length; i++) { |
|
1144 |
if (cd.containsKey(itemNames[i])) { |
|
1145 |
Object openItem = cd.get(itemNames[i]); |
|
1146 |
Object javaItem = |
|
687 | 1147 |
converters[i].fromOpenValue(openItem); |
2 | 1148 |
setters[i].invoke(o, javaItem); |
1149 |
} |
|
1150 |
} |
|
1151 |
} catch (Exception e) { |
|
1152 |
throw invalidObjectException(e); |
|
1153 |
} |
|
1154 |
return o; |
|
1155 |
} |
|
1156 |
||
1157 |
private Method[] setters; |
|
1158 |
} |
|
1159 |
||
1160 |
/** Builder for when the target class has a constructor that is |
|
1161 |
annotated with @ConstructorProperties so we can see the correspondence |
|
1162 |
to getters. */ |
|
1163 |
private static final class CompositeBuilderViaConstructor |
|
1164 |
extends CompositeBuilder { |
|
1165 |
||
1166 |
CompositeBuilderViaConstructor(Class targetClass, String[] itemNames) { |
|
1167 |
super(targetClass, itemNames); |
|
1168 |
} |
|
1169 |
||
1170 |
String applicable(Method[] getters) throws InvalidObjectException { |
|
1171 |
||
1172 |
final Class<ConstructorProperties> propertyNamesClass = ConstructorProperties.class; |
|
1173 |
||
1174 |
Class targetClass = getTargetClass(); |
|
687 | 1175 |
Constructor[] constrs = targetClass.getConstructors(); |
2 | 1176 |
|
1177 |
// Applicable if and only if there are any annotated constructors |
|
687 | 1178 |
List<Constructor> annotatedConstrList = newList(); |
268
aa06754a95de
6643627: JMX source code includes incorrect Java code
emcmanus
parents:
2
diff
changeset
|
1179 |
for (Constructor<?> constr : constrs) { |
2 | 1180 |
if (Modifier.isPublic(constr.getModifiers()) |
1181 |
&& constr.getAnnotation(propertyNamesClass) != null) |
|
1182 |
annotatedConstrList.add(constr); |
|
1183 |
} |
|
1184 |
||
1185 |
if (annotatedConstrList.isEmpty()) |
|
1186 |
return "no constructor has @ConstructorProperties annotation"; |
|
1187 |
||
1188 |
annotatedConstructors = newList(); |
|
1189 |
||
1190 |
// Now check that all the annotated constructors are valid |
|
1191 |
// and throw an exception if not. |
|
1192 |
||
1193 |
// First link the itemNames to their getter indexes. |
|
1194 |
Map<String, Integer> getterMap = newMap(); |
|
1195 |
String[] itemNames = getItemNames(); |
|
1196 |
for (int i = 0; i < itemNames.length; i++) |
|
1197 |
getterMap.put(itemNames[i], i); |
|
1198 |
||
1199 |
// Run through the constructors making the checks in the spec. |
|
1200 |
// For each constructor, remember the correspondence between its |
|
1201 |
// parameters and the items. The int[] for a constructor says |
|
1202 |
// what parameter index should get what item. For example, |
|
1203 |
// if element 0 is 2 then that means that item 0 in the |
|
1204 |
// CompositeData goes to parameter 2 of the constructor. If an |
|
1205 |
// element is -1, that item isn't given to the constructor. |
|
1206 |
// Also remember the set of properties in that constructor |
|
1207 |
// so we can test unambiguity. |
|
1208 |
Set<BitSet> getterIndexSets = newSet(); |
|
687 | 1209 |
for (Constructor constr : annotatedConstrList) { |
2 | 1210 |
String[] propertyNames = |
1211 |
constr.getAnnotation(propertyNamesClass).value(); |
|
1212 |
||
1213 |
Type[] paramTypes = constr.getGenericParameterTypes(); |
|
1214 |
if (paramTypes.length != propertyNames.length) { |
|
1215 |
final String msg = |
|
1216 |
"Number of constructor params does not match " + |
|
1217 |
"@ConstructorProperties annotation: " + constr; |
|
1218 |
throw new InvalidObjectException(msg); |
|
1219 |
} |
|
1220 |
||
1221 |
int[] paramIndexes = new int[getters.length]; |
|
1222 |
for (int i = 0; i < getters.length; i++) |
|
1223 |
paramIndexes[i] = -1; |
|
1224 |
BitSet present = new BitSet(); |
|
1225 |
||
1226 |
for (int i = 0; i < propertyNames.length; i++) { |
|
1227 |
String propertyName = propertyNames[i]; |
|
1228 |
if (!getterMap.containsKey(propertyName)) { |
|
1229 |
final String msg = |
|
1230 |
"@ConstructorProperties includes name " + propertyName + |
|
1231 |
" which does not correspond to a property: " + |
|
1232 |
constr; |
|
1233 |
throw new InvalidObjectException(msg); |
|
1234 |
} |
|
1235 |
int getterIndex = getterMap.get(propertyName); |
|
1236 |
paramIndexes[getterIndex] = i; |
|
1237 |
if (present.get(getterIndex)) { |
|
1238 |
final String msg = |
|
1239 |
"@ConstructorProperties contains property " + |
|
1240 |
propertyName + " more than once: " + constr; |
|
1241 |
throw new InvalidObjectException(msg); |
|
1242 |
} |
|
1243 |
present.set(getterIndex); |
|
1244 |
Method getter = getters[getterIndex]; |
|
1245 |
Type propertyType = getter.getGenericReturnType(); |
|
1246 |
if (!propertyType.equals(paramTypes[i])) { |
|
1247 |
final String msg = |
|
1248 |
"@ConstructorProperties gives property " + propertyName + |
|
1249 |
" of type " + propertyType + " for parameter " + |
|
1250 |
" of type " + paramTypes[i] + ": " + constr; |
|
1251 |
throw new InvalidObjectException(msg); |
|
1252 |
} |
|
1253 |
} |
|
1254 |
||
1255 |
if (!getterIndexSets.add(present)) { |
|
1256 |
final String msg = |
|
1257 |
"More than one constructor has a @ConstructorProperties " + |
|
1258 |
"annotation with this set of names: " + |
|
1259 |
Arrays.toString(propertyNames); |
|
1260 |
throw new InvalidObjectException(msg); |
|
1261 |
} |
|
1262 |
||
1263 |
Constr c = new Constr(constr, paramIndexes, present); |
|
1264 |
annotatedConstructors.add(c); |
|
1265 |
} |
|
1266 |
||
1267 |
/* Check that no possible set of items could lead to an ambiguous |
|
1268 |
* choice of constructor (spec requires this check). For any |
|
1269 |
* pair of constructors, their union would be the minimal |
|
1270 |
* ambiguous set. If this set itself corresponds to a constructor, |
|
1271 |
* there is no ambiguity for that pair. In the usual case, one |
|
1272 |
* of the constructors is a superset of the other so the union is |
|
1273 |
* just the bigger constuctor. |
|
1274 |
* |
|
1275 |
* The algorithm here is quadratic in the number of constructors |
|
1276 |
* with a @ConstructorProperties annotation. Typically this corresponds |
|
1277 |
* to the number of versions of the class there have been. Ten |
|
1278 |
* would already be a large number, so although it's probably |
|
1279 |
* possible to have an O(n lg n) algorithm it wouldn't be |
|
1280 |
* worth the complexity. |
|
1281 |
*/ |
|
1282 |
for (BitSet a : getterIndexSets) { |
|
1283 |
boolean seen = false; |
|
1284 |
for (BitSet b : getterIndexSets) { |
|
1285 |
if (a == b) |
|
1286 |
seen = true; |
|
1287 |
else if (seen) { |
|
1288 |
BitSet u = new BitSet(); |
|
1289 |
u.or(a); u.or(b); |
|
1290 |
if (!getterIndexSets.contains(u)) { |
|
1291 |
Set<String> names = new TreeSet<String>(); |
|
1292 |
for (int i = u.nextSetBit(0); i >= 0; |
|
1293 |
i = u.nextSetBit(i+1)) |
|
1294 |
names.add(itemNames[i]); |
|
1295 |
final String msg = |
|
1296 |
"Constructors with @ConstructorProperties annotation " + |
|
1297 |
" would be ambiguous for these items: " + |
|
1298 |
names; |
|
1299 |
throw new InvalidObjectException(msg); |
|
1300 |
} |
|
1301 |
} |
|
1302 |
} |
|
1303 |
} |
|
1304 |
||
1305 |
return null; // success! |
|
1306 |
} |
|
1307 |
||
687 | 1308 |
final Object fromCompositeData(CompositeData cd, |
1309 |
String[] itemNames, |
|
1310 |
MXBeanMapping[] mappings) |
|
2 | 1311 |
throws InvalidObjectException { |
1312 |
// The CompositeData might come from an earlier version where |
|
1313 |
// not all the items were present. We look for a constructor |
|
1314 |
// that accepts just the items that are present. Because of |
|
1315 |
// the ambiguity check in applicable(), we know there must be |
|
1316 |
// at most one maximally applicable constructor. |
|
1317 |
CompositeType ct = cd.getCompositeType(); |
|
1318 |
BitSet present = new BitSet(); |
|
1319 |
for (int i = 0; i < itemNames.length; i++) { |
|
1320 |
if (ct.getType(itemNames[i]) != null) |
|
1321 |
present.set(i); |
|
1322 |
} |
|
1323 |
||
1324 |
Constr max = null; |
|
1325 |
for (Constr constr : annotatedConstructors) { |
|
1326 |
if (subset(constr.presentParams, present) && |
|
1327 |
(max == null || |
|
1328 |
subset(max.presentParams, constr.presentParams))) |
|
1329 |
max = constr; |
|
1330 |
} |
|
1331 |
||
1332 |
if (max == null) { |
|
1333 |
final String msg = |
|
1334 |
"No constructor has a @ConstructorProperties for this set of " + |
|
1335 |
"items: " + ct.keySet(); |
|
1336 |
throw new InvalidObjectException(msg); |
|
1337 |
} |
|
1338 |
||
1339 |
Object[] params = new Object[max.presentParams.cardinality()]; |
|
1340 |
for (int i = 0; i < itemNames.length; i++) { |
|
1341 |
if (!max.presentParams.get(i)) |
|
1342 |
continue; |
|
1343 |
Object openItem = cd.get(itemNames[i]); |
|
687 | 1344 |
Object javaItem = mappings[i].fromOpenValue(openItem); |
2 | 1345 |
int index = max.paramIndexes[i]; |
1346 |
if (index >= 0) |
|
1347 |
params[index] = javaItem; |
|
1348 |
} |
|
1349 |
||
1350 |
try { |
|
1351 |
return max.constructor.newInstance(params); |
|
1352 |
} catch (Exception e) { |
|
1353 |
final String msg = |
|
1354 |
"Exception constructing " + getTargetClass().getName(); |
|
1355 |
throw invalidObjectException(msg, e); |
|
1356 |
} |
|
1357 |
} |
|
1358 |
||
1359 |
private static boolean subset(BitSet sub, BitSet sup) { |
|
1360 |
BitSet subcopy = (BitSet) sub.clone(); |
|
1361 |
subcopy.andNot(sup); |
|
1362 |
return subcopy.isEmpty(); |
|
1363 |
} |
|
1364 |
||
1365 |
private static class Constr { |
|
687 | 1366 |
final Constructor constructor; |
2 | 1367 |
final int[] paramIndexes; |
1368 |
final BitSet presentParams; |
|
687 | 1369 |
Constr(Constructor constructor, int[] paramIndexes, |
2 | 1370 |
BitSet presentParams) { |
1371 |
this.constructor = constructor; |
|
1372 |
this.paramIndexes = paramIndexes; |
|
1373 |
this.presentParams = presentParams; |
|
1374 |
} |
|
1375 |
} |
|
1376 |
||
1377 |
private List<Constr> annotatedConstructors; |
|
1378 |
} |
|
1379 |
||
1380 |
/** Builder for when the target class is an interface and contains |
|
1381 |
no methods other than getters. Then we can make an instance |
|
1382 |
using a dynamic proxy that forwards the getters to the source |
|
1383 |
CompositeData. */ |
|
1384 |
private static final class CompositeBuilderViaProxy |
|
1385 |
extends CompositeBuilder { |
|
1386 |
||
1387 |
CompositeBuilderViaProxy(Class targetClass, String[] itemNames) { |
|
1388 |
super(targetClass, itemNames); |
|
1389 |
} |
|
1390 |
||
1391 |
String applicable(Method[] getters) { |
|
1392 |
Class targetClass = getTargetClass(); |
|
1393 |
if (!targetClass.isInterface()) |
|
1394 |
return "not an interface"; |
|
1395 |
Set<Method> methods = |
|
1396 |
newSet(Arrays.asList(targetClass.getMethods())); |
|
1397 |
methods.removeAll(Arrays.asList(getters)); |
|
1398 |
/* If the interface has any methods left over, they better be |
|
1399 |
* public methods that are already present in java.lang.Object. |
|
1400 |
*/ |
|
1401 |
String bad = null; |
|
1402 |
for (Method m : methods) { |
|
1403 |
String mname = m.getName(); |
|
1404 |
Class[] mparams = m.getParameterTypes(); |
|
1405 |
try { |
|
1406 |
Method om = Object.class.getMethod(mname, mparams); |
|
1407 |
if (!Modifier.isPublic(om.getModifiers())) |
|
1408 |
bad = mname; |
|
1409 |
} catch (NoSuchMethodException e) { |
|
1410 |
bad = mname; |
|
1411 |
} |
|
1412 |
/* We don't catch SecurityException since it shouldn't |
|
1413 |
* happen for a method in Object and if it does we would |
|
1414 |
* like to know about it rather than mysteriously complaining. |
|
1415 |
*/ |
|
1416 |
} |
|
1417 |
if (bad != null) |
|
1418 |
return "contains methods other than getters (" + bad + ")"; |
|
1419 |
return null; // success! |
|
1420 |
} |
|
1421 |
||
687 | 1422 |
final Object fromCompositeData(CompositeData cd, |
1423 |
String[] itemNames, |
|
1424 |
MXBeanMapping[] converters) { |
|
2 | 1425 |
final Class targetClass = getTargetClass(); |
1426 |
return |
|
1427 |
Proxy.newProxyInstance(targetClass.getClassLoader(), |
|
1428 |
new Class[] {targetClass}, |
|
1429 |
new CompositeDataInvocationHandler(cd)); |
|
1430 |
} |
|
1431 |
} |
|
1432 |
||
1433 |
static InvalidObjectException invalidObjectException(String msg, |
|
1434 |
Throwable cause) { |
|
1435 |
return EnvHelp.initCause(new InvalidObjectException(msg), cause); |
|
1436 |
} |
|
1437 |
||
1438 |
static InvalidObjectException invalidObjectException(Throwable cause) { |
|
1439 |
return invalidObjectException(cause.getMessage(), cause); |
|
1440 |
} |
|
1441 |
||
1442 |
static OpenDataException openDataException(String msg, Throwable cause) { |
|
1443 |
return EnvHelp.initCause(new OpenDataException(msg), cause); |
|
1444 |
} |
|
1445 |
||
1446 |
static OpenDataException openDataException(Throwable cause) { |
|
1447 |
return openDataException(cause.getMessage(), cause); |
|
1448 |
} |
|
1449 |
||
1450 |
static void mustBeComparable(Class collection, Type element) |
|
1451 |
throws OpenDataException { |
|
1452 |
if (!(element instanceof Class) |
|
1453 |
|| !Comparable.class.isAssignableFrom((Class<?>) element)) { |
|
1454 |
final String msg = |
|
1455 |
"Parameter class " + element + " of " + |
|
1456 |
collection.getName() + " does not implement " + |
|
1457 |
Comparable.class.getName(); |
|
1458 |
throw new OpenDataException(msg); |
|
1459 |
} |
|
1460 |
} |
|
1461 |
||
1462 |
/** |
|
1463 |
* Utility method to take a string and convert it to normal Java variable |
|
1464 |
* name capitalization. This normally means converting the first |
|
1465 |
* character from upper case to lower case, but in the (unusual) special |
|
1466 |
* case when there is more than one character and both the first and |
|
1467 |
* second characters are upper case, we leave it alone. |
|
1468 |
* <p> |
|
1469 |
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays |
|
1470 |
* as "URL". |
|
1471 |
* |
|
1472 |
* @param name The string to be decapitalized. |
|
1473 |
* @return The decapitalized version of the string. |
|
1474 |
*/ |
|
1475 |
public static String decapitalize(String name) { |
|
1476 |
if (name == null || name.length() == 0) { |
|
1477 |
return name; |
|
1478 |
} |
|
1479 |
int offset1 = Character.offsetByCodePoints(name, 0, 1); |
|
1480 |
// Should be name.offsetByCodePoints but 6242664 makes this fail |
|
1481 |
if (offset1 < name.length() && |
|
1482 |
Character.isUpperCase(name.codePointAt(offset1))) |
|
1483 |
return name; |
|
1484 |
return name.substring(0, offset1).toLowerCase() + |
|
1485 |
name.substring(offset1); |
|
1486 |
} |
|
1487 |
||
1488 |
/** |
|
1489 |
* Reverse operation for java.beans.Introspector.decapitalize. For any s, |
|
1490 |
* capitalize(decapitalize(s)).equals(s). The reverse is not true: |
|
1491 |
* e.g. capitalize("uRL") produces "URL" which is unchanged by |
|
1492 |
* decapitalize. |
|
1493 |
*/ |
|
1494 |
static String capitalize(String name) { |
|
1495 |
if (name == null || name.length() == 0) |
|
1496 |
return name; |
|
1497 |
int offset1 = name.offsetByCodePoints(0, 1); |
|
1498 |
return name.substring(0, offset1).toUpperCase() + |
|
1499 |
name.substring(offset1); |
|
1500 |
} |
|
1501 |
||
1502 |
public static String propertyName(Method m) { |
|
1503 |
String rest = null; |
|
1504 |
String name = m.getName(); |
|
1505 |
if (name.startsWith("get")) |
|
1506 |
rest = name.substring(3); |
|
1507 |
else if (name.startsWith("is") && m.getReturnType() == boolean.class) |
|
1508 |
rest = name.substring(2); |
|
1509 |
if (rest == null || rest.length() == 0 |
|
1510 |
|| m.getParameterTypes().length > 0 |
|
1511 |
|| m.getReturnType() == void.class |
|
1512 |
|| name.equals("getClass")) |
|
1513 |
return null; |
|
1514 |
return rest; |
|
1515 |
} |
|
1516 |
||
1517 |
private final static Map<Type, Type> inProgress = newIdentityHashMap(); |
|
1518 |
// really an IdentityHashSet but that doesn't exist |
|
1519 |
} |