String Literal Encoding

Checks that all String and character literals contain only chars of a given encoding.

Name Description Default
encoding The allowed encoding within String and character literals. US-ASCII

The check will signal the use of characters in a different encoding. E.g. if you choose to only allow characters in ASCII encoding, the following will raise a warning, since there is a German umlaut.

String s = "Viel Glück!"

The properly encoded character (the 'ü' is replaced by '\u00fc') will not be signaled.

String s = "Viel Gl\u00fcck!"

Please consider using AnyEdit tools plugin for Eclipse to help convert characters of a different encoding the Unicode notation.

Example Rule Configurations

Please note to add the rule below the TreeWalker rule.

Using encoding ASCII encoding (the default).

<module name="Checker">
  <module name="TreeWalker">
    <module name="StringLiteralEncoding"/>
  </module>
</module>

Using encoding ISO-8859-1.

<module name="Checker">
  <module name="TreeWalker">
    <module name="StringLiteralEncoding">
      <property name="encoding" value="ISO-8859-1"/>
    </module>
  </module>
</module>

Alternative regexp rule

If you just want to check that there are no non-ASCII characters within a Java file, excluding comments, use the following:

<module name="RegexpSinglelineJava">
  <property name="format" value="[^\x00-\x7F]" />
  <property name="message" value="Line contains non-ASCII char." />
  <property name="ignoreComments" value="true" />
</module>

While the StringLiteralEncoding rule only checks String literals for characters outside a given encoding, the RegexpSinglelineJava rule above checks for any non-ASCII character outside a comment. The RegexpSinglelineJava rule is provided in any Checkstyle configuration. You won't need to include the smartics checkstyle rules library.