62,614
社区成员
发帖
与我相关
我的任务
分享
import java.lang.reflect.Method;
public class ApplicationRun
{
public void run(String className) throws Exception
{
Class<?> classtype = Class.forName(className);
Object obj = classtype.newInstance();
Method[] methods = classtype.getMethods();
for(Method method:methods)
{
if (method.isAnnotationPresent(Test.class))
{
method.invoke(obj,new Object[]{});
}
}
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
public @interface Test
{
}
import java.lang.reflect.InvocationTargetException;
public class MyClass
{
public void method1()
{
System.out.println("method1");
}
@Test
public void method2()
{
System.out.println("method2");
}
@Test
public int add(int a,int b)
{
return a+b;
}
@Test
public void doSomething(String str)
{
System.out.println(str);
}
@Test
public void doSomething2()
{
System.out.println("doSomething2()");
}
public static void main(String[] args) throws Exception
{
String className = MyClass.class.getName();
ApplicationRun testRun = new ApplicationRun();
testRun.run(className);
}
}
if(method.getReturnType().equals(void.class)){
System.out.println(method + " \t " + method.getReturnType());
}