Class MappedReservedWords

java.lang.Object
software.amazon.smithy.codegen.core.MappedReservedWords
All Implemented Interfaces:
ReservedWords

public final class MappedReservedWords extends 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"));
 
 
  • Constructor Details

    • MappedReservedWords

      public MappedReservedWords(Map<String,String> mappings, Map<String,String> caseInsensitiveMappings)
      Parameters:
      mappings - Map of reserved word to replacement words.
      caseInsensitiveMappings - Map of case-insensitive reserved word to replacement words.
  • Method Details

    • builder

      public static MappedReservedWords.Builder builder()
      Returns:
      Creates a new Builder.
    • escape

      public String escape(String word)
      Description copied from interface: ReservedWords
      Escapes a reserved word.
      Specified by:
      escape in interface ReservedWords
      Parameters:
      word - Word to escape.
      Returns:
      Returns the converted value.
    • isReserved

      public boolean isReserved(String word)
      Description copied from interface: ReservedWords
      Checks if the given word is reserved.
      Specified by:
      isReserved in interface ReservedWords
      Parameters:
      word - Word to check.
      Returns:
      Returns true if the word is reserved.