パラメータアノテーション

長い間、使う意味が解らなかったものを見つけた時の喜びが感動である。
メソッドのパラメータにつけるアノテーションの便利さが、なかなか見つからなかった。
見つけた時の感動を忘れずに励みにしようと思う。
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
/**
 * Perl5パターンハンドラ・インターセプタ
 */
final class P5HandleInterceptor implements MethodInterceptor{
   private Class<?> cls;
   protected P5HandleInterceptor(Class<?> cls){
      this.cls = cls;
   }
   /* (非 Javadoc)
    * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
    */
   public Object invoke(MethodInvocation m) throws Throwable{
      // @P5Pattern が付くパラメータと、@P5Target が付くパラメータを探して、値を取得
      String target = null;                         // 検査対象文字列
      String patternStr = null;                     // 指定されたパターン
      Object args = m.getArguments();
      Annotation
 anos = m.getMethod().getParameterAnnotations();
      for(int n=0;n < anos.length;n++){
         for(int k=0;k < anos[n].length;k++){
            if (anos[n][k].annotationType().equals(jp.sourceforge.jsuite.oro.P5Pattern.class)){
               if (args[n].getClass().getName().equals("java.lang.String")){
                  patternStr = (String)args[n];
               }
            }else if(anos[n][k].annotationType().equals(P5Target.class)){
               if (args[n].getClass().getName().equals("java.lang.String")){
                  target = (String)args[n];
               }
            }
         }
      }
      if (patternStr==null){
         throw new RuntimeException("must be P5Pattern annotation String Parameter");
      }
      if (target==null){
         throw new RuntimeException("must be P5Target annotation String Parameter");
      }
       
      // セットするフィールドを探す→@P5Result が付いてるString
      Field resultField = null;                     // 結果フィールド
      Field fls = this.cls.getDeclaredFields();

      for(int i=0;i < fls.length;i++){
         P5Result patternResult = fls[i].getAnnotation(P5Result.class);
         if (patternResult != null){
            if (fls[i].getType().getName().equals("java.lang.String")){
               resultField = fls[i];
               resultField.setAccessible(true);
            }else{
               throw new RuntimeException("must be P5Result annotation String Field");
            }
         }
      }
      if (resultField==null){
         throw new RuntimeException("must be P5Result annotation String Field");
      }
      // Perl5 パターンマッチ
      Perl5Compiler compiler = new Perl5Compiler();
      Perl5Matcher matcher = new Perl5Matcher();
      Pattern pattern = compiler.compile(patternStr);
      int matchCount = 0;
      PatternMatcherInput input = new PatternMatcherInput(target);
      while*1;
            // メソッド実行
            m.proceed();
         }
      }
      // メソッド実行回数=マッチ数を返す
      return matchCount;
   }
}

*1:matcher.contains(input,pattern))){
         MatchResult res = matcher.getMatch();
         int glen = res.groups();
         for(int i=0;i < glen;i++){
            matchCount++;
            // 結果セット
            resultField.set(m.getThis(),res.group(i