【问题标题】:How to make @Runwith to run custom annotation processor class?如何使@Runwith 运行自定义注释处理器类?
【发布时间】:2016-11-29 08:31:10
【问题描述】:

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

注解-

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

AnnotationProcessor 类 公共类 AnnotationProcessor 扩展了 Runner{

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 !!!!!");
    }
  } 

【问题讨论】:

    标签: java junit annotations


    【解决方案1】:

    经过头脑风暴并做了一些实验,得到了所需的结果。 请建议是否有更好的方法。只有 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多