Class Refaster


  • public class Refaster
    extends Object
    Static utilities to indicate special handling in Refaster templates.
    Author:
    [email protected] (Louis Wasserman)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T anyOf​(T... expressions)
      Indicates that Refaster should attempt to match a target expression against each of the specified template expressions, in order, and succeed at the first match.
      static <T> T[] asVarargs​(T arg)
      Indicates that Refaster should treat this @Repeated argument specifically as a varargs argument.
      static <T> Class<T> clazz()
      This is a placeholder for the expression T.class.
      static void emitComment​(String literal)
      This is a special method to emit a one-line comment.
      static <T> T emitCommentBefore​(String literal, T expression)
      This is a special method to emit a comment before an expression.
      static <E extends Enum<E>>
      E
      enumValueOf​(String string)
      This is a placeholder for the expression E.valueOf(string).
      static <T> boolean isInstance​(Object o)
      This is a placeholder for the Java instanceof operator that can be used with Refaster type variables.
      static <T> T[] newArray​(int size)
      This is a placeholder for new T[size].
    • Method Detail

      • asVarargs

        public static <T> T[] asVarargs​(T arg)
        Indicates that Refaster should treat this @Repeated argument specifically as a varargs argument.

        For example, you might write

        
            @BeforeTemplate void something(@Repeated T arg) {
             Stream.of(asVarargs(arg)); // doesn't use the Stream.of(T) overload, but Stream.of(T...)
            }
         
      • anyOf

        @SafeVarargs
        public static <T> T anyOf​(T... expressions)
        Indicates that Refaster should attempt to match a target expression against each of the specified template expressions, in order, and succeed at the first match.

        This method should only be used in Refaster templates, but should never actually be run.

        For example, instead of writing

        
         @BeforeTemplate <E> List<E> copyOfSingleton(E element) {
           return ImmutableList.copyOf(Collections.singletonList(element));
         }
         @BeforeTemplate <E> List<E> copyOfArrayList(E element) {
           return ImmutableList.copyOf(Lists.newArrayList(element));
         }
         

        one could alternately write

        
         @BeforeTemplate <E> List<E> singleton(E element) {
           return ImmutableList.copyOf(Refaster.anyOf(
             Collections.singletonList(element),
             Lists.newArrayList(element)));
         }
         
      • isInstance

        public static <T> boolean isInstance​(Object o)
        This is a placeholder for the Java instanceof operator that can be used with Refaster type variables. The type argument must always be specified explicitly, e.g. Refaster.<String>isInstance(o).

        For example, instead of writing the broken

        
         @AfterTemplate <T> boolean instanceOf(Object o) {
           return o instanceof T; // you want to match this, but it won't compile
         }
         

        you would instead write

        
         @AfterTemplate <T> boolean instanceOf(Object o) {
           return Refaster.<T>isInstance(o); // generates the replacement "o instanceof T"
         }
         
        Throws:
        IllegalArgumentException - if T is not specified explicitly.
      • newArray

        public static <T> T[] newArray​(int size)
        This is a placeholder for new T[size]. The type argument must always be specified explicitly, e.g. Refaster.<String>newArray(10).

        For example, instead of writing the broken

        
         @AfterTemplate <T> T[] newTArray(int size) {
           return new T[size]; // you want to generate this code, but it won't compile
         }
         

        you would instead write

        
         @AfterTemplate <T> T[] newTArray(int size) {
           return Refaster.<T>newArray(size);
         }
         
        Throws:
        IllegalArgumentException - if T is not specified explicitly.
      • clazz

        public static <T> Class<T> clazz()
        This is a placeholder for the expression T.class. The type argument must always be specified explicitly, e.g. Refaster.<String>clazz().

        For example, instead of writing the broken

        
         @AfterTemplate <T> T[] getEnumConstants() {
           return T.class.getEnumConstants(); // you want to inline this, but it won't compile
         }
         
        you would instead write
        
         @AfterTemplate <T> T[] getEnumConstants() {
           return Refaster.<T>clazz().getEnumConstants();
         }
         
        Throws:
        IllegalArgumentException - if T is not specified explicitly.
      • enumValueOf

        public static <E extends Enum<E>> E enumValueOf​(String string)
        This is a placeholder for the expression E.valueOf(string). The type argument must always be specified explicitly, e.g. Refaster.<RoundingMode>valueOf(string).

        For example, instead of writing the broken

        
         @BeforeTemplate <E extends Enum<E>> E valueOf(String str) {
           return E.valueOf(str);
         }
         

        you would instead write

        
         @BeforeTemplate <E extends Enum<E>> E valueOf(String str) {
           return Refaster.<E>enumValueOf(str);
         }
         
        Throws:
        IllegalArgumentException - if E is not specified explicitly.
      • emitCommentBefore

        public static <T> T emitCommentBefore​(String literal,
                                              T expression)
        This is a special method to emit a comment before an expression. The comment argument must always be a string literal. This method cannot be static imported.

        For example, instead of writing

        
         @AfterTemplate int lengthWithComment(String str) {
           return /* comment \*\/ str.length();
         }
         

        you would instead write

        
         @AfterTemplate int lengthWithComment(String str) {
           return Refaster.emitCommentBefore("comment", str.length());
         }
         
      • emitComment

        public static void emitComment​(String literal)
        This is a special method to emit a one-line comment. The comment argument must always be a string literal. This method cannot be static imported.

        For example, instead of writing

        
         @AfterTemplate void printWithComment(String str) {
           // comment
           System.out.println(str);
         }
         

        you would instead write

        
         @AfterTemplate void printWithComment(String str) {
           Refaster.emitComment("comment");
           System.out.println(str);
         }