fix cast issue with inheritance and generics

This commit is contained in:
Renaud Pawlak 2017-11-04 10:38:47 +01:00
parent 8fd1ef2819
commit e22523ed18
4 changed files with 54 additions and 6 deletions

View File

@ -291,7 +291,7 @@
<dependency>
<groupId>org.jsweet.candies</groupId>
<artifactId>threejs</artifactId>
<version>75-20170726</version>
<version>75-20171101</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

View File

@ -476,12 +476,15 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
boolean fullImport = require || GLOBALS_CLASS_NAME.equals(targetName);
if (fullImport) {
if (context.useRequireForModules) {
context.addHeader("import."+targetName, "import " + targetName + " = require(\"" + moduleName + "\");\n");
context.addHeader("import." + targetName,
"import " + targetName + " = require(\"" + moduleName + "\");\n");
} else {
context.addHeader("import."+targetName, "import * as " + targetName + " from '" + moduleName + "';\n");
context.addHeader("import." + targetName,
"import * as " + targetName + " from '" + moduleName + "';\n");
}
} else {
context.addHeader("import."+targetName, "import { " + targetName + " } from '" + moduleName + "';\n");
context.addHeader("import." + targetName,
"import { " + targetName + " } from '" + moduleName + "';\n");
}
}
context.registerImportedName(compilationUnit.getSourceFile().getName(), sourceElement, targetName);
@ -4859,9 +4862,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} else {
print("<");
substituteAndPrintType(cast.clazz).print(">");
// Java always allows casting when an interface is involved
// Java always allows casting when an interface or a type param
// is involved
// (that's weak!!)
if (cast.expr.type.tsym.isInterface() || cast.type.tsym.isInterface()) {
if (cast.expr.type.tsym.isInterface() || cast.type.tsym.isInterface()
|| cast.clazz.type.getKind() == TypeKind.TYPEVAR) {
print("<any>");
}
}

View File

@ -44,6 +44,7 @@ import source.structural.GetClass;
import source.structural.GlobalsAccess;
import source.structural.Inheritance;
import source.structural.InheritanceOrderInSameFile;
import source.structural.InheritanceWithGenerics;
import source.structural.InnerClass;
import source.structural.InnerClassFieldClash;
import source.structural.InnerClassNotStatic;
@ -487,4 +488,12 @@ public class StructuralTests extends AbstractTest {
}, getSourceFile(FieldInitialization.class));
}
@Test
public void testInheritanceWithGenerics() {
transpile(logHandler -> {
logHandler.assertNoProblems();
}, getSourceFile(InheritanceWithGenerics.class));
}
}

View File

@ -0,0 +1,34 @@
package source.structural;
import java.util.Map;
class T1 {
public void m() {
}
}
class T2 extends T1 {
}
public class InheritanceWithGenerics {
protected <T> T resolveObject(T elementDefaultObject, String elementName, Map<String, String> attributes) {
return elementDefaultObject;
}
}
class SubClass {
protected <T> T resolveObject(T elementDefaultObject, String elementName, Map<String, String> attributes) {
if (elementDefaultObject instanceof T1) {
T1 piece = new T2();
return (T) piece;
}
return elementDefaultObject;
}
}