1 61 62 package org.apache.commons.discovery.tools; 63 64 import java.lang.reflect.Method ; 65 import java.lang.reflect.Modifier ; 66 67 import org.apache.commons.discovery.log.DiscoveryLogFactory; 68 import org.apache.commons.logging.Log; 69 70 71 74 public class ClassUtils { 75 private static Log log = DiscoveryLogFactory.newLog(ClassUtils.class); 76 public static void setLog(Log _log) { 77 log = _log; 78 } 79 80 87 public static String getPackageName(Class clazz) { 88 Package clazzPackage = clazz.getPackage(); 89 String packageName; 90 if (clazzPackage != null) { 91 packageName = clazzPackage.getName(); 92 } else { 93 String clazzName = clazz.getName(); 94 packageName = new String (clazzName.toCharArray(), 0, clazzName.lastIndexOf('.')); 95 } 96 return packageName; 97 } 98 99 103 public static Method findPublicStaticMethod(Class clazz, 104 Class returnType, 105 String methodName, 106 Class [] paramTypes) { 107 boolean problem = false; 108 Method method = null; 109 110 try { 112 method = clazz.getDeclaredMethod(methodName, paramTypes); 113 } catch(NoSuchMethodException e) { 114 problem = true; 115 log.debug("Class " + clazz.getName() + ": missing method '" + methodName + "(...)", e); 116 } 117 118 if (!problem && 120 !(Modifier.isPublic(method.getModifiers()) && 121 Modifier.isStatic(method.getModifiers()) && 122 method.getReturnType() == returnType)) { 123 if (log.isDebugEnabled()) { 124 if (!Modifier.isPublic(method.getModifiers())) { 125 log.debug(methodName + "() is not public"); 126 } 127 if (!Modifier.isStatic(method.getModifiers())) { 128 log.debug(methodName + "() is not static"); 129 } 130 if (method.getReturnType() != returnType) { 131 log.debug("Method returns: " + method.getReturnType().getName() + "@@" + method.getReturnType().getClassLoader()); 132 log.debug("Should return: " + returnType.getName() + "@@" + returnType.getClassLoader()); 133 } 134 } 135 problem = true; 136 method = null; 137 } 138 139 return method; 140 } 141 } 142 | Popular Tags |