1 7 8 package com.sun.jmx.mbeanserver; 9 import java.io.InvalidObjectException ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Type ; 13 import javax.management.Descriptor ; 14 import javax.management.MBeanException ; 15 import javax.management.ReflectionException ; 16 import javax.management.RuntimeErrorException ; 17 import javax.management.RuntimeMBeanException ; 18 import javax.management.openmbean.OpenDataException ; 19 import javax.management.openmbean.OpenType ; 20 21 24 final class ConvertingMethod { 25 static ConvertingMethod from(Method m) { 26 try { 27 return new ConvertingMethod(m); 28 } catch (OpenDataException ode) { 29 final String 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 (msg, ode); 33 } 34 } 35 36 Method getMethod() { 37 return method; 38 } 39 40 Descriptor getDescriptor() { 41 return Introspector.descriptorForElement(method); 42 } 43 44 Type getGenericReturnType() { 45 return method.getGenericReturnType(); 46 } 47 48 Type [] getGenericParameterTypes() { 49 return method.getGenericParameterTypes(); 50 } 51 52 String getName() { 53 return method.getName(); 54 } 55 56 OpenType getOpenReturnType() { 57 return returnConverter.getOpenType(); 58 } 59 60 OpenType [] getOpenParameterTypes() { 61 final OpenType [] types = new OpenType [paramConverters.length]; 62 for (int i = 0; i < paramConverters.length; i++) 63 types[i] = paramConverters[i].getOpenType(); 64 return types; 65 } 66 67 75 void checkCallFromOpen() throws IllegalArgumentException { 76 try { 77 for (OpenConverter paramConverter : paramConverters) 78 paramConverter.checkReconstructible(); 79 } catch (InvalidObjectException e) { 80 throw new IllegalArgumentException (e); 81 } 82 } 83 84 92 void checkCallToOpen() throws IllegalArgumentException { 93 try { 94 returnConverter.checkReconstructible(); 95 } catch (InvalidObjectException e) { 96 throw new IllegalArgumentException (e); 97 } 98 } 99 100 String [] getOpenSignature() { 101 if (paramConverters.length == 0) 102 return noStrings; 103 104 String [] sig = new String [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 toOpenReturnValue(MXBeanLookup lookup, Object ret) 111 throws OpenDataException { 112 return returnConverter.toOpenValue(lookup, ret); 113 } 114 115 final Object fromOpenReturnValue(MXBeanLookup lookup, Object ret) 116 throws InvalidObjectException { 117 return returnConverter.fromOpenValue(lookup, ret); 118 } 119 120 final Object [] toOpenParameters(MXBeanLookup lookup, Object [] params) 121 throws OpenDataException { 122 if (paramConversionIsIdentity || params == null) 123 return params; 124 final Object [] oparams = new Object [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 [] fromOpenParameters(MXBeanLookup lookup, Object [] params) 131 throws InvalidObjectException { 132 if (paramConversionIsIdentity || params == null) 133 return params; 134 final Object [] jparams = new Object [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 toOpenParameter(MXBeanLookup lookup, 141 Object param, 142 int paramNo) 143 throws OpenDataException { 144 return paramConverters[paramNo].toOpenValue(lookup, param); 145 } 146 147 final Object fromOpenParameter(MXBeanLookup lookup, 148 Object param, 149 int paramNo) 150 throws InvalidObjectException { 151 return paramConverters[paramNo].fromOpenValue(lookup, param); 152 } 153 154 Object invokeWithOpenReturn(MXBeanLookup lookup, 155 Object obj, Object [] params) 156 throws MBeanException , IllegalAccessException , 157 InvocationTargetException { 158 final Object [] javaParams; 159 try { 160 javaParams = fromOpenParameters(lookup, params); 161 } catch (InvalidObjectException e) { 162 final String msg = methodName() + ": cannot convert parameters " + 164 "from open values: " + e; 165 throw new MBeanException (e, msg); 166 } 167 final Object javaReturn = method.invoke(obj, javaParams); 168 try { 169 return returnConverter.toOpenValue(lookup, javaReturn); 170 } catch (OpenDataException e) { 171 final String msg = methodName() + ": cannot convert return " + 173 "value to open value: " + e; 174 throw new MBeanException (e, msg); 175 } 176 } 177 178 private String methodName() { 179 return method.getDeclaringClass() + "." + method.getName(); 180 } 181 182 private ConvertingMethod(Method m) throws OpenDataException { 183 this.method = m; 184 returnConverter = OpenConverter.toConverter(m.getGenericReturnType()); 185 Type [] 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 [] noOpenTypes = new OpenType [0]; 196 private static final String [] noStrings = new String [0]; 197 198 private final Method method; 199 private final OpenConverter returnConverter; 200 private final OpenConverter[] paramConverters; 201 private final boolean paramConversionIsIdentity; 202 } 203 | Popular Tags |