This commit is contained in:
sand
2012-08-10 09:23:14 +08:00
parent 81548b0202
commit d17962d5c9
2 changed files with 31 additions and 54 deletions
@@ -4,89 +4,60 @@ 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
{
public class ReflectionUtils {
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className)
{
private final static Log LOG = LogFactory.getLog(ReflectionUtils.class);
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className) {
Class<?> result = null;
try
{
try {
result = HttpServletRequest.class.getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
LOG.debug("can'r find class in web container classLoader ", e);
}
catch (ClassNotFoundException e)
{
}
if (result == null)
{
try
{
if (result == null) {
try {
result = Thread.currentThread().getContextClassLoader().loadClass(className);
}
catch (ClassNotFoundException e)
{
} catch (ClassNotFoundException e) {
LOG.error("can'r find class in current thread context classLoader ", e);
}
}
return result;
}
public static Object callStaticMethod(Class<?> classObject, String methodName) {
public static Object callStaticMethod(Class<?> classObject, String methodName)
{
try
{
try {
Method m = classObject.getMethod(methodName);
return m.invoke(classObject);
}
catch (Exception e)
{
} catch (Exception e) {
LOG.warn("callStaticMethod fail:class=" + classObject.getName() + " method=" + methodName, e);
return null;
}
}
public static Object callObjectMethod(Object obj, String methodName)
{
try
{
public static Object callObjectMethod(Object obj, String methodName, Object... methodParams) {
try {
Method m = obj.getClass().getMethod(methodName);
return m.invoke(obj);
}
catch (Exception e)
{
return m.invoke(obj, methodParams);
} catch (Exception e) {
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
return null;
}
}
public static Object callObjectMethod(Object obj, String methodName, Object methodParam)
{
try
{
Method m = obj.getClass().getMethod(methodName);
return m.invoke(obj,methodParam);
}
catch (Exception e)
{
return null;
}
}
public void getTest()
{
public void getTest() {
System.out.println("dd");
// return "test";
}
public static void main(String args[])
{
public static void main(String args[]) {
System.out.println(callObjectMethod(new ReflectionUtils(), "getTest"));
}
@@ -3,6 +3,8 @@ package com.alibaba.druid.util;
import java.util.LinkedHashMap;
import java.util.Map;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSON;
/**
@@ -10,6 +12,8 @@ import com.alibaba.fastjson.JSON;
*/
public class StringUtils {
private final static Log LOG = LogFactory.getLog(StringUtils.class);
/**
* Example: subString("12345","1","4")=23
*
@@ -52,6 +56,7 @@ public class StringUtils {
try {
return Integer.parseInt(in);
} catch (NumberFormatException e) {
LOG.warn("stringToInteger fail,string=" + in, e);
return null;
}
}
@@ -67,6 +72,7 @@ public class StringUtils {
try {
return Long.parseLong(in);
} catch (NumberFormatException e) {
LOG.warn("stringToLong fail,string=" + in, e);
return null;
}
}