Search in sources :

Example 1 with SuperConstructorInvocation

use of com.google.devtools.j2objc.ast.SuperConstructorInvocation in project j2objc by google.

the class TreeConverter method needsImplicitSuperCall.

private static boolean needsImplicitSuperCall(MethodDeclaration node) {
    ExecutableElement method = node.getExecutableElement();
    if (!ElementUtil.isConstructor(method)) {
        return false;
    }
    TypeMirror superType = ElementUtil.getDeclaringClass(method).getSuperclass();
    if (TypeUtil.isNone(superType)) {
        // java.lang.Object supertype is null.
        return false;
    }
    List<Statement> stmts = node.getBody().getStatements();
    if (stmts.isEmpty()) {
        return true;
    }
    Statement firstStmt = stmts.get(0);
    return !(firstStmt instanceof SuperConstructorInvocation || firstStmt instanceof ConstructorInvocation);
}
Also used : ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation)

Example 2 with SuperConstructorInvocation

use of com.google.devtools.j2objc.ast.SuperConstructorInvocation in project j2objc by google.

the class TreeConverter method addImplicitSuperCall.

private static void addImplicitSuperCall(MethodDeclaration node) {
    ExecutableElement method = node.getExecutableElement();
    DeclaredType superType = (DeclaredType) ElementUtil.getDeclaringClass(method).getSuperclass();
    TypeElement superClass = TypeUtil.asTypeElement(superType);
    ExecutableElement superConstructor = findDefaultConstructorElement(superClass);
    SuperConstructorInvocation invocation = new SuperConstructorInvocation(new ExecutablePair(superConstructor, (ExecutableType) JdtTypes.asMemberOfInternal(superType, superConstructor))).setVarargsType(getVarargsType(BindingConverter.unwrapExecutableElement(superConstructor), Collections.emptyList()));
    node.getBody().addStatement(0, invocation);
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) DeclaredType(javax.lang.model.type.DeclaredType)

Example 3 with SuperConstructorInvocation

use of com.google.devtools.j2objc.ast.SuperConstructorInvocation in project j2objc by google.

the class TreeConverter method createAnonymousConstructor.

private static MethodDeclaration createAnonymousConstructor(JdtExecutableElement constructorElem, IMethodBinding constructorBinding) {
    MethodDeclaration constructor = new MethodDeclaration(constructorElem);
    Block body = new Block();
    constructor.setBody(body);
    IMethodBinding superConstructorBinding = findSuperConstructor(constructorBinding);
    ExecutablePair superConstructor = new ExecutablePair(BindingConverter.getExecutableElement(superConstructorBinding), BindingConverter.getType(superConstructorBinding));
    SuperConstructorInvocation superCall = new SuperConstructorInvocation(superConstructor);
    body.addStatement(superCall);
    Iterator<? extends VariableElement> params = constructorElem.getParameters().iterator();
    if (constructorElem.hasSuperOuter()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.setExpression(new SimpleName(param));
    }
    while (params.hasNext()) {
        VariableElement param = params.next();
        constructor.addParameter(new SingleVariableDeclaration(param));
        superCall.addArgument(new SimpleName(param));
    }
    assert constructor.getParameters().size() == constructorElem.getParameters().size();
    return constructor;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Block(com.google.devtools.j2objc.ast.Block) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 4 with SuperConstructorInvocation

use of com.google.devtools.j2objc.ast.SuperConstructorInvocation in project j2objc by google.

the class TreeConverter method convertMethodInvocation.

private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
    JCTree.JCExpression method = node.getMethodSelect();
    String methodName = getMemberName(method);
    ExecutableType type = (ExecutableType) method.type;
    Symbol.MethodSymbol sym = (Symbol.MethodSymbol) getMemberSymbol(method);
    JCTree.JCExpression target = method.getKind() == Kind.MEMBER_SELECT ? ((JCTree.JCFieldAccess) method).selected : null;
    if ("this".equals(methodName)) {
        ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if ("super".equals(methodName)) {
        SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
        if (target != null) {
            newNode.setExpression((Expression) convert(target));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    if (target != null && "super".equals(getMemberName(target))) {
        SuperMethodInvocation newNode = new SuperMethodInvocation().setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement).setName(convertSimpleName(sym, type, getPosition(node)));
        if (target.getKind() == Kind.MEMBER_SELECT) {
            // foo.bar.MyClass.super.print(...):
            //   target: foo.bar.MyClass.super
            //   target.selected: foo.bar.MyClass
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) target).selected));
        }
        for (JCTree.JCExpression arg : node.getArguments()) {
            newNode.addArgument((Expression) convert(arg));
        }
        return newNode;
    }
    MethodInvocation newNode = new MethodInvocation();
    newNode.setName(convertSimpleName(sym, type, getPosition(method)));
    if (target != null) {
        newNode.setExpression((Expression) convert(target));
    }
    for (JCTree.JCExpression arg : node.getArguments()) {
        newNode.addArgument((Expression) convert(arg));
    }
    return newNode.setTypeMirror(node.type).setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement);
}
Also used : ExecutableType(javax.lang.model.type.ExecutableType) ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) JCTree(com.sun.tools.javac.tree.JCTree) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation)

Example 5 with SuperConstructorInvocation

use of com.google.devtools.j2objc.ast.SuperConstructorInvocation in project j2objc by google.

the class TreeConverter method convertSuperConstructorInvocation.

private static TreeNode convertSuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation node) {
    IMethodBinding binding = node.resolveConstructorBinding();
    SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(BindingConverter.getExecutableElement(binding), BindingConverter.getType(binding))).setVarargsType(getVarargsType(binding, node.arguments()));
    for (Object argument : node.arguments()) {
        newNode.addArgument((Expression) convert(argument));
    }
    return newNode.setExpression((Expression) TreeConverter.convert(node.getExpression()));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation)

Aggregations

SuperConstructorInvocation (com.google.devtools.j2objc.ast.SuperConstructorInvocation)5 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)4 ConstructorInvocation (com.google.devtools.j2objc.ast.ConstructorInvocation)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 AssertStatement (com.google.devtools.j2objc.ast.AssertStatement)1 Block (com.google.devtools.j2objc.ast.Block)1 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)1 DoStatement (com.google.devtools.j2objc.ast.DoStatement)1 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 IfStatement (com.google.devtools.j2objc.ast.IfStatement)1 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)1 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)1 SimpleName (com.google.devtools.j2objc.ast.SimpleName)1