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