1 23 24 package com.sun.enterprise.admin.server.core.jmx; 25 26 import java.util.Collection ; 28 import java.util.Iterator ; 29 import java.util.HashMap ; 30 import java.util.ArrayList ; 31 import java.lang.reflect.Modifier ; 32 import java.lang.reflect.Method ; 33 import java.lang.reflect.InvocationTargetException ; 34 35 import com.sun.enterprise.admin.common.ParamInfo; 37 38 import javax.management.ReflectionException ; 40 import javax.management.MBeanException ; 41 42 43 51 public class Introspector 52 { 53 54 protected Class mClassReflected = null; 55 56 60 public Introspector (Class aClass) 61 { 62 if (aClass == null) 63 { 64 throw new IllegalArgumentException (); 65 } 66 mClassReflected = aClass; 67 } 68 69 81 82 public boolean implementsInterface(Class aClass) 83 { 84 boolean implInterface = false; 85 Class thisClass = mClassReflected; 86 87 while (aClass != null && thisClass != null && !implInterface) 88 { 89 Class [] interfaces = thisClass.getInterfaces(); 90 for (int i = 0 ; i < interfaces.length ; i++) 91 { 92 Class anInterface = interfaces[i]; 93 if (anInterface.getName().equals(aClass.getName())) 94 { 95 implInterface = true; 96 break; 97 } 98 } 99 thisClass = thisClass.getSuperclass(); 100 } 101 return ( implInterface ); 102 } 103 104 111 112 public boolean extendsClass(Class aClass) 113 { 114 if (aClass == null) 115 { 116 return false; 117 } 118 119 boolean extendsClass = false; 120 Class superClass = mClassReflected.getSuperclass(); 121 122 while (superClass != null && !extendsClass) 123 { 124 if (superClass.getName().equals(aClass.getName())) 125 { 126 extendsClass = true; 127 } 128 superClass = superClass.getSuperclass(); 129 } 130 131 return ( extendsClass ); 132 } 133 134 148 149 public Method getMethod(String operationName, String [] signature) 150 throws ClassNotFoundException , NoSuchMethodException , SecurityException 151 { 152 Method method = null; 153 Class [] parameterTypes = null; 154 if (signature != null) 155 { 156 parameterTypes = new Class [signature.length]; 157 for (int i = 0 ; i < signature.length ; i++) 158 { 159 String parameterName = signature[i]; 160 Class primitiveClass = ParamInfo.getPrimitiveClass(parameterName); 161 boolean parameterIsPrimitive = ( primitiveClass != null ); 162 if (parameterIsPrimitive) 163 { 164 parameterTypes[i] = primitiveClass; 165 } 166 else 167 { 168 parameterTypes[i] = Class.forName(parameterName); 169 } 170 } 171 } 172 method = mClassReflected.getMethod(operationName, parameterTypes); 173 return ( method ); 174 } 175 176 187 188 public Object invokeMethodOn(Method method, Object targetObject, 189 Object [] actualParams) 190 throws java.lang.IllegalAccessException , 191 java.lang.reflect.InvocationTargetException 192 { 193 Object result = null; 194 195 if (method == null || targetObject == null) { 197 throw new IllegalArgumentException (); 198 } 199 result = method.invoke(targetObject, actualParams); 200 return ( result ); 201 } 202 203 233 249 public Method [] getCallableInstanceMethods(Collection excludeList) 250 { 251 boolean includeAll = false; 252 if (excludeList == null || excludeList.isEmpty()) 253 { 254 includeAll = true; 255 } 256 257 ArrayList methodList = new ArrayList (); 258 Class aClass = mClassReflected; 259 Method [] methods = null; 260 while (aClass != null) 261 { 262 boolean shouldInclude = 263 includeAll || ! excludeList.contains(aClass.getName()); 264 if (shouldInclude) 265 { 266 Method [] declMethods = aClass.getDeclaredMethods(); 267 for (int i = 0 ; i < declMethods.length ; i++) 268 { 269 int modifiers = declMethods[i].getModifiers(); 270 boolean isCallableInstanceMethod = false; 271 boolean isPublicMethod = Modifier.isPublic(modifiers); 272 boolean isAbstractMethod = Modifier.isAbstract(modifiers); 273 boolean isStaticMethod = Modifier.isStatic(modifiers); 274 275 isCallableInstanceMethod = isPublicMethod && !isAbstractMethod 276 && !isStaticMethod; 277 if (isCallableInstanceMethod) 278 { 279 methodList.add(declMethods[i]); 280 } 281 } 282 } 283 aClass = aClass.getSuperclass(); 284 } 285 methods = new Method [methodList.size()]; 286 return ((Method [])methodList.toArray(methods)); 287 } 288 289 public Method [] getDeclaredConcretePublicMethods() 290 { 291 Method [] declMethods = mClassReflected.getDeclaredMethods(); 292 ArrayList publicMethods = new ArrayList (); 293 for (int i = 0 ; i < declMethods.length ; i++) 294 { 295 int modifiers = declMethods[i].getModifiers(); 296 boolean isPublic = Modifier.isPublic (modifiers); 297 boolean isAbstract = Modifier.isAbstract(modifiers); 298 if (isPublic && !isAbstract) 299 { 300 publicMethods.add(declMethods[i]); 301 } 302 } 303 Method [] pMethods = new Method [publicMethods.size()]; 304 return ( (Method [])publicMethods.toArray (pMethods) ); 305 } 306 } | Popular Tags |