Class JavaEscapeHelper
- java.lang.Object
-
- org.apache.sling.commons.compiler.source.JavaEscapeHelper
-
public final class JavaEscapeHelper extends Object
TheJavaEscapeHelper
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
All Methods Static Methods Concrete Methods Modifier and Type Method Description static @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
unescape(@NotNull String escapeSequence)
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 Detail
-
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 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
@NotNull public static @NotNull String unescapeAll(@NotNull @NotNull String input)
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
@NotNull public static @NotNull String makeJavaPackage(@NotNull @NotNull String path)
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
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
-
-