Q:

How do I allow immutable classes when the VisibilityModifier checkstyle module is enabled?

I work with a Java codebase that have strict Checkstyle checks, and it doesn't like the following immutable value object:
public class Foo {
    public final int bar;
    public final String baz;

    public Foo(int bar, String baz) {
        this.bar = bar;
        this.baz = baz;
    }
}

This is because the VisibilityModifier module forces instance variables to be private and have getters.  

Is there a way to make Checkstyle make an exception for instance variables that are public final?
If so, is there a way to make this exception only apply to test classes?

A:

I hate to tell you this, and I know it may not be the answer you were hoping for, but:
It really is not possible out of the box, because the behavior is hardcoded in the check. Excerpt from the Checkstyle source code:
if (!("private".equals(variableScope)
    || inInterfaceOrAnnotationBlock // implicitly static and final
    || (mods.contains("static") && mods.contains("final"))
    || ("package".equals(variableScope) && isPackageAllowed())
    || ("protected".equals(variableScope) && isProtectedAllowed())
    || ("public".equals(variableScope)
        && getPublicMemberRegexp().matcher(varName).find())))
{
    log(varNameAST.getLineNo(), varNameAST.getColumnNo(),
        "variable.notPrivate", varName);
}

You would have to write your own check, possibly as a subclass of VisibilityModifierCheck which overrides the method visitToken() in order to allow your exception.
On another note, if your class Foo is an inner class, then you can also simply declare the fields private and still access them from the outer class. Sort of a special case, but often one where the getters and setters are particularly annoying.

