Checkstyle - 带有注释的方法必须在构造函数之前

Checkstyle - Method with annotation must be before constructor

如何定义 Checkstyle 验证以确保 所有具有特定注释的方法 出现在 java [= 的构造函数之前25=]?

验证应接受以下内容:

class User {

    @Injected // -> [OK]: method with @Injected is before the constructor. 
    public void setName(String name) {
        this.name = name;
    }

    public User(String name) {
        this.name = name;
    }
}

以下应该会导致 Checkstyle 违规:

class User {
    public User(String name) {
        this.name = name;
    }

    @Injected // -> [NOK]: method should be before the constructor
    public void setName(String name) {
        this.name = name;
    }
}

是否需要Checkstyle Check available out of the box that could be configured to check this or a custom Check implemenation来实现这个?

不,没有开箱即用的这种 Checkstyle 检查。

为此需要实现自定义 Checkstyle Check,并且必须通过将其添加到验证配置中来触发此自定义验证 xml。