History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/com/sun/jmx/mbeanserver/ConvertingMethod.java (1 of 18352 files). Show all

Unified · Split

java/pkg/com/sun/jmx/mbeanserver/ConvertingMethod.java+228−0
@@ -0,0 +1,228 @@
1+/*
2+ * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package com.sun.jmx.mbeanserver;
27+import java.io.InvalidObjectException;
28+import java.lang.reflect.InvocationTargetException;
29+import java.lang.reflect.Method;
30+import java.lang.reflect.Type;
31+
32+import javax.management.Descriptor;
33+import javax.management.MBeanException;
34+import javax.management.openmbean.OpenDataException;
35+import javax.management.openmbean.OpenType;
36+
37+final class ConvertingMethod {
38+ static ConvertingMethod from(Method m) {
39+ try {
40+ return new ConvertingMethod(m);
41+ } catch (OpenDataException ode) {
42+ final String msg = "Method " + m.getDeclaringClass().getName() +
43+ "." + m.getName() + " has parameter or return type that " +
44+ "cannot be translated into an open type";
45+ throw new IllegalArgumentException(msg, ode);
46+ }
47+ }
48+
49+ Method getMethod() {
50+ return method;
51+ }
52+
53+ Descriptor getDescriptor() {
54+ return Introspector.descriptorForElement(method);
55+ }
56+
57+ Type getGenericReturnType() {
58+ return method.getGenericReturnType();
59+ }
60+
61+ Type[] getGenericParameterTypes() {
62+ return method.getGenericParameterTypes();
63+ }
64+
65+ String getName() {
66+ return method.getName();
67+ }
68+
69+ OpenType<?> getOpenReturnType() {
70+ return returnMapping.getOpenType();
71+ }
72+
73+ OpenType<?>[] getOpenParameterTypes() {
74+ final OpenType<?>[] types = new OpenType<?>[paramMappings.length];
75+ for (int i = 0; i < paramMappings.length; i++)
76+ types[i] = paramMappings[i].getOpenType();
77+ return types;
78+ }
79+
80+ /* Check that this method will be callable when we are going from
81+ * open types to Java types, for example when we are going from
82+ * an MXBean wrapper to the underlying resource.
83+ * The parameters will be converted to
84+ * Java types, so they must be "reconstructible". The return
85+ * value will be converted to an Open Type, so if it is convertible
86+ * at all there is no further check needed.
87+ */
88+ void checkCallFromOpen() {
89+ try {
90+ for (MXBeanMapping paramConverter : paramMappings)
91+ paramConverter.checkReconstructible();
92+ } catch (InvalidObjectException e) {
93+ throw new IllegalArgumentException(e);
94+ }
95+ }
96+
97+ /* Check that this method will be callable when we are going from
98+ * Java types to open types, for example when we are going from
99+ * an MXBean proxy to the open types that it will be mapped to.
100+ * The return type will be converted back to a Java type, so it
101+ * must be "reconstructible". The parameters will be converted to
102+ * open types, so if it is convertible at all there is no further
103+ * check needed.
104+ */
105+ void checkCallToOpen() {
106+ try {
107+ returnMapping.checkReconstructible();
108+ } catch (InvalidObjectException e) {
109+ throw new IllegalArgumentException(e);
110+ }
111+ }
112+
113+ String[] getOpenSignature() {
114+ if (paramMappings.length == 0)
115+ return noStrings;
116+
117+ String[] sig = new String[paramMappings.length];
118+ for (int i = 0; i < paramMappings.length; i++)
119+ sig[i] = paramMappings[i].getOpenClass().getName();
120+ return sig;
121+ }
122+
123+ final Object toOpenReturnValue(MXBeanLookup lookup, Object ret)
124+ throws OpenDataException {
125+ return returnMapping.toOpenValue(ret);
126+ }
127+
128+ final Object fromOpenReturnValue(MXBeanLookup lookup, Object ret)
129+ throws InvalidObjectException {
130+ return returnMapping.fromOpenValue(ret);
131+ }
132+
133+ final Object[] toOpenParameters(MXBeanLookup lookup, Object[] params)
134+ throws OpenDataException {
135+ if (paramConversionIsIdentity || params == null)
136+ return params;
137+ final Object[] oparams = new Object[params.length];
138+ for (int i = 0; i < params.length; i++)
139+ oparams[i] = paramMappings[i].toOpenValue(params[i]);
140+ return oparams;
141+ }
142+
143+ final Object[] fromOpenParameters(Object[] params)
144+ throws InvalidObjectException {
145+ if (paramConversionIsIdentity || params == null)
146+ return params;
147+ final Object[] jparams = new Object[params.length];
148+ for (int i = 0; i < params.length; i++)
149+ jparams[i] = paramMappings[i].fromOpenValue(params[i]);
150+ return jparams;
151+ }
152+
153+ final Object toOpenParameter(MXBeanLookup lookup,
154+ Object param,
155+ int paramNo)
156+ throws OpenDataException {
157+ return paramMappings[paramNo].toOpenValue(param);
158+ }
159+
160+ final Object fromOpenParameter(MXBeanLookup lookup,
161+ Object param,
162+ int paramNo)
163+ throws InvalidObjectException {
164+ return paramMappings[paramNo].fromOpenValue(param);
165+ }
166+
167+ Object invokeWithOpenReturn(MXBeanLookup lookup,
168+ Object obj, Object[] params)
169+ throws MBeanException, IllegalAccessException,
170+ InvocationTargetException {
171+ MXBeanLookup old = MXBeanLookup.getLookup();
172+ try {
173+ MXBeanLookup.setLookup(lookup);
174+ return invokeWithOpenReturn(obj, params);
175+ } finally {
176+ MXBeanLookup.setLookup(old);
177+ }
178+ }
179+
180+ private Object invokeWithOpenReturn(Object obj, Object[] params)
181+ throws MBeanException, IllegalAccessException,
182+ InvocationTargetException {
183+ final Object[] javaParams;
184+ try {
185+ javaParams = fromOpenParameters(params);
186+ } catch (InvalidObjectException e) {
187+ // probably can't happen
188+ final String msg = methodName() + ": cannot convert parameters " +
189+ "from open values: " + e;
190+ throw new MBeanException(e, msg);
191+ }
192+ final Object javaReturn = method.invoke(obj, javaParams);
193+ try {
194+ return returnMapping.toOpenValue(javaReturn);
195+ } catch (OpenDataException e) {
196+ // probably can't happen
197+ final String msg = methodName() + ": cannot convert return " +
198+ "value to open value: " + e;
199+ throw new MBeanException(e, msg);
200+ }
201+ }
202+
203+ private String methodName() {
204+ return method.getDeclaringClass() + "." + method.getName();
205+ }
206+
207+ private ConvertingMethod(Method m) throws OpenDataException {
208+ this.method = m;
209+ MXBeanMappingFactory mappingFactory = MXBeanMappingFactory.DEFAULT;
210+ returnMapping =
211+ mappingFactory.mappingForType(m.getGenericReturnType(), mappingFactory);
212+ Type[] params = m.getGenericParameterTypes();
213+ paramMappings = new MXBeanMapping[params.length];
214+ boolean identity = true;
215+ for (int i = 0; i < params.length; i++) {
216+ paramMappings[i] = mappingFactory.mappingForType(params[i], mappingFactory);
217+ identity &= DefaultMXBeanMappingFactory.isIdentity(paramMappings[i]);
218+ }
219+ paramConversionIsIdentity = identity;
220+ }
221+
222+ private static final String[] noStrings = new String[0];
223+
224+ private final Method method;
225+ private final MXBeanMapping returnMapping;
226+ private final MXBeanMapping[] paramMappings;
227+ private final boolean paramConversionIsIdentity;
228+}