2016-11-29 76 views
0

我想写一个注释和注释处理器类,它处理一个方法执行所需的时间。此方法将包含处理自定义注释的代码。现在,下面是工作代码,但它有一个陷阱。我需要在具有自定义注释的类中编写主要方法,然后调用我想要避免的Annotation处理器类。如何解决这个问题。我想要的是每当我将@Performance方法添加到它应该计算的方法上并给出该方法消耗的时间?任何人都可以告诉我们如何实现这一目标?如何使@Runwith运行自定义注释处理器类?

注释 -

@Target({ ElementType.METHOD }) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface Performance { 
    boolean active() default true; 
} 

AnnotationProcessor类 公共类AnnotationProcessor扩展亚军{

public void main(Class<?> clazz) { 

    Long startTime, endTime;  

    for(Method m : clazz.getMethods()){ 
     if(m.isAnnotationPresent(Performance.class)){ 
      Annotation an = m.getAnnotation(Performance.class); 
      try{ 
       Performance per = (Performance) an; 
       if(per.active()){ 
        startTime=System.currentTimeMillis(); 
         System.out.println("---------------- Start time --------["+startTime+"]---------------"); 
         m.invoke(clazz.newInstance()); 
        endTime=System.currentTimeMillis(); 
         System.out.println("---------------- End time ----------["+endTime+"]---------------"); 
         System.out.println("---------------- Time difference :"+(endTime-startTime)); 
       } 
      }catch(Throwable t){ 
       t.printStackTrace(); 
      } 
     } 
    } 
} 

@Override 
public Description getDescription() { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public void run(RunNotifier arg0) { 
    // TODO Auto-generated method stub 
} 

}

测试类的定制注释 -

@RunWith(AnnotationProcessor.class) 
public class CheckAnnotation { 

    public CheckAnnotation(){} 

    public static void main(String[] args) { 
     method(); 
     methodA(); 
    } 

    @Performance 
    public static void method(){ 
     for(int k=0;k<100000;k++) 
     { 
      for(int l=3;l<9000000;l++){ 
       //some code 
      } 
     } 
     System.out.println("Writing code done !!!!"); 
    } 

    @Performance 
    public static void methodA(){ 
     for(int j=1;j<32434324;j++){ 
      // some code 
     } 
     System.out.println("Code execution done !!!!!"); 
    } 
    } 

回答

0

头脑风暴和做一些实验后得到了所需的结果。 请建议是否有更好的方法可能。只有Annotation处理器类有变化。

public class AnnotationProcessor extends Runner{ 

    public AnnotationProcessor(Class<?> clazz) { 

     Long startTime, endTime;  
     for(Method m : clazz.getMethods()){ 
      if(m.isAnnotationPresent(Performance.class)){ 

       Annotation an = m.getAnnotation(Performance.class); 
       try{ 
         Performance per = (Performance) an; 
         startTime=System.currentTimeMillis(); 
          System.out.println("---------------- Start time --------["+startTime+"]---------------"); 
          m.invoke(clazz.newInstance()); 
         endTime=System.currentTimeMillis(); 
          System.out.println("---------------- End time ----------["+endTime+"]---------------"); 
          System.out.println("---------------- Time difference :"+(endTime-startTime)); 
          System.out.println(); 

       }catch(Throwable t){ 
        t.printStackTrace(); 
       } 
      } 
     } 
    } 


    @Override 
    public Description getDescription() { 
     return Description.EMPTY; 
    } 

    @Override 
    public void run(RunNotifier arg0) { 
     // TODO Auto-generated method stub 
    } 
}