cache the reflect class and reflect method.

This commit is contained in:
septer
2012-08-10 18:35:36 +08:00
parent 9e2aa378a8
commit b272bfd4b7
2 changed files with 151 additions and 41 deletions
@@ -14,19 +14,30 @@ public class ReflectionUtils {
private final static Log LOG = LogFactory.getLog(ReflectionUtils.class);
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className) {
public static Class<?> getClassFromWebContainer(String className) {
Class<?> result = null;
try {
result = HttpServletRequest.class.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
if (LOG.isDebugEnabled()) LOG.debug("can'r find class in web container classLoader ", e);
}
return result;
}
public static Class<?> getClassFromCurrentClassLoader(String className) {
Class<?> result = null;
try {
result = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
LOG.error("can'r find class in current thread context classLoader ", e);
}
return result;
}
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className) {
Class<?> result = getClassFromWebContainer(className);
if (result == null) {
try {
result = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
LOG.error("can'r find class in current thread context classLoader ", e);
}
result = getClassFromCurrentClassLoader(className);
}
return result;
}
@@ -51,7 +62,25 @@ public class ReflectionUtils {
return null;
}
}
public static Method getObjectMethod(Object obj, String methodName) {
try {
return obj.getClass().getMethod(methodName);
} catch (Exception e) {
LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
return null;
}
}
public static Method getObjectMethod(Object obj, String methodName, Integer id) {
try {
return obj.getClass().getMethod(methodName, Integer.class);
} catch (Exception e) {
LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
return null;
}
}
public static Object callObjectMethod(Object obj, String methodName, Integer id) {
try {
Method m = obj.getClass().getMethod(methodName, Integer.class);