fixes for J4TS with modules => avoid self imports and use the right inner class names

This commit is contained in:
Renaud Pawlak 2021-02-27 11:25:06 +01:00
parent e4b1148812
commit 7f82548327
2 changed files with 17 additions and 17 deletions

View File

@ -605,6 +605,15 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
private void useModule(boolean require, boolean direct, PackageElement targetPackage, JCTree sourceTree, private void useModule(boolean require, boolean direct, PackageElement targetPackage, JCTree sourceTree,
String targetName, String moduleName, Symbol sourceElement) { String targetName, String moduleName, Symbol sourceElement) {
// avoid imports for names of types that are declared in this file
for (JCTree t : getCompilationUnit().getTypeDecls()) {
if (t instanceof JCClassDecl) {
if (targetName.equals(((JCClassDecl)t).sym.getSimpleName().toString())) {
return;
}
}
}
if (targetName.equals(GLOBALS_CLASS_NAME)) { if (targetName.equals(GLOBALS_CLASS_NAME)) {
return; return;
} }
@ -1805,9 +1814,6 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
scanner.scan(defaultMethod.typarams); scanner.scan(defaultMethod.typarams);
scanner.scan(defaultMethod.recvparam); scanner.scan(defaultMethod.recvparam);
} else { } else {
if (defaultMethod.getName().toString().equals("forEach")) {
System.out.println();
}
scanner.scan(defaultMethod); scanner.scan(defaultMethod);
} }
} }
@ -4258,18 +4264,6 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
print("."); print(".");
} }
} else { } else {
if (context.useModules) {
if (!((ClassSymbol) selected.type.tsym).sourcefile.getName()
.equals(getCompilationUnit().sourcefile.getName())) {
// TODO: when using several qualified
// Globals classes, we
// need to disambiguate (use qualified
// name with
// underscores)
print(GLOBALS_CLASS_NAME).print(".");
}
}
Map<String, VarSymbol> vars = new HashMap<>(); Map<String, VarSymbol> vars = new HashMap<>();
Util.fillAllVariablesInScope(vars, getStack(), inv, getParent(JCMethodDecl.class)); Util.fillAllVariablesInScope(vars, getStack(), inv, getParent(JCMethodDecl.class));
if (vars.containsKey(methName)) { if (vars.containsKey(methName)) {
@ -4575,7 +4569,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
// current class // current class
if (!prefixAdded && clazz.getEnclosingElement() instanceof ClassSymbol) { if (!prefixAdded && clazz.getEnclosingElement() instanceof ClassSymbol) {
if (context.useModules) { if (context.useModules) {
print(clazz.getEnclosingElement().getSimpleName() + "."); String enclosingName = clazz.getEnclosingElement().getSimpleName().toString();
if (context.hasClassNameMapping(clazz.getEnclosingElement())) {
enclosingName = context.getClassNameMapping(clazz.getEnclosingElement());
}
print(enclosingName + ".");
prefixAdded = true; prefixAdded = true;
} else { } else {
// if the class has not been imported, we need to add // if the class has not been imported, we need to add
@ -4864,6 +4862,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
* Prints a new-class expression tree (default behavior). * Prints a new-class expression tree (default behavior).
*/ */
public void printDefaultNewClass(JCNewClass newClass) { public void printDefaultNewClass(JCNewClass newClass) {
String mappedType = context.getTypeMappingTarget(newClass.clazz.type.toString()); String mappedType = context.getTypeMappingTarget(newClass.clazz.type.toString());
if (typeChecker.checkType(newClass, null, newClass.clazz)) { if (typeChecker.checkType(newClass, null, newClass.clazz)) {

View File

@ -1701,7 +1701,8 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
@Override @Override
public boolean substituteExtends(TypeElement type) { public boolean substituteExtends(TypeElement type) {
// J4TS hack to avoid name clash between date classes (should be solved automatically) // J4TS hack to avoid name clash between date classes (should be solved automatically)
if ("java.sql.Date".equals(type.getQualifiedName().toString())) { if (context.useModules && type.getEnclosingElement() != null && "java.sql".equals(type.getEnclosingElement().toString())
&& Date.class.getName().equals(type.getSuperclass().toString())) {
String pathToImportedClass = util().getRelativePath( String pathToImportedClass = util().getRelativePath(
"@/" + getCompilationUnit().getPackage().toString().replace('.', '/'), "@/" + getCompilationUnit().getPackage().toString().replace('.', '/'),
("@/" + Date.class.getName()).replace('.', '/')); ("@/" + Date.class.getName()).replace('.', '/'));