@Replace now uses a better-looking mustache-like syntax

This commit is contained in:
Renaud Pawlak 2017-05-11 09:52:57 +02:00
parent 6341b398d6
commit 244e2112e1
4 changed files with 52 additions and 16 deletions

View File

@ -30,6 +30,24 @@ import java.lang.annotation.Target;
* The annotation's value contains TypeScript which is generated as is by the
* JSweet transpiler. The code will be checked by the TypeScript transpiler.
*
* <p>
* The replacing code can contain variables substituted using a mustache-like
* convention ({{variableName}}). Note that no space is allowed between the
* variable name and the mustaches. Here is the list of supported variables:
*
* <ul>
* <li>{{className}}: the current class.</li>
* <li>{{methodName}}: the current method name.</li>
* <li>{{body}}: the body of the current method. A typical use of this variable
* is to wrap the original behavior in a lambda. For instance:
* <code>[before code] let _result = () => { {{body}} }(); [after code] return result;</code>
* </li>
* <li>{{baseIndent}}: the indentation of the replaced method. Can be used to
* generate well-formatted code.</li>
* <li>{{indent}}: substituted with an indentation. Can be used to generate
* well-formatted code.</li>
* </ul>
*
* @author Renaud Pawlak
*/
@Retention(RetentionPolicy.RUNTIME)
@ -37,10 +55,10 @@ import java.lang.annotation.Target;
@Documented
public @interface Replace {
/**
* The code that will be generated by the transpiler in place of the
* annotated method body.
*/
java.lang.String value();
/**
* The code that will be generated by the transpiler in place of the
* annotated method body.
*/
java.lang.String value();
}

View File

@ -30,6 +30,24 @@ import java.lang.annotation.Target;
* The annotation's value contains TypeScript which is generated as is by the
* JSweet transpiler. The code will be checked by the TypeScript transpiler.
*
* <p>
* The replacing code can contain variables substituted using a mustache-like
* convention ({{variableName}}). Note that no space is allowed between the
* variable name and the mustaches. Here is the list of supported variables:
*
* <ul>
* <li>{{className}}: the current class.</li>
* <li>{{methodName}}: the current method name.</li>
* <li>{{body}}: the body of the current method. A typical use of this variable
* is to wrap the original behavior in a lambda. For instance:
* <code>[before code] let _result = () => { {{body}} }(); [after code] return result;</code>
* </li>
* <li>{{baseIndent}}: the indentation of the replaced method. Can be used to
* generate well-formatted code.</li>
* <li>{{indent}}: substituted with an indentation. Can be used to generate
* well-formatted code.</li>
* </ul>
*
* @author Renaud Pawlak
*/
@Retention(RetentionPolicy.RUNTIME)
@ -37,10 +55,10 @@ import java.lang.annotation.Target;
@Documented
public @interface Replace {
/**
* The code that will be generated by the transpiler in place of the
* annotated method body.
*/
java.lang.String value();
/**
* The code that will be generated by the transpiler in place of the
* annotated method body.
*/
java.lang.String value();
}

View File

@ -160,11 +160,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
public static final String ENUM_WRAPPER_CLASS_NAME = "_$name";
public static final String ENUM_WRAPPER_CLASS_ORDINAL = "_$ordinal";
public static final String VAR_DECL_KEYWORD = "let";
public static final String BODY_MARKER = "#BODY#";
public static final String BASE_INDENT_MARKER = "#BASEINDENT#";
public static final String INDENT_MARKER = "#INDENT#";
public static final String METHOD_NAME_MARKER = "#METHODNAME#";
public static final String CLASS_NAME_MARKER = "#CLASSNAME#";
public static final String BODY_MARKER = "{{body}}";
public static final String BASE_INDENT_MARKER = "{{baseIndent}}";
public static final String INDENT_MARKER = "{{indent}}";
public static final String METHOD_NAME_MARKER = "{{methodName}}";
public static final String CLASS_NAME_MARKER = "{{className}}";
public static final String GENERATOR_PREFIX = "__generator_";
protected static Logger logger = Logger.getLogger(Java2TypeScriptTranslator.class);

View File

@ -331,7 +331,7 @@ public class StructuralTests extends AbstractTest {
context.addAnnotation("@Replace('return (this.i + 2)')",
"source.structural.ReplaceAnnotation.m2()");
context.addAnnotation(
"@Replace('let result = (() => { #BODY# })(); return (result + '#METHODNAME#'.length);')",
"@Replace('let result = (() => { {{body}} })(); return (result + '{{methodName}}'.length);')",
"source.structural.ReplaceAnnotation.m4()");
}
};