2016-04-22 43 views
0

我正在使用Play框架開發服務器。在我的幾個方法中,我需要執行一些以前的操作(基本上是輸入檢查),所以我認爲這樣做的最佳方式是Action Composition在Play框架中組合動作時重複相同動作

我可以用幾個註解沒有問題

@Action1 // <---------------------------------------- This action is executed 
@Action2(value = "someValue") // <------------------- This action is executed 
public CompletionStage<Result> doSomething() { 
    ... 
} 

但只要我儘量重複這些動作具體行動不執行的一個:

@Action1 // <---------------------------------------- This action is executed 
@Action2(value = "someValue") // <------------------- This action is not executed 
@Action2(value = "someOtherValue") // <-------------- This action is not executed 
public CompletionStage<Result> doSomething() { 
    ... 
} 

Action1註釋看起來像Play Framework exampleVerboseAnnotation,所以我不認爲這是值得寫在這裏。正如我Action2註釋可以重複我已經宣佈RepeatableAction2註釋是這樣的:

@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface RepeatableAction2 { 
    Action2[] value() default {}; 
} 

Action2看起來是這樣的:

@With(Action2Impl.class) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Repeatable(value = RepeatableAction2.class) 
public @interface Action2 { 
    String value(); 
} 

的方法是否正確註解。當我添加:

for (Method m : Application.class.getDeclaredMethods()) { 
    RequiredJsonValues reqs = m.getAnnotation(RequiredJsonValues.class); 
    for (RequiredJsonValue req : reqs.value()) { 
     System.out.println("Method: " + m + " annotation: " + req); 
    } 
} 

在方法的開始,我得到

Method: public java.util.concurrent.CompletionStage controllers.SomeController.doSomething() annotation: @util.Action2(value=someValue) 
Method: public java.util.concurrent.CompletionStage controllers.SomeController.doSomething() annotation: @util.Action2(value=someOtherValue) 

那我做錯了嗎?有沒有其他方法可以用不同的值連續多次進行同一動作?

回答

0

最後我做了它的工作

所解釋in here了Java 8的編譯器並不需要寫入的RepeatableAction2,但它在編譯時增加了,所以我需要添加該批註的實現:

@With(RepeatableAction2Impl.class) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface RepeatableAction2 { 
    Action2[] value() default {}; 
} 

,並在執行我手動鏈中的所有操作:

public class RepeatableAction2Impl extends Action<RepeatableAction2> { 

    @Override 
    public CompletionStage<Result> call(Http.Context ctx) { 
     if (configuration.value().length > 0) { 
      int actions = configuration.value().length; 
      List<Action<Action2>> actionList = new ArrayList<>(); 
      // Create actions 
      for (int i = 0; i < actions; i++) { 
       Action2Impl action2Impl = new Action2Impl(); 
       action2Impl.configuration = configuration.value()[i]; 
       actionList.add(action2Impl); 
      } 
      // Chaining 
      actionList.get(actions - 1).delegate = delegate; 
      for (int i = 0; i < actions - 1; i++) { 
       actionList.get(i).delegate = actionList.get(i + 1); 
      } 
      // Delegate the work to actions 
      return actionList.get(0).call(ctx); 
     } else { 
      return delegate.call(ctx); 
     } 
    } 

} 

雖然這是一個可行的解決方案是STIL我對我來說似乎有點難看。有沒有其他方式我錯過了?