mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
ability to substitute a method's body in adapter
This commit is contained in:
parent
659e715b1b
commit
cfaac45185
@ -631,9 +631,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
boolean fullImport = require || GLOBALS_CLASS_NAME.equals(targetName);
|
boolean fullImport = require || GLOBALS_CLASS_NAME.equals(targetName);
|
||||||
if (fullImport) {
|
if (fullImport) {
|
||||||
if (context.useRequireForModules) {
|
if (context.useRequireForModules) {
|
||||||
context.addHeader("import." + targetName,
|
context.addHeader("import." + targetName, "import " + targetName + " = require("
|
||||||
"import " + targetName + " = require(" + getStringLiteralQuote()
|
+ getStringLiteralQuote() + moduleName + getStringLiteralQuote() + ");\n");
|
||||||
+ moduleName + getStringLiteralQuote() + ");\n");
|
|
||||||
} else {
|
} else {
|
||||||
context.addHeader("import." + targetName,
|
context.addHeader("import." + targetName,
|
||||||
"import * as " + targetName + " from '" + moduleName + "';\n");
|
"import * as " + targetName + " from '" + moduleName + "';\n");
|
||||||
@ -1899,8 +1898,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
if (!classdecl.sym.isAnonymous()) {
|
if (!classdecl.sym.isAnonymous()) {
|
||||||
println().printIndent()
|
println().printIndent()
|
||||||
.print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name)
|
.print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name)
|
||||||
.print("[" + getStringLiteralQuote() + CLASS_NAME_IN_CONSTRUCTOR
|
.print("[" + getStringLiteralQuote() + CLASS_NAME_IN_CONSTRUCTOR + getStringLiteralQuote()
|
||||||
+ getStringLiteralQuote() + "] = ")
|
+ "] = ")
|
||||||
.print(getStringLiteralQuote() + classdecl.sym.getQualifiedName().toString()
|
.print(getStringLiteralQuote() + classdecl.sym.getQualifiedName().toString()
|
||||||
+ getStringLiteralQuote() + ";");
|
+ getStringLiteralQuote() + ";");
|
||||||
}
|
}
|
||||||
@ -1909,12 +1908,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
if (!interfaces.isEmpty()) {
|
if (!interfaces.isEmpty()) {
|
||||||
println().printIndent()
|
println().printIndent()
|
||||||
.print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name)
|
.print(getScope().enumWrapperClassScope ? classdecl.sym.getSimpleName().toString() : name)
|
||||||
.print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME
|
.print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote()
|
||||||
+ getStringLiteralQuote() + "] = ");
|
+ "] = ");
|
||||||
print("[");
|
print("[");
|
||||||
for (String itf : interfaces) {
|
for (String itf : interfaces) {
|
||||||
print(getStringLiteralQuote()).print(itf)
|
print(getStringLiteralQuote()).print(itf).print(getStringLiteralQuote() + ",");
|
||||||
.print(getStringLiteralQuote() + ",");
|
|
||||||
}
|
}
|
||||||
removeLastChar();
|
removeLastChar();
|
||||||
print("];").println();
|
print("];").println();
|
||||||
@ -2028,9 +2026,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
// end of namespace =================================================
|
// end of namespace =================================================
|
||||||
|
|
||||||
if (getScope().enumScope && getScope().isComplexEnum && !getScope().anonymousClasses.contains(classdecl)) {
|
if (getScope().enumScope && getScope().isComplexEnum && !getScope().anonymousClasses.contains(classdecl)) {
|
||||||
println().printIndent().print(classdecl.sym.getSimpleName().toString())
|
println().printIndent().print(classdecl.sym.getSimpleName().toString()).print(
|
||||||
.print("[" + getStringLiteralQuote() + ENUM_WRAPPER_CLASS_WRAPPERS
|
"[" + getStringLiteralQuote() + ENUM_WRAPPER_CLASS_WRAPPERS + getStringLiteralQuote() + "] = [");
|
||||||
+ getStringLiteralQuote() + "] = [");
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (JCTree tree : classdecl.defs) {
|
for (JCTree tree : classdecl.defs) {
|
||||||
if (tree instanceof JCVariableDecl
|
if (tree instanceof JCVariableDecl
|
||||||
@ -2052,8 +2049,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
.print("(");
|
.print("(");
|
||||||
}
|
}
|
||||||
print("" + (index++) + ", ");
|
print("" + (index++) + ", ");
|
||||||
print(getStringLiteralQuote() + varDecl.sym.name.toString()
|
print(getStringLiteralQuote() + varDecl.sym.name.toString() + getStringLiteralQuote());
|
||||||
+ getStringLiteralQuote());
|
|
||||||
if (!newClass.args.isEmpty()) {
|
if (!newClass.args.isEmpty()) {
|
||||||
print(", ");
|
print(", ");
|
||||||
}
|
}
|
||||||
@ -2119,7 +2115,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
case JSweetConfig.NEW_FUNCTION_NAME:
|
case JSweetConfig.NEW_FUNCTION_NAME:
|
||||||
return "new";
|
return "new";
|
||||||
default:
|
default:
|
||||||
if(context.hasMethodNameMapping(methodDecl.sym)) {
|
if (context.hasMethodNameMapping(methodDecl.sym)) {
|
||||||
return context.getMethodNameMapping(methodDecl.sym);
|
return context.getMethodNameMapping(methodDecl.sym);
|
||||||
} else {
|
} else {
|
||||||
return name;
|
return name;
|
||||||
@ -2485,6 +2481,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
} else {
|
} else {
|
||||||
print(" ").print("{").println().startIndent();
|
print(" ").print("{").println().startIndent();
|
||||||
|
|
||||||
|
if (!getAdapter().substituteMethodBody(parent.sym, methodDecl.sym)) {
|
||||||
|
|
||||||
String replacedBody = null;
|
String replacedBody = null;
|
||||||
if (context.hasAnnotationType(methodDecl.sym, JSweetConfig.ANNOTATION_REPLACE)) {
|
if (context.hasAnnotationType(methodDecl.sym, JSweetConfig.ANNOTATION_REPLACE)) {
|
||||||
replacedBody = (String) context.getAnnotationValue(methodDecl.sym,
|
replacedBody = (String) context.getAnnotationValue(methodDecl.sym,
|
||||||
@ -2541,6 +2539,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
}
|
}
|
||||||
printIndent().print(replacedBody).println();
|
printIndent().print(replacedBody).println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
endIndent().printIndent().print("}");
|
endIndent().printIndent().print("}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3234,7 +3235,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isLazyInitialized(VarSymbol var) {
|
private boolean isLazyInitialized(VarSymbol var) {
|
||||||
return context.options.isLazyInitializedStatics() && var.isStatic() && context.lazyInitializedStatics.contains(var)
|
return context.options.isLazyInitializedStatics() && var.isStatic()
|
||||||
|
&& context.lazyInitializedStatics.contains(var)
|
||||||
&& /* enum fields are not lazy initialized */ !(var.isEnum()
|
&& /* enum fields are not lazy initialized */ !(var.isEnum()
|
||||||
&& var.getEnclosingElement().equals(var.type.tsym));
|
&& var.getEnclosingElement().equals(var.type.tsym));
|
||||||
}
|
}
|
||||||
@ -3642,8 +3644,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
if ("class".equals(fieldAccess.name.toString())) {
|
if ("class".equals(fieldAccess.name.toString())) {
|
||||||
if (fieldAccess.type instanceof Type.ClassType
|
if (fieldAccess.type instanceof Type.ClassType
|
||||||
&& context.isInterface(((Type.ClassType) fieldAccess.type).typarams_field.head.tsym)) {
|
&& context.isInterface(((Type.ClassType) fieldAccess.type).typarams_field.head.tsym)) {
|
||||||
print(getStringLiteralQuote()).print(context
|
print(getStringLiteralQuote())
|
||||||
.getRootRelativeJavaName(((Type.ClassType) fieldAccess.type).typarams_field.head.tsym))
|
.print(context.getRootRelativeJavaName(
|
||||||
|
((Type.ClassType) fieldAccess.type).typarams_field.head.tsym))
|
||||||
.print(getStringLiteralQuote());
|
.print(getStringLiteralQuote());
|
||||||
} else {
|
} else {
|
||||||
String name = fieldAccess.selected.type.tsym.toString();
|
String name = fieldAccess.selected.type.tsym.toString();
|
||||||
@ -4519,8 +4522,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
print(", '" + INTERFACES_FIELD_NAME + "', { configurable: true, value: ");
|
print(", '" + INTERFACES_FIELD_NAME + "', { configurable: true, value: ");
|
||||||
print("[");
|
print("[");
|
||||||
for (String i : interfaces) {
|
for (String i : interfaces) {
|
||||||
print(getStringLiteralQuote()).print(i)
|
print(getStringLiteralQuote()).print(i).print(getStringLiteralQuote() + ",");
|
||||||
.print(getStringLiteralQuote() + ",");
|
|
||||||
}
|
}
|
||||||
removeLastChar();
|
removeLastChar();
|
||||||
print("]");
|
print("]");
|
||||||
@ -4781,8 +4783,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
print("for(" + VAR_DECL_KEYWORD + " " + indexVarName + "=0; " + indexVarName + " < ")
|
print("for(" + VAR_DECL_KEYWORD + " " + indexVarName + "=0; " + indexVarName + " < ")
|
||||||
.print(foreachLoop.expr).print("." + "length" + "; " + indexVarName + "++) {").println()
|
.print(foreachLoop.expr).print("." + "length" + "; " + indexVarName + "++) {").println()
|
||||||
.startIndent().printIndent();
|
.startIndent().printIndent();
|
||||||
print(VAR_DECL_KEYWORD + " " + avoidJSKeyword(foreachLoop.var.name.toString()) + " = ").print(foreachLoop.expr)
|
print(VAR_DECL_KEYWORD + " " + avoidJSKeyword(foreachLoop.var.name.toString()) + " = ")
|
||||||
.print("[" + indexVarName + "];").println();
|
.print(foreachLoop.expr).print("[" + indexVarName + "];").println();
|
||||||
} else {
|
} else {
|
||||||
String arrayVarName = "array" + Util.getId();
|
String arrayVarName = "array" + Util.getId();
|
||||||
print("{").println().startIndent().printIndent();
|
print("{").println().startIndent().printIndent();
|
||||||
@ -5363,8 +5365,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
if (caseStatement.pat instanceof JCIdent) {
|
if (caseStatement.pat instanceof JCIdent) {
|
||||||
Object value = ((VarSymbol) ((JCIdent) caseStatement.pat).sym).getConstValue();
|
Object value = ((VarSymbol) ((JCIdent) caseStatement.pat).sym).getConstValue();
|
||||||
if (context.types.isSameType(context.symtab.stringType, caseStatement.pat.type)) {
|
if (context.types.isSameType(context.symtab.stringType, caseStatement.pat.type)) {
|
||||||
print(getStringLiteralQuote() + value + getStringLiteralQuote()
|
print(getStringLiteralQuote() + value + getStringLiteralQuote() + " /* " + caseStatement.pat
|
||||||
+ " /* " + caseStatement.pat + " */");
|
+ " */");
|
||||||
} else {
|
} else {
|
||||||
print("" + value + " /* " + caseStatement.pat + " */");
|
print("" + value + " /* " + caseStatement.pat + " */");
|
||||||
}
|
}
|
||||||
@ -5802,14 +5804,13 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
print("[0]");
|
print("[0]");
|
||||||
print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME
|
print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote() + "]")
|
||||||
+ getStringLiteralQuote() + "]").print(" != null && ");
|
.print(" != null && ");
|
||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
print("[0]");
|
print("[0]");
|
||||||
print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME
|
print("[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote()
|
||||||
+ getStringLiteralQuote() + "].indexOf(\"")
|
+ "].indexOf(\"").print(type.tsym.getQualifiedName().toString()).print("\") >= 0");
|
||||||
.print(type.tsym.getQualifiedName().toString()).print("\") >= 0");
|
|
||||||
print(" || ");
|
print(" || ");
|
||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
@ -5818,21 +5819,19 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
print("[0]");
|
print("[0]");
|
||||||
print(".constructor[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME
|
print(".constructor[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote()
|
||||||
+ getStringLiteralQuote() + "]").print(" != null && ");
|
+ "]").print(" != null && ");
|
||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
print("[0]");
|
print("[0]");
|
||||||
print(".constructor[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME
|
print(".constructor[" + getStringLiteralQuote() + INTERFACES_FIELD_NAME + getStringLiteralQuote()
|
||||||
+ getStringLiteralQuote() + "].indexOf(\"")
|
+ "].indexOf(\"").print(type.tsym.getQualifiedName().toString()).print("\") >= 0");
|
||||||
.print(type.tsym.getQualifiedName().toString()).print("\") >= 0");
|
|
||||||
if (CharSequence.class.getName().equals(type.tsym.getQualifiedName().toString())) {
|
if (CharSequence.class.getName().equals(type.tsym.getQualifiedName().toString())) {
|
||||||
print(" || typeof ");
|
print(" || typeof ");
|
||||||
print(exprStr, expr);
|
print(exprStr, expr);
|
||||||
if (checkFirstArrayElement)
|
if (checkFirstArrayElement)
|
||||||
print("[0]");
|
print("[0]");
|
||||||
print(" === " + getStringLiteralQuote() + "string"
|
print(" === " + getStringLiteralQuote() + "string" + getStringLiteralQuote());
|
||||||
+ getStringLiteralQuote());
|
|
||||||
}
|
}
|
||||||
print(")");
|
print(")");
|
||||||
} else {
|
} else {
|
||||||
@ -5967,10 +5966,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
}
|
}
|
||||||
if (assignedType.isInterface() && expression.type.tsym.isEnum()) {
|
if (assignedType.isInterface() && expression.type.tsym.isEnum()) {
|
||||||
String relTarget = getRootRelativeName((Symbol) expression.type.tsym);
|
String relTarget = getRootRelativeName((Symbol) expression.type.tsym);
|
||||||
print("((wrappers, value) => wrappers===undefined?value:wrappers[value])(").print(relTarget)
|
print("((wrappers, value) => wrappers===undefined?value:wrappers[value])(")
|
||||||
.print("[" + getStringLiteralQuote()
|
.print(relTarget).print("[" + getStringLiteralQuote()
|
||||||
+ Java2TypeScriptTranslator.ENUM_WRAPPER_CLASS_WRAPPERS
|
+ Java2TypeScriptTranslator.ENUM_WRAPPER_CLASS_WRAPPERS + getStringLiteralQuote() + "], ")
|
||||||
+ getStringLiteralQuote() + "], ")
|
|
||||||
.print(expression).print(")");
|
.print(expression).print(")");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -772,7 +772,18 @@ public class PrinterAdapter {
|
|||||||
* @return true if substituted
|
* @return true if substituted
|
||||||
*/
|
*/
|
||||||
public boolean substituteOverloadMethodBody(TypeElement parentTypeElement, Overload overload) {
|
public boolean substituteOverloadMethodBody(TypeElement parentTypeElement, Overload overload) {
|
||||||
return false;
|
return parentAdapter == null ? false : parentAdapter.substituteOverloadMethodBody(parentTypeElement, overload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitutes method's body
|
||||||
|
*
|
||||||
|
* @param parentTypeElement parent class
|
||||||
|
* @param method method's symbol
|
||||||
|
* @return true if substituted
|
||||||
|
*/
|
||||||
|
public boolean substituteMethodBody(TypeElement parentTypeElement, ExecutableElement method) {
|
||||||
|
return parentAdapter == null ? false : parentAdapter.substituteMethodBody(parentTypeElement, method);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user