-1

我正在使用 Play Framework 开发服务器。在我的几种方法中,我需要执行一些先前的操作(基本上是输入检查),所以我认为最好的方法是Action Composition

我可以毫无问题地使用多个注释

@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 示例VerboseAnnotation的注释,所以我认为不值得在这里写。由于我的注释可以重复,我声明了这样的注释:Action2RepeatableAction2

@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)

那么我做错了什么?有没有其他方法可以用不同的值多次链接相同的操作?

4

1 回答 1

0

最后我让它工作了

正如此处解释的那样,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);
        }
    }

}

虽然这是一个可行的解决方案,但对我来说仍然有点难看。还有其他我想念的方式吗?

于 2016-04-22T17:44:19.130 回答