mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 07:19:22 +00:00
async/await propagation for overloads and overrides
This commit is contained in:
parent
80721ed55d
commit
a2157730c8
@ -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<MethodSymbol> 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();
|
||||
|
||||
@ -1319,8 +1319,8 @@ public class JSweetContext extends Context {
|
||||
|
||||
if (extraAnnotations != null) {
|
||||
for (String annotationType : annotationTypes) {
|
||||
Set<Symbol> symbols = extraAnnotations.get(annotationType);
|
||||
if (symbols != null && symbols.contains(symbol)) {
|
||||
Set<Element> 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<String, Set<Symbol>> extraAnnotations;
|
||||
private Map<String, Set<Element>> 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<String, Set<Symbol>>();
|
||||
extraAnnotations = new HashMap<String, Set<Element>>();
|
||||
}
|
||||
Set<Symbol> symbols = extraAnnotations.get(annotationTypeName);
|
||||
if (symbols == null) {
|
||||
symbols = new HashSet<>();
|
||||
extraAnnotations.put(annotationTypeName, symbols);
|
||||
Set<Element> elements = extraAnnotations.get(annotationTypeName);
|
||||
if (elements == null) {
|
||||
elements = new HashSet<>();
|
||||
extraAnnotations.put(annotationTypeName, elements);
|
||||
}
|
||||
symbols.add(symbol);
|
||||
elements.add(symbol);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -2646,6 +2646,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
print("{").println().startIndent().printIndent();
|
||||
// temporary cast to any because of Java generics bug
|
||||
print("return <any>");
|
||||
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 ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user