KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > mbeanserver > ConvertingMethod


1 /*
2  * @(#)ConvertingMethod.java 1.9 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.mbeanserver;
9 import java.io.InvalidObjectException JavaDoc;
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.Type JavaDoc;
13 import javax.management.Descriptor JavaDoc;
14 import javax.management.MBeanException JavaDoc;
15 import javax.management.ReflectionException JavaDoc;
16 import javax.management.RuntimeErrorException JavaDoc;
17 import javax.management.RuntimeMBeanException JavaDoc;
18 import javax.management.openmbean.OpenDataException JavaDoc;
19 import javax.management.openmbean.OpenType JavaDoc;
20
21 /**
22    @author Eamonn McManus
23 */

24 final class ConvertingMethod {
25     static ConvertingMethod from(Method JavaDoc m) {
26     try {
27         return new ConvertingMethod(m);
28     } catch (OpenDataException JavaDoc ode) {
29         final String JavaDoc msg = "Method " + m.getDeclaringClass().getName() +
30         "." + m.getName() + " has parameter or return type that " +
31         "cannot be translated into an open type";
32         throw new IllegalArgumentException JavaDoc(msg, ode);
33     }
34     }
35
36     Method JavaDoc getMethod() {
37     return method;
38     }
39     
40     Descriptor JavaDoc getDescriptor() {
41         return Introspector.descriptorForElement(method);
42     }
43
44     Type JavaDoc getGenericReturnType() {
45         return method.getGenericReturnType();
46     }
47
48     Type JavaDoc[] getGenericParameterTypes() {
49         return method.getGenericParameterTypes();
50     }
51
52     String JavaDoc getName() {
53     return method.getName();
54     }
55
56     OpenType JavaDoc getOpenReturnType() {
57     return returnConverter.getOpenType();
58     }
59
60     OpenType JavaDoc[] getOpenParameterTypes() {
61     final OpenType JavaDoc[] types = new OpenType JavaDoc[paramConverters.length];
62     for (int i = 0; i < paramConverters.length; i++)
63         types[i] = paramConverters[i].getOpenType();
64     return types;
65     }
66     
67     /* Check that this method will be callable when we are going from
68      * open types to Java types, for example when we are going from
69      * an MXBean wrapper to the underlying resource.
70      * The parameters will be converted to
71      * Java types, so they must be "reconstructible". The return
72      * value will be converted to an Open Type, so if it is convertible
73      * at all there is no further check needed.
74      */

75     void checkCallFromOpen() throws IllegalArgumentException JavaDoc {
76         try {
77             for (OpenConverter paramConverter : paramConverters)
78                 paramConverter.checkReconstructible();
79         } catch (InvalidObjectException JavaDoc e) {
80             throw new IllegalArgumentException JavaDoc(e);
81         }
82     }
83
84     /* Check that this method will be callable when we are going from
85      * Java types to open types, for example when we are going from
86      * an MXBean proxy to the open types that it will be mapped to.
87      * The return type will be converted back to a Java type, so it
88      * must be "reconstructible". The parameters will be converted to
89      * open types, so if it is convertible at all there is no further
90      * check needed.
91      */

92     void checkCallToOpen() throws IllegalArgumentException JavaDoc {
93         try {
94             returnConverter.checkReconstructible();
95         } catch (InvalidObjectException JavaDoc e) {
96             throw new IllegalArgumentException JavaDoc(e);
97         }
98     }
99
100     String JavaDoc[] getOpenSignature() {
101     if (paramConverters.length == 0)
102         return noStrings;
103
104     String JavaDoc[] sig = new String JavaDoc[paramConverters.length];
105     for (int i = 0; i < paramConverters.length; i++)
106         sig[i] = paramConverters[i].getOpenClass().getName();
107     return sig;
108     }
109
110     final Object JavaDoc toOpenReturnValue(MXBeanLookup lookup, Object JavaDoc ret)
111             throws OpenDataException JavaDoc {
112     return returnConverter.toOpenValue(lookup, ret);
113     }
114
115     final Object JavaDoc fromOpenReturnValue(MXBeanLookup lookup, Object JavaDoc ret)
116             throws InvalidObjectException JavaDoc {
117     return returnConverter.fromOpenValue(lookup, ret);
118     }
119
120     final Object JavaDoc[] toOpenParameters(MXBeanLookup lookup, Object JavaDoc[] params)
121             throws OpenDataException JavaDoc {
122     if (paramConversionIsIdentity || params == null)
123         return params;
124     final Object JavaDoc[] oparams = new Object JavaDoc[params.length];
125     for (int i = 0; i < params.length; i++)
126         oparams[i] = paramConverters[i].toOpenValue(lookup, params[i]);
127     return oparams;
128     }
129
130     final Object JavaDoc[] fromOpenParameters(MXBeanLookup lookup, Object JavaDoc[] params)
131             throws InvalidObjectException JavaDoc {
132     if (paramConversionIsIdentity || params == null)
133         return params;
134     final Object JavaDoc[] jparams = new Object JavaDoc[params.length];
135     for (int i = 0; i < params.length; i++)
136         jparams[i] = paramConverters[i].fromOpenValue(lookup, params[i]);
137     return jparams;
138     }
139
140     final Object JavaDoc toOpenParameter(MXBeanLookup lookup,
141                                  Object JavaDoc param,
142                                  int paramNo)
143         throws OpenDataException JavaDoc {
144         return paramConverters[paramNo].toOpenValue(lookup, param);
145     }
146
147     final Object JavaDoc fromOpenParameter(MXBeanLookup lookup,
148                                    Object JavaDoc param,
149                                    int paramNo)
150         throws InvalidObjectException JavaDoc {
151         return paramConverters[paramNo].fromOpenValue(lookup, param);
152     }
153
154     Object JavaDoc invokeWithOpenReturn(MXBeanLookup lookup,
155                                 Object JavaDoc obj, Object JavaDoc[] params)
156         throws MBeanException JavaDoc, IllegalAccessException JavaDoc,
157                    InvocationTargetException JavaDoc {
158     final Object JavaDoc[] javaParams;
159     try {
160         javaParams = fromOpenParameters(lookup, params);
161     } catch (InvalidObjectException JavaDoc e) {
162         // probably can't happen
163
final String JavaDoc msg = methodName() + ": cannot convert parameters " +
164         "from open values: " + e;
165         throw new MBeanException JavaDoc(e, msg);
166     }
167     final Object JavaDoc javaReturn = method.invoke(obj, javaParams);
168     try {
169         return returnConverter.toOpenValue(lookup, javaReturn);
170     } catch (OpenDataException JavaDoc e) {
171         // probably can't happen
172
final String JavaDoc msg = methodName() + ": cannot convert return " +
173         "value to open value: " + e;
174         throw new MBeanException JavaDoc(e, msg);
175     }
176     }
177
178     private String JavaDoc methodName() {
179     return method.getDeclaringClass() + "." + method.getName();
180     }
181
182     private ConvertingMethod(Method JavaDoc m) throws OpenDataException JavaDoc {
183     this.method = m;
184     returnConverter = OpenConverter.toConverter(m.getGenericReturnType());
185     Type JavaDoc[] params = m.getGenericParameterTypes();
186     paramConverters = new OpenConverter[params.length];
187     boolean identity = true;
188     for (int i = 0; i < params.length; i++) {
189         paramConverters[i] = OpenConverter.toConverter(params[i]);
190         identity &= paramConverters[i].isIdentity();
191     }
192     paramConversionIsIdentity = identity;
193     }
194
195     private static final OpenType JavaDoc[] noOpenTypes = new OpenType JavaDoc[0];
196     private static final String JavaDoc[] noStrings = new String JavaDoc[0];
197
198     private final Method JavaDoc method;
199     private final OpenConverter returnConverter;
200     private final OpenConverter[] paramConverters;
201     private final boolean paramConversionIsIdentity;
202 }
203
Popular Tags