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 String
escapeChar
(char ch) Escapes the provided character so that it's a valid Java identifier character.static @NotNull String
getJavaIdentifier
(@NotNull String identifier) Converts the given identifier to a legal Java identifier.static boolean
isJavaKeyword
(@NotNull String key) Test whether the argument is a Java keyword, according to the Java Language Specification.static boolean
isJavaLiteral
(@NotNull String key) Test whether the argument is a Java literal, according to the Java Language Specification..static boolean
isSpecialIdentifier
(@NotNull String key) Test whether the argument is a special identifier, according to the Java Language Specification.static @NotNull String
makeJavaPackage
(@NotNull String path) Converts the givenpath
to a Java package or fully-qualified class name, depending on thepath
's value.static char
Returns the original character that was escaped, given anescapeSequence
.static @NotNull String
unescapeAll
(@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
. TheescapeSequence
has to match the(__[0-9a-f]{4}__)
pattern.- Parameters:
escapeSequence
- the escaped string- Returns:
- the original character
- Throws:
IllegalArgumentException
- if theescaped
string 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 givenpath
to 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:
true
if the String is a Java keyword,false
otherwise
-
isJavaLiteral
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
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
-