Class JavaEscapeHelper

java.lang.Object
org.apache.sling.commons.compiler.source.JavaEscapeHelper

public final class JavaEscapeHelper extends Object
The JavaEscapeHelper class provides various methods which can be used to generate valid Java identifiers or package / class names, especially when generating them from a transcompiler.
  • Method Details

    • getJavaIdentifier

      @NotNull public static @NotNull String getJavaIdentifier(@NotNull @NotNull String identifier)
      Converts the given identifier to a legal Java identifier.
      Parameters:
      identifier - the identifier to convert
      Returns:
      legal Java identifier corresponding to the given identifier
    • escapeChar

      @NotNull public static @NotNull String escapeChar(char ch)
      Escapes the provided character so that it's a valid Java identifier character. This method does not check if the provided character is valid and will escape any character into a sequence matching the (__[0-9a-f]{4}__) pattern, by using the character's decimal code and converting it to hexadecimal.
      Parameters:
      ch - the character to escape
      Returns:
      the escaped character representation
    • unescape

      public static char unescape(@NotNull @NotNull String escapeSequence)
      Returns the original character that was escaped, given an escapeSequence. The escapeSequence has to match the (__[0-9a-f]{4}__) pattern.
      Parameters:
      escapeSequence - the escaped string
      Returns:
      the original character
      Throws:
      IllegalArgumentException - if the escaped string does not match the (__[0-9a-f]{4}__) pattern
    • unescapeAll

      @NotNull public static @NotNull String unescapeAll(@NotNull @NotNull String input)
      Provided a string which could contain escape sequences generated through escapeChar(char), this method will unescape all such sequences.
      Parameters:
      input - a string containing escaped characters
      Returns:
      a string with all escaped sequences produced by escapeChar(char) replaced by the original character
    • makeJavaPackage

      @NotNull public static @NotNull String makeJavaPackage(@NotNull @NotNull String path)
      Converts the given path to a Java package or fully-qualified class name, depending on the path's value.
      Parameters:
      path - the path to convert
      Returns:
      Java package / fully-qualified class name corresponding to the given path
    • isJavaKeyword

      public static boolean isJavaKeyword(@NotNull @NotNull String key)
      Test whether the argument is a Java keyword, according to the Java Language Specification.
      Parameters:
      key - the String to test
      Returns:
      true if the String is a Java keyword, false otherwise
    • isJavaLiteral

      public static boolean isJavaLiteral(@NotNull @NotNull String key)
      Test whether the argument is a Java literal, according to the Java Language Specification..
      Parameters:
      key - the String to test
      Returns:
      true if the String is a Java literal, false otherwise
    • isSpecialIdentifier

      public static boolean isSpecialIdentifier(@NotNull @NotNull String key)
      Test whether the argument is a special identifier, according to the Java Language Specification.
      Parameters:
      key - the String to test
      Returns:
      true if the String is a Java special identifier, false otherwise