diff --git a/transpiler/src/main/java/org/jsweet/transpiler/AsyncAwaitPropagationScanner.java b/transpiler/src/main/java/org/jsweet/transpiler/AsyncAwaitPropagationScanner.java index 3035f757..83dff012 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/AsyncAwaitPropagationScanner.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/AsyncAwaitPropagationScanner.java @@ -18,12 +18,20 @@ */ package org.jsweet.transpiler; +import java.util.LinkedList; import java.util.List; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; + import org.jsweet.JSweetConfig; +import org.jsweet.transpiler.OverloadScanner.Overload; import org.jsweet.transpiler.util.AbstractTreeScanner; +import org.jsweet.transpiler.util.Util; import com.sun.tools.javac.code.Symbol.MethodSymbol; +import com.sun.tools.javac.code.Symbol.TypeSymbol; +import com.sun.tools.javac.code.Type.MethodType; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; @@ -63,6 +71,18 @@ public class AsyncAwaitPropagationScanner extends AbstractTreeScanner { JCMethodDecl parent = getParent(JCMethodDecl.class); if (!context.hasAnnotationType(parent.sym, JSweetConfig.ANNOTATION_ASYNC)) { context.addExtraAnnotationType(parent.sym, JSweetConfig.ANNOTATION_ASYNC); + if (context.isInvalidOverload(parent.sym)) { + Overload overload = context.getOverload((TypeElement) parent.sym.getEnclosingElement(), parent.sym); + for (ExecutableElement e : overload.getMethods()) { + context.addExtraAnnotationType(e, JSweetConfig.ANNOTATION_ASYNC); + } + } + List candidates = new LinkedList<>(); + Util.collectMatchingMethodDeclarationsInType(context.types, (TypeSymbol)parent.sym.getEnclosingElement(), parent.name.toString(), (MethodType)parent.sym.type, true, candidates); + for (MethodSymbol candidate : candidates) { + context.addExtraAnnotationType(candidate, JSweetConfig.ANNOTATION_ASYNC); + } + stillWorking = true; } JCTree directParent = getParent(); diff --git a/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java b/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java index eed788aa..ecc3f571 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/JSweetContext.java @@ -1319,8 +1319,8 @@ public class JSweetContext extends Context { if (extraAnnotations != null) { for (String annotationType : annotationTypes) { - Set symbols = extraAnnotations.get(annotationType); - if (symbols != null && symbols.contains(symbol)) { + Set elements = extraAnnotations.get(annotationType); + if (elements != null && elements.contains(symbol)) { return true; } } @@ -1329,25 +1329,25 @@ public class JSweetContext extends Context { return hasActualAnnotationType(symbol, annotationTypes); } - private Map> extraAnnotations; + private Map> extraAnnotations; /** * Adds an extra annotation type to a symbol (no args). * See {@link #hasAnnotationType(Symbol, String...)}. * - * @param symbol the symbol to add the annotation to + * @param element the element to add the annotation to * @param annotationTypeName the annotation type name */ - public void addExtraAnnotationType(Symbol symbol, String annotationTypeName) { + public void addExtraAnnotationType(Element symbol, String annotationTypeName) { if (extraAnnotations == null) { - extraAnnotations = new HashMap>(); + extraAnnotations = new HashMap>(); } - Set symbols = extraAnnotations.get(annotationTypeName); - if (symbols == null) { - symbols = new HashSet<>(); - extraAnnotations.put(annotationTypeName, symbols); + Set elements = extraAnnotations.get(annotationTypeName); + if (elements == null) { + elements = new HashSet<>(); + extraAnnotations.put(annotationTypeName, elements); } - symbols.add(symbol); + elements.add(symbol); } /** diff --git a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java index 7debcfff..6d297a75 100644 --- a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java +++ b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java @@ -2646,6 +2646,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { print("{").println().startIndent().printIndent(); // temporary cast to any because of Java generics bug print("return "); + if (isAsyncMethod(method)) { + print("await "); + } if (!isGlobalsClassName(parent.name.toString())) { if (method.sym.isStatic()) { print(getQualifiedTypeName(parent.sym, false, false).toString()); @@ -2674,23 +2677,23 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { } protected void printMethodReturnDeclaration(JCMethodDecl methodDecl, boolean inCoreWrongOverload) { - if (inCoreWrongOverload && !methodDecl.sym.isConstructor()) { - print(" : any"); - } else { - if (methodDecl.restype != null && methodDecl.restype.type.getTag() != TypeTag.VOID) { - print(" : "); + if (methodDecl.restype != null && methodDecl.restype.type.getTag() != TypeTag.VOID) { + print(" : "); - boolean promisify = isAsyncMethod(methodDecl) - && !methodDecl.restype.type.tsym.getQualifiedName().toString().endsWith(".Promise"); - if (promisify) { - print("Promise<"); - } + boolean promisify = isAsyncMethod(methodDecl) + && !methodDecl.restype.type.tsym.getQualifiedName().toString().endsWith(".Promise"); + if (promisify) { + print("Promise<"); + } - substituteAndPrintType(methodDecl.restype); + if (inCoreWrongOverload && !methodDecl.sym.isConstructor()) { + print("any"); + } else { + substituteAndPrintType(methodDecl.restype); + } - if (promisify) { - print(">"); - } + if (promisify) { + print(">"); } } } @@ -2717,7 +2720,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter { } if (isAsyncMethod(methodDecl)) { - print(" async "); + print("async "); } } diff --git a/transpiler/src/test/java/source/syntax/AsyncAwaitPropagation.java b/transpiler/src/test/java/source/syntax/AsyncAwaitPropagation.java index c5747b81..c09e7f9b 100644 --- a/transpiler/src/test/java/source/syntax/AsyncAwaitPropagation.java +++ b/transpiler/src/test/java/source/syntax/AsyncAwaitPropagation.java @@ -2,16 +2,20 @@ package source.syntax; import jsweet.lang.Async; -public class AsyncAwaitPropagation { +public class AsyncAwaitPropagation implements Interface { @Async int m1() { return 0; } - int m2() { + public int m2() { return 1 + m1(); } + int m2(int i) { + return i + m1(); + } + void m3() { if (m1() == 1) { // do something @@ -20,8 +24,16 @@ public class AsyncAwaitPropagation { } } - int m4() { + public int m4() { return m2(); } + + int m5() { + return m2(2); + } } + +interface Interface { + int m2(); +} \ No newline at end of file