support for @Replace (with markers)

This commit is contained in:
Renaud Pawlak 2017-03-17 13:56:49 +01:00
parent ce52c5ce1c
commit 77d384751c
6 changed files with 79 additions and 25 deletions

View File

@ -238,7 +238,7 @@ public abstract class JSweetConfig {
public static final String ANNOTATION_ROOT = JSweetConfig.LANG_PACKAGE + ".Root";
public static final String ANNOTATION_NAME = JSweetConfig.LANG_PACKAGE + ".Name";
public static final String ANNOTATION_DECORATOR = JSweetConfig.LANG_PACKAGE + ".Decorator";
public static final String ANNOTATION_TYPE_SCRIPT_BODY = JSweetConfig.LANG_PACKAGE + ".TypeScriptBody";
public static final String ANNOTATION_REPLACE = JSweetConfig.LANG_PACKAGE + ".Replace";
public static final String ANNOTATION_FUNCTIONAL_INTERFACE = FunctionalInterface.class.getName();
/**

View File

@ -147,6 +147,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#";
protected static Logger logger = Logger.getLogger(Java2TypeScriptTranslator.class);
@ -2021,11 +2026,13 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} else {
print(" ").print("{").println().startIndent();
if (context.hasAnnotationType(methodDecl.sym, JSweetConfig.ANNOTATION_TYPE_SCRIPT_BODY)) {
String replacedBody = (String) context.getAnnotationValue(methodDecl.sym,
JSweetConfig.ANNOTATION_TYPE_SCRIPT_BODY, null);
printIndent().print(replacedBody).println();
} else {
String replacedBody = null;
if (context.hasAnnotationType(methodDecl.sym, JSweetConfig.ANNOTATION_REPLACE)) {
replacedBody = (String) context.getAnnotationValue(methodDecl.sym,
JSweetConfig.ANNOTATION_REPLACE, null);
}
int position = getCurrentPosition();
if (replacedBody == null || replacedBody.contains(BODY_MARKER)) {
enter(methodDecl.getBody());
if (!methodDecl.getBody().stats.isEmpty()
&& methodDecl.getBody().stats.head.toString().startsWith("super(")) {
@ -2041,6 +2048,17 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
printBlockStatements(methodDecl.getBody().stats);
}
exit();
if (replacedBody != null) {
String orgBody = getOutput().substring(position);
removeLastChars(getCurrentPosition() - position);
replacedBody = replacedBody.replace(BODY_MARKER, orgBody)
.replace(BASE_INDENT_MARKER, getIndentString()).replace(INDENT_MARKER, INDENT)
.replace(METHOD_NAME_MARKER, methodDecl.getName().toString())
.replace(CLASS_NAME_MARKER, parent.sym.getQualifiedName().toString());
}
}
if (replacedBody != null) {
printIndent().print(replacedBody).println();
}
endIndent().printIndent().print("}");
}
@ -2148,11 +2166,13 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
}
if (context.hasAnnotationType(method.sym, JSweetConfig.ANNOTATION_TYPE_SCRIPT_BODY)) {
String replacedBody = (String) context.getAnnotationValue(method.sym,
JSweetConfig.ANNOTATION_TYPE_SCRIPT_BODY, null);
printIndent().print(replacedBody).println();
} else {
String replacedBody = null;
if (context.hasAnnotationType(method.sym, JSweetConfig.ANNOTATION_REPLACE)) {
replacedBody = (String) context.getAnnotationValue(method.sym, JSweetConfig.ANNOTATION_REPLACE,
null);
}
int position = getCurrentPosition();
if (replacedBody == null || replacedBody.contains(BODY_MARKER)) {
enter(method.getBody());
com.sun.tools.javac.util.List<JCStatement> stats = skipFirst ? method.getBody().stats.tail
: method.getBody().stats;
@ -2184,6 +2204,19 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
}
exit();
if (replacedBody != null) {
getIndent();
printIndent();
String orgBody = getOutput().substring(position);
removeLastChars(getCurrentPosition() - position);
replacedBody = replacedBody.replace(BODY_MARKER, orgBody)
.replace(BASE_INDENT_MARKER, getIndentString()).replace(INDENT_MARKER, INDENT)
.replace(METHOD_NAME_MARKER, method.getName().toString())
.replace(CLASS_NAME_MARKER, method.sym.getEnclosingElement().getQualifiedName().toString());
}
}
if (replacedBody != null) {
printIndent().print(replacedBody).println();
}
} else {
String returnValue = Util.getTypeInitialValue(method.sym.getReturnType());

View File

@ -62,7 +62,7 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
return positionStack;
}
private static final String INDENT = " ";
protected static final String INDENT = " ";
private StringBuilder out = new StringBuilder();
@ -94,8 +94,8 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
* @param fillSourceMap
* tells if printer fills the source map
*/
public AbstractTreePrinter(TranspilationHandler logHandler, JSweetContext context, JCCompilationUnit compilationUnit,
PrinterAdapter adapter, boolean fillSourceMap) {
public AbstractTreePrinter(TranspilationHandler logHandler, JSweetContext context,
JCCompilationUnit compilationUnit, PrinterAdapter adapter, boolean fillSourceMap) {
super(logHandler, context, compilationUnit);
this.typeChecker = new TypeChecker(this);
this.adapter = adapter;
@ -118,14 +118,14 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
return this;
}
// /**
// * Print a given AST.
// */
// public AbstractTreePrinter print(ExtendedElement element) {
// scan(element);
// return this;
// }
// /**
// * Print a given AST.
// */
// public AbstractTreePrinter print(ExtendedElement element) {
// scan(element);
// return this;
// }
/**
* Enters the given tree (se {@link #scan(JCTree)}.
*/
@ -201,6 +201,17 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
return this;
}
/**
* Returns the current indentation as a string.
*/
public String getIndentString() {
String indentString = "";
for (int i = 0; i < indent; i++) {
indentString += INDENT;
}
return indentString;
}
/**
* Increments the current indentation value.
*/
@ -367,7 +378,7 @@ public abstract class AbstractTreePrinter extends AbstractTreeScanner {
public abstract AbstractTreePrinter printConstructorArgList(JCNewClass newClass, boolean localClass);
protected abstract AbstractTreePrinter substituteAndPrintType(JCTree typeTree);
/**
* Prints a comma-separated list of variable names (no types).
*/

View File

@ -54,6 +54,8 @@ public class Maps {
m.clear();
trace.push("[" + m.values() + "]");
trace.push("-" + m.get("undefinedKey") + "-");
$export("trace", trace.join(","));

View File

@ -44,4 +44,12 @@ public class NativeStringBuilder {
}
StringBuilder sb2 = new StringBuilder();
public void test(char[] c, int i, int l) {
sb2.append(c);
sb2.append(c, i, l);
}
}

View File

@ -2,7 +2,7 @@ package source.structural;
import static jsweet.util.Globals.$export;
import jsweet.lang.TypeScriptBody;
import jsweet.lang.Replace;
public class TypeScriptBodyAnnotation {
@ -14,7 +14,7 @@ public class TypeScriptBodyAnnotation {
int i = 1;
@TypeScriptBody("return this.i + 1;")
@Replace("return this.i + 1;")
public int m1() {
return i;
}