2016-03-21 3 views
1

私は多くを検索しましたが、それほど有用なものは何も見つかりませんでした。Guiceのネストされたアノテーションメソッドのインターセプト

問題: 私が作成したカスタム注釈のように:私はDIのためのGuiceを使用しています

@MapExceptions(value = { 
     @MapException(sources = {IllegalArgumentException.class, RuntimeException.class}, destination = BadRequestException.class), 
     @MapException(sources = {RuntimeException.class}, destination = BadRequestException.class) 
}) 

  1. 2つのメソッドインターセプタを記述する必要がありますか?実際の作業は@MapExceptionで完了しています
  2. もしそうなら、@MapExceptionインターセプタは@MapExceptionsインターセプタのメソッド呼び出しメソッドを呼び出すことができますか?私はコードを複製したくない。
  3. マイ@MapExceptionインターセプターは

パブリッククラスMapExceptionInterceptor私はバインディング現在

bindInterceptor(Matchers.any(), Matchers.annotatedWith(MapException.class), new MapExceptionInterceptor()); 

次使用していますMethodInterceptorの{

@Override 
public Object invoke(MethodInvocation invocation) throws Throwable { 
    try { 
     return invocation.proceed(); 
    } catch (Exception actualException) { 
     Method method = invocation.getMethod(); 
     Annotation[] annotations = method.getDeclaredAnnotations(); 
     for (Annotation annotation : annotations) { 
      if (annotation instanceof MapException) { 
       MapException mapException = (MapException) annotation; 
       Class<? extends Throwable> destinationClass = mapException.destination(); 
       Class<? extends Throwable>[] sourceClasses = mapException.sources(); 
       for (Class sourceExceptionClass : sourceClasses) { 
        if (actualException.getClass().isInstance(sourceExceptionClass)) { 
         Constructor ctr = destinationClass.getConstructor(String.class); 
         throw (Throwable) ctr.newInstance(actualException.getMessage()); 
        } 
       } 
      } 
     } 
     throw actualException; 
    } 
} 

}

が、これは大丈夫です実装し、次のようになります。 ?それとも改善することができますか?

ありがとうございました!

答えて

0

したがって、内部注釈は単なるデータバッグです。 これを解決するために、すべての作業を行う外部アノテーション(MapExceptions)のインターセプタを作成しました。

+0

これを解決するために使用したコードを提供できますか? – behinddwalls

関連する問題