add support for static methods with no targets

This commit is contained in:
Renaud Pawlak 2020-01-22 08:49:06 +01:00
parent 617e5f9696
commit 647100fc05
2 changed files with 48 additions and 13 deletions

View File

@ -135,7 +135,8 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
/**
* Creates a root adapter (with no parent).
*
* @param context the transpilation context
* @param context
* the transpilation context
*/
public Java2TypeScriptAdapter(JSweetContext context) {
super(context);
@ -143,12 +144,12 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
}
/**
* Creates a new adapter that will try delegate to the given parent adapter when
* not implementing its own behavior.
* Creates a new adapter that will try delegate to the given parent adapter
* when not implementing its own behavior.
*
* @param parentAdapter cannot be null: if no parent you must use the
* {@link #AbstractPrinterAdapter(JSweetContext)}
* constructor
* @param parentAdapter
* cannot be null: if no parent you must use the
* {@link #AbstractPrinterAdapter(JSweetContext)} constructor
*/
public Java2TypeScriptAdapter(PrinterAdapter parent) {
super(parent);
@ -428,6 +429,12 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
print(relTarget).print("[").print(relTarget).print("[").print(invocationElement.getTargetExpression())
.print("]").print("]");
return true;
case "values":
printMacroName("Enum." + targetMethodName);
print("function() { " + VAR_DECL_KEYWORD + " result: number[] = []; for(" + VAR_DECL_KEYWORD
+ " val in ").print(relTarget).print(
") { if(!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
return true;
case "valueOf":
printMacroName("Enum." + targetMethodName);
if (invocationElement.getArgumentCount() == 1) {
@ -436,12 +443,6 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
return true;
}
break;
case "values":
printMacroName("Enum." + targetMethodName);
print("function() { " + VAR_DECL_KEYWORD + " result: number[] = []; for(" + VAR_DECL_KEYWORD
+ " val in ").print(relTarget).print(
") { if(!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
return true;
case "equals":
printMacroName("Enum." + targetMethodName);
print("(<any>(").print(invocationElement.getTargetExpression()).print(") === <any>(")
@ -464,6 +465,19 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
return true;
}
// enum static methods
if (targetType != null && targetType.getKind() == ElementKind.ENUM) {
String relTarget = getRootRelativeName((Symbol) targetType);
switch (targetMethodName) {
case "values":
printMacroName("Enum." + targetMethodName);
print("function() { " + VAR_DECL_KEYWORD + " result: number[] = []; for(" + VAR_DECL_KEYWORD
+ " val in ").print(relTarget).print(
") { if(!isNaN(<any>val)) { result.push(parseInt(val,10)); } } return result; }()");
return true;
}
}
if (targetClassName != null && targetMethodName != null) {
switch (targetClassName) {
case UTIL_CLASSNAME:
@ -1351,7 +1365,8 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
}
/**
* @param enclosingElement is required for functional (ie dynamic) type mappings
* @param enclosingElement
* is required for functional (ie dynamic) type mappings
*/
public boolean substituteAndPrintType(ExtendedElement enclosingElement, TypeElement type) {
String typeFullName = type.toString();

View File

@ -22,6 +22,7 @@ public class ComplexEnums {
assert A.i == 2;
assert $insert("ComplexEnums.A['__class']") == "source.enums.ComplexEnums.A";
assert $insert("ComplexEnums.InnerEnum['__class']") == "source.enums.ComplexEnums.InnerEnum";
assert MyComplexEnum.getByValue(1.5f) == MyComplexEnum.RATIO_3_2;
trace.push("" + InnerEnum.E3.getMode());
trace.push("" + A.i);
@ -81,9 +82,28 @@ enum MyComplexEnum {
aNonStaticMethod();
this.aNonStaticMethod();
aStaticMethod2();
name();
return value;
}
public static MyComplexEnum getByValue(Float f) {
for (MyComplexEnum e : values()) {
if (e.value == f) {
return e;
}
}
return null;
}
public static MyComplexEnum getByValue2(Float f) {
for (MyComplexEnum e : MyComplexEnum.values()) {
if (e.value == f) {
return e;
}
}
return null;
}
public static void aStaticMethod() {
ComplexEnums.trace.push("static");
}