Class MappedReservedWords
- java.lang.Object
-
- software.amazon.smithy.codegen.core.MappedReservedWords
-
- All Implemented Interfaces:
ReservedWords
public final class MappedReservedWords extends java.lang.Object implements ReservedWords
A reserved words implementation that maps known words to other words.The following example shows how to use this class to make reserved words safe for the targeted code:
ReservedWords reserved = MappedReservedWords.builder() .put("exception", "apiException") .put("void", "void_") .build(); String safeWord = reserved.escape("exception"); System.out.println(safeWord); // outputs "apiException"
The detection of reserved words can be made case-insensitive such that "bar", "BAR", "Bar", etc., can be detected as reserved words.
ReservedWords reserved = MappedReservedWords.builder() .put("foo", "Hi") .putCaseInsensitive("bar", "bam") .build(); assert(reserved.escape("foo").equals("Hi")); assert(reserved.escape("Foo").equals("Foo")); assert(reserved.escape("BAR").equals("bam"));
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MappedReservedWords.Builder
Builder to create a newMappedReservedWords
instance.
-
Constructor Summary
Constructors Constructor Description MappedReservedWords(java.util.Map<java.lang.String,java.lang.String> mappings, java.util.Map<java.lang.String,java.lang.String> caseInsensitiveMappings)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MappedReservedWords.Builder
builder()
java.lang.String
escape(java.lang.String word)
Escapes a reserved word.boolean
isReserved(java.lang.String word)
Checks if the given word is reserved.
-
-
-
Constructor Detail
-
MappedReservedWords
public MappedReservedWords(java.util.Map<java.lang.String,java.lang.String> mappings, java.util.Map<java.lang.String,java.lang.String> caseInsensitiveMappings)
- Parameters:
mappings
- Map of reserved word to replacement words.caseInsensitiveMappings
- Map of case-insensitive reserved word to replacement words.
-
-
Method Detail
-
builder
public static MappedReservedWords.Builder builder()
- Returns:
- Creates a new Builder.
-
escape
public java.lang.String escape(java.lang.String word)
Description copied from interface:ReservedWords
Escapes a reserved word.- Specified by:
escape
in interfaceReservedWords
- Parameters:
word
- Word to escape.- Returns:
- Returns the converted value.
-
isReserved
public boolean isReserved(java.lang.String word)
Description copied from interface:ReservedWords
Checks if the given word is reserved.- Specified by:
isReserved
in interfaceReservedWords
- Parameters:
word
- Word to check.- Returns:
- Returns true if the word is reserved.
-
-