我想实现的功能是通过run调用Test注解的不带参数无返回值的方法 为什么不能打印出来 invoke难道没执行么?
RUN类代码如下:
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);
}
}

解决方案 »

  1.   

    是不是Test注解类少了@Retention(value=RUNTIME)
      

  2.   

    加了@Retention(value=RUNTIME)后就会出现一个异常
    Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at ApplicationRun.run(ApplicationRun.java:14)
    at MyClass.main(MyClass.java:32)
      

  3.   

    你没判断参数个数就执行add(int ,int)执行的时候出错了
      

  4.   


    这个程序并不是最好的,有些还可以改进
    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);
        }

     
     
      

  5.   

    本来就没有for (Object ob : method.getAnnotations()) {
    System.out.println(ob);
    }一个也找不出来
      

  6.   

    注解Test要改成@Retention(RetentionPolicy.RUNTIME)
    @interface Test {
    }
    但是由于你每个方法的参数不是一样的,所以全部都用
    method.invoke(obj,new Object[]{});
    来执行是不行的,
    如果全部是对象,又不怕空指针异常可以这样 method.invoke(obj, new Object[method.getGenericParameterTypes().length]);但这个也不全行,那个add(int ,int)方法就会报错,因为传入的参数是NULL,NULL-> int 就异常了LZ自己再作修改
      

  7.   

    大概的原因 我知道了 就是不知道如何判断方法的返回为void,并且是无参的 系统认为我的run方法是把所有含有Test注解的方法输出 ,我用Invoke时没传参 有的方法有参数 所以产生参数异常。谁知道争样通过反射中的getReturnType()来判断方法返回类型是不是void?
      

  8.   


    if(method.getReturnType().equals(void.class)){
    System.out.println(method + " \t " + method.getReturnType());
    }
      

  9.   

    终于弄出来了 改成 if("void"==method.getReturnType().getName() && 0==method.getParameterTypes().length)
            method.invoke(obj,new Object[]{});
    就好了