Class JavaEscapeHelper
java.lang.Object
org.apache.sling.commons.compiler.source.JavaEscapeHelper
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 Summary
Modifier and TypeMethodDescriptionstatic @NotNull StringescapeChar(char ch) Escapes the provided character so that it's a valid Java identifier character.static @NotNull StringgetJavaIdentifier(@NotNull String identifier) Converts the given identifier to a legal Java identifier.static booleanisJavaKeyword(@NotNull String key) Test whether the argument is a Java keyword, according to the Java Language Specification.static booleanisJavaLiteral(@NotNull String key) Test whether the argument is a Java literal, according to the Java Language Specification..static booleanisSpecialIdentifier(@NotNull String key) Test whether the argument is a special identifier, according to the Java Language Specification.static @NotNull StringmakeJavaPackage(@NotNull String path) Converts the givenpathto a Java package or fully-qualified class name, depending on thepath's value.static charReturns the original character that was escaped, given anescapeSequence.static @NotNull StringunescapeAll(@NotNull String input) Provided a string which could contain escape sequences generated throughescapeChar(char), this method will unescape all such sequences.
-
Method Details
-
getJavaIdentifier
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
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
Returns the original character that was escaped, given anescapeSequence. TheescapeSequencehas to match the(__[0-9a-f]{4}__)pattern.- Parameters:
escapeSequence- the escaped string- Returns:
- the original character
- Throws:
IllegalArgumentException- if theescapedstring does not match the (__[0-9a-f]{4}__) pattern
-
unescapeAll
Provided a string which could contain escape sequences generated throughescapeChar(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
Converts the givenpathto a Java package or fully-qualified class name, depending on thepath's value.- Parameters:
path- the path to convert- Returns:
- Java package / fully-qualified class name corresponding to the given path
-
isJavaKeyword
Test whether the argument is a Java keyword, according to the Java Language Specification.- Parameters:
key- the String to test- Returns:
trueif the String is a Java keyword,falseotherwise
-
isJavaLiteral
Test whether the argument is a Java literal, according to the Java Language Specification..- Parameters:
key- the String to test- Returns:
trueif the String is a Java literal,falseotherwise
-
isSpecialIdentifier
Test whether the argument is a special identifier, according to the Java Language Specification.- Parameters:
key- the String to test- Returns:
trueif the String is a Java special identifier,falseotherwise
-