您的位置:首页 > 编程语言 > Java开发

java反射记录

2015-07-08 17:30 681 查看
 /**
     * 
     * 反射实现
     * 
     * */
    public static boolean invokeTest(String className, String methodName,
            Class<?>[] parmList)
    {
        boolean isExist = false;
        
        try {
            Class<?> mClass = Class.forName(className);

           /* Method[] methodList = mClass.getMethods();
            for(Method method : methodList)
            {
                LogUtils.logDebug(true, method.getName());
                if(methodName.equals(method.getName()))
                {
                    isExist = true;
                    Class<?>[] sClasses = method.getParameterTypes();
                    break;
                }
            }*/
            
            Method mMethod = null;
            try {
                mMethod = mClass.getMethod(methodName,
                        new Class<?>[]{int.class, String.class});
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            try {
                mMethod.invoke(mClass.newInstance(), 2,<span style="font-family:Arial, Helvetica, sans-serif;">methodName);</span>
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return isExist;
    }


/**
*
* 要调用的函数
*
* */
public void test(int arg1, String arg2)
{
LogUtils.logDebug(true, "invoke test arg1:" + arg1 + " arg2:" + arg2);
}
/**
*
* 反射调用
*
* */
invokeTest("org.zywx.wbpalmstar.plugin.utils.UIConfig",
"test", new Class[]{int.class, String.class});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: