Java 在Play Framework中执行合成操作时重复相同的操作

Java 在Play Framework中执行合成操作时重复相同的操作,java,playframework,annotations,playframework-2.5,Java,Playframework,Annotations,Playframework 2.5,我正在使用Play框架开发一个服务器。在我的一些方法中,我需要执行一些以前的操作(基本上是输入检查),所以我认为最好的方法是 我可以毫无问题地使用几个注释 @Action1 // <---------------------------------------- This action is executed @Action2(value = "someValue") // <------------------- This action is executed public Com

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

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

@Action1 // <---------------------------------------- This action is executed
@Action2(value = "someValue") // <------------------- This action is executed
public CompletionStage<Result> doSomething() {
    ...
}
Action2
看起来是这样的:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatableAction2 {
    Action2[] value() default {};
}
@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)
那么我做错了什么?有没有其他方法可以用不同的值多次链接相同的操作?

最后我成功了

如前所述,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);
        }
    }

}
公共类RepeatableAction2Impl扩展了操作{
@凌驾
公共CompletionStage调用(Http.Context ctx){
if(configuration.value().length>0){
int actions=configuration.value().length;
List actionList=新建ArrayList();
//创建操作
for(int i=0;i
虽然这是一个有效的解决方案,但对我来说还是有点难看。我还有其他的路要走吗

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);
        }
    }

}