mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
fix cast issue with inheritance and generics
This commit is contained in:
parent
8fd1ef2819
commit
e22523ed18
@ -291,7 +291,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jsweet.candies</groupId>
|
<groupId>org.jsweet.candies</groupId>
|
||||||
<artifactId>threejs</artifactId>
|
<artifactId>threejs</artifactId>
|
||||||
<version>75-20170726</version>
|
<version>75-20171101</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
@ -476,12 +476,15 @@ 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, "import " + targetName + " = require(\"" + moduleName + "\");\n");
|
context.addHeader("import." + targetName,
|
||||||
|
"import " + targetName + " = require(\"" + moduleName + "\");\n");
|
||||||
} else {
|
} else {
|
||||||
context.addHeader("import."+targetName, "import * as " + targetName + " from '" + moduleName + "';\n");
|
context.addHeader("import." + targetName,
|
||||||
|
"import * as " + targetName + " from '" + moduleName + "';\n");
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
context.registerImportedName(compilationUnit.getSourceFile().getName(), sourceElement, targetName);
|
||||||
@ -4859,9 +4862,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
|||||||
} else {
|
} else {
|
||||||
print("<");
|
print("<");
|
||||||
substituteAndPrintType(cast.clazz).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!!)
|
// (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>");
|
print("<any>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,7 @@ import source.structural.GetClass;
|
|||||||
import source.structural.GlobalsAccess;
|
import source.structural.GlobalsAccess;
|
||||||
import source.structural.Inheritance;
|
import source.structural.Inheritance;
|
||||||
import source.structural.InheritanceOrderInSameFile;
|
import source.structural.InheritanceOrderInSameFile;
|
||||||
|
import source.structural.InheritanceWithGenerics;
|
||||||
import source.structural.InnerClass;
|
import source.structural.InnerClass;
|
||||||
import source.structural.InnerClassFieldClash;
|
import source.structural.InnerClassFieldClash;
|
||||||
import source.structural.InnerClassNotStatic;
|
import source.structural.InnerClassNotStatic;
|
||||||
@ -487,4 +488,12 @@ public class StructuralTests extends AbstractTest {
|
|||||||
}, getSourceFile(FieldInitialization.class));
|
}, getSourceFile(FieldInitialization.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInheritanceWithGenerics() {
|
||||||
|
transpile(logHandler -> {
|
||||||
|
logHandler.assertNoProblems();
|
||||||
|
}, getSourceFile(InheritanceWithGenerics.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user