您的位置:首页 > 其它

利用反射获取方法的反回值类型和参数个数

2014-05-01 16:23 316 查看
package com.test;

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import java.lang.reflect.Type;

import org.junit.Test;

public class Application

{

public void run(String ClassName) throws Exception

{

Class<?> classType = Class.forName(ClassName);

Object object = classType.newInstance();

Method[] methods = classType.getMethods();

for(int i = 0; i< methods.length;i++)

{

if(methods[i].isAnnotationPresent(Test.class))

{

Type type = methods[i].getReturnType();

Type[] param = methods[i].getGenericParameterTypes();

if(type == void.class && param.length == 0)

{

methods[i].invoke(object,new Object[]{});

}

}

}

return;

}

}

package com.test;

import org.junit.Test;

public class MyTest

{

/**

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception

{

System.out.println("zjhans");

Application app = new Application();

app.run("com.test.myTEST");

}

@Test

public void add()

{

System.out.println("This is Test");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐