mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge remote-tracking branch 'zhb/master'
Conflicts: src/main/java/com/alibaba/druid/support/http/StatViewServlet.java
This commit is contained in:
@@ -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"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user