AvoidEscapedUnicodeCharacters

Since Checkstyle 5.8

Description

Restricts using Unicode escapes (such as \u221e). It is possible to allow using escapes for non-printable, control characters. Also, this check can be configured to allow using escapes if trail comment is present. By the option it is possible to allow using escapes if literal contains only them.

Properties

name description type default value since
allowByTailComment Allow use escapes if trail comment is present. boolean false 5.8
allowEscapesForControlCharacters Allow use escapes for non-printable, control characters. boolean false 5.8
allowIfAllCharactersEscaped Allow if all characters in literal are escaped. boolean false 5.8
allowNonPrintableEscapes Allow use escapes for non-printable, whitespace characters. boolean false 5.8

Examples

To configure the check:

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

Examples of using Unicode:

public class Example1 {
  // OK, perfectly clear even without a comment.
  String unitAbbrev = "μs";
  // violation below, the reader has no idea what this is. 'should be avoided.'
  String unitAbbrev1 = "\u03bcs";
  // violation below
  String unitAbbrev2 = "\u03bc\u03bc\u03bc";
  // violation below
  String unitAbbrev3 = "\u03bcs"; // it is  μs
  // violation below
  String unitAbbrev4 = "\u03bc\u03bcs";
  public static int content() {
    char content = 'r';
    // violation below
    return '\ufeff' + content;
  }
}
        

An example of how to configure the check to allow using escapes for non-printable, control characters:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AvoidEscapedUnicodeCharacters">
      <property name="allowEscapesForControlCharacters" value="true"/>
    </module>
  </module>
</module>
        

Example of using escapes for non-printable, control characters:

public class Example2 {
  // OK, a normal String below
  String unitAbbrev = "μs";
  // violation below, μs is a printable character. 'should be avoided.'
  String unitAbbrev1 = "\u03bcs";
  // violation below
  String unitAbbrev2 = "\u03bc\u03bc\u03bc";
  // violation below
  String unitAbbrev3 = "\u03bcs";
  // violation below
  String unitAbbrev4 = "\u03bc\u03bcs";
  public static int content() {
    char content = 'r';
    // OK, non-printable control character.
    return '\ufeff' + content;
  }
}
        

An example of how to configure the check to allow using escapes if trail comment is present:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AvoidEscapedUnicodeCharacters">
      <property name="allowByTailComment" value="true"/>
    </module>
  </module>
</module>
        

Example of using escapes if trail comment is present:

public class Example3 {
  // OK, a normal String below
  String unitAbbrev = "μs";
  // violation below
  String unitAbbrev1 = "\u03bcs";
  // violation below
  String unitAbbrev2 = "\u03bc\u03bc\u03bc";
  // violation below
  String unitAbbrev3 = "\u03bcs";
  // ok, because there is trailing comment and allowByTailComment=true
  String unitAbbrev4 = "\u03bc\u03bcs"; // it is  μs
  public static int content() {
    char content = 'r';
    // violation below
    return '\ufeff' + content;
  }
}
        

An example of how to configure the check to allow if all characters in literal are escaped.

<module name="Checker">
  <module name="TreeWalker">
    <module name="AvoidEscapedUnicodeCharacters">
      <property name="allowIfAllCharactersEscaped" value="true"/>
    </module>
  </module>
</module>
        

Example of using escapes if all characters in literal are escaped:

public class Example4 {
  // OK, a normal String below
  String unitAbbrev = "μs";
  // violation below, not all characters are escaped ('s'). 'should be avoided.'
  String unitAbbrev1 = "\u03bcs";
  // ok, because below are escape characters and allowIfAllCharacters = true.
  String unitAbbrev2 = "\u03bc\u03bc\u03bc";
  // violation below
  String unitAbbrev3 = "\u03bcs"; // it is  μs
  // violation below, not all characters are escaped ('s'). 'should be avoided.'
  String unitAbbrev4 = "\u03bc\u03bcs";
  public static int content() {
    char content = 'r';
    // OK, all control characters are escaped below
    return '\ufeff' + content;
  }
}
        

An example of how to configure the check to allow using escapes for non-printable whitespace characters:

<module name="Checker">
  <module name="TreeWalker">
    <module name="AvoidEscapedUnicodeCharacters">
      <property name="allowNonPrintableEscapes" value="true"/>
    </module>
  </module>
</module>
        

Example of using escapes for non-printable whitespace characters:

public class Example5 {
  // OK, a normal String below.
  String unitAbbrev = "μs";
  // violation below, printable escape character. 'should be avoided.'
  String unitAbbrev1 = "\u03bcs";
  // violation below, printable escape character. 'should be avoided.'
  String unitAbbrev2 = "\u03bc\u03bc\u03bc";
  // violation below
  String unitAbbrev3 = "\u03bcs"; // it is μs
  // violation below
  String unitAbbrev4 = "\u03bc\u03bcs";
  public static int content() {
    char content = 'r';
    // OK, non-printable escape character below
    return '\ufeff' + content;
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker