How to avoid Checkstyle's FinalClass module to complain when there are inner classes inheriting from base?

471 views Asked by At

Here is a simplified scenario of my real model class to highlight the issue.

public class NullableModel {
    public static final NullableModel NULL = new NullableModel() {

        @Override
        public boolean isNull() {
            return true;
        }
    };

    public static NullableModel withValues(Object... values) {
        // some checks that sets 'allValid' boolean depending if value is OK
        boolean allValid = ...
        return allValid? new NullableModel(values) : NULL;
    }

    private Object[] values;

    private NullableModel(Object... values) {
        this.values = values;
    }

    public boolean isNull() {
        return false;
    }

    ...
}

If I did not miss anything, this code compiles properly. If I run checkstyle here with FinalClass module enabled it will complain because NullableModel is not final (while all constructors are private). But if I make final NullableModel, it will give me a compilation error in the NULL static final declaration as nothing can inherit from NullableModel, because is final.

The FinalClass check is valid for lot of other parts of my project, and this is just the exception. So, as I cannot make it final, then the question is, how do I avoid the checkstyle error here without disabling the FinalClass check?

Thanks in advance

0

There are 0 answers