2016-11-29 1 views
0

私は、メソッドの実行に必要な時間を処理する注釈と注釈プロセッサークラスを作成したいと考えています。このメソッドには、私が定義したカスタムアノテーションを処理するコードが含まれます。さて、以下は作業コードですが、落とし穴があります。カスタムアノテーションを持つクラスにmainメソッドを記述して、アノテーションプロセッサクラスを呼び出す必要があります。これは避けたいものです。この問題を克服する方法。私が欲しいのは、計算するべきメソッドに@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

ブレーンストーミングしていくつかの実験を行った後、必要な結果が得られました。 可能な方法があれば教えてください。注釈プロセッサークラスにのみ変更があります。

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 
    } 
} 
関連する問題