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.