1 22 package org.jboss.mx.metadata; 23 24 import javax.management.MBeanAttributeInfo ; 25 import javax.management.MBeanOperationInfo ; 26 import javax.management.MBeanParameterInfo ; 27 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 28 import java.lang.reflect.Method ; 29 import java.util.HashMap ; 30 31 42 43 public class MethodMapper 44 { 45 private HashMap map = null; 46 47 50 public MethodMapper(Class resourceClass) 51 { 52 if (null == resourceClass) 53 { 54 throw new IllegalArgumentException ("resourceClass cannot be null"); 55 } 56 57 map = createMap(resourceClass); 58 } 59 60 63 public Method lookupOperation(MBeanOperationInfo info) 64 { 65 if (null == info) 66 { 67 throw new IllegalArgumentException ("MBeanOperationInfo cannot be null"); 68 } 69 70 return (Method ) map.get(operationSignature(info)); 71 } 72 73 76 public Method lookupGetter(MBeanAttributeInfo info) 77 { 78 if (null == info) 79 { 80 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 81 } 82 83 return (Method ) map.get(getterSignature(info)); 84 } 85 86 89 public Method lookupGetter(ModelMBeanAttributeInfo info) 90 { 91 if (null == info) 92 { 93 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 94 } 95 96 return (Method ) map.get(getterSignature(info)); 97 } 98 99 102 public Method lookupSetter(MBeanAttributeInfo info) 103 { 104 if (null == info) 105 { 106 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 107 } 108 109 return (Method ) map.get(setterSignature(info)); 110 } 111 112 115 public Method lookupSetter(ModelMBeanAttributeInfo info) 116 { 117 if (null == info) 118 { 119 throw new IllegalArgumentException ("ModelMBeanAttributeInfo cannot be null"); 120 } 121 122 return (Method ) map.get(setterSignature(info)); 123 } 124 125 128 public Method lookupMethod(String returnType, String name, String [] signature) 129 { 130 if (null == returnType) 131 { 132 throw new IllegalArgumentException ("returnType cannot be null"); 133 } 134 135 if (null == name) 136 { 137 throw new IllegalArgumentException ("method name cannot be null"); 138 } 139 140 return (Method ) map.get(methodSignature(returnType, name, signature)); 141 } 142 143 146 public static String getterSignature(MBeanAttributeInfo info) 147 { 148 if (null == info) 149 { 150 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 151 } 152 153 String prefix = (info.isIs()) ? "is" : "get"; 154 return methodSignature(info.getType(), prefix + info.getName(), null); 155 } 156 157 161 public static String getterSignature(ModelMBeanAttributeInfo info) 162 { 163 if (null == info) 164 { 165 throw new IllegalArgumentException ("ModelMBeanAttributeInfo cannot be null"); 166 } 167 168 String methodName = (String ) info.getDescriptor().getFieldValue("getMethod"); 169 170 if (null == methodName) 171 { 172 throw new IllegalArgumentException ("getMethod not defined in ModelMBeanAttributeInfo descriptor"); 173 } 174 175 return methodSignature(info.getType(), methodName, null); 176 } 177 178 181 public static String setterSignature(MBeanAttributeInfo info) 182 { 183 if (null == info) 184 { 185 throw new IllegalArgumentException ("MBeanAttributeInfo cannot be null"); 186 } 187 188 return methodSignature(Void.TYPE.getName(), "set" + info.getName(), new String []{info.getType()}); 189 } 190 191 195 public static String setterSignature(ModelMBeanAttributeInfo info) 196 { 197 if (null == info) 198 { 199 throw new IllegalArgumentException ("ModelMBeanAttributeInfo cannot be null"); 200 } 201 202 String methodName = (String ) info.getDescriptor().getFieldValue("setMethod"); 203 204 if (null == methodName) 205 { 206 throw new IllegalArgumentException ("setMethod not defined in ModelMBeanAttributeInfo descriptor"); 207 } 208 209 return methodSignature(Void.TYPE.getName(), methodName, new String []{info.getType()}); 210 } 211 212 215 public static String operationSignature(MBeanOperationInfo info) 216 { 217 if (null == info) 218 { 219 throw new IllegalArgumentException ("MBeanOperationInfo cannot be null"); 220 } 221 222 MBeanParameterInfo [] params = info.getSignature(); 223 String [] signature = new String [params.length]; 224 for (int i = 0; i < signature.length; i++) 225 { 226 signature[i] = params[i].getType(); 227 } 228 return methodSignature(info.getReturnType(), info.getName(), signature); 229 } 230 231 234 public static String methodSignature(Method method) 235 { 236 if (null == method) 237 { 238 throw new IllegalArgumentException ("Method cannot be null"); 239 } 240 241 Class [] paramtypes = method.getParameterTypes(); 242 String [] signature = new String [paramtypes.length]; 243 for (int i = 0; i < signature.length; i++) 244 { 245 signature[i] = paramtypes[i].getName(); 246 } 247 return methodSignature(method.getReturnType().getName(), method.getName(), signature); 248 } 249 250 253 public static String methodSignature(String returnType, String name, String [] signature) 254 { 255 if (null == returnType) 256 { 257 throw new IllegalArgumentException ("returnType cannot be null"); 258 } 259 if (null == name) 260 { 261 throw new IllegalArgumentException ("method name cannot be null"); 262 } 263 264 StringBuffer buf = new StringBuffer (returnType).append(';').append(name); 265 if (null == signature) 266 { 267 return buf.toString(); 268 } 269 270 for (int i = 0; i < signature.length; i++) 271 { 272 buf.append(';').append(signature[i]); } 274 275 return buf.toString(); 276 } 277 278 281 protected HashMap createMap(Class resourceClass) 282 { 283 HashMap cmap = new HashMap (); 284 Method [] methods = resourceClass.getMethods(); 285 for (int i = 0; i < methods.length; i++) 286 { 287 cmap.put(methodSignature(methods[i]), methods[i]); 288 } 289 return cmap; 290 } 291 } 292 | Popular Tags |