Merge remote-tracking branch 'zhb/master'

Conflicts:
	src/main/java/com/alibaba/druid/support/http/StatViewServlet.java
This commit is contained in:
zhouhaibin
2012-08-10 12:45:48 +08:00
parent e5365eae94
commit 339e5f944f
20 changed files with 611 additions and 301 deletions
@@ -4,21 +4,28 @@ import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
/**
* @author sandzhang<sandzhangtoo@gmail.com>
*/
public class ReflectionUtils {
private final static Log LOG = LogFactory.getLog(ReflectionUtils.class);
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(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);
}
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);
}
}
return result;
@@ -30,16 +37,28 @@ public class ReflectionUtils {
Method m = classObject.getMethod(methodName);
return m.invoke(classObject);
} catch (Exception e) {
LOG.warn("callStaticMethod fail:class=" + classObject.getName() + " method=" + methodName, e);
return null;
}
}
public static String getTest() {
return "test";
public static Object callObjectMethod(Object obj, String methodName, Object... methodParams) {
try {
Method m = obj.getClass().getMethod(methodName);
return m.invoke(obj, methodParams);
} catch (Exception e) {
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
return null;
}
}
public void getTest() {
System.out.println("dd");
// return "test";
}
public static void main(String args[]) {
System.out.println(callStaticMethod(ReflectionUtils.class, "getTest"));
System.out.println(callObjectMethod(new ReflectionUtils(), "getTest"));
}
}