Search in sources :

Example 1 with ConstructorInvocation

use of com.google.devtools.j2objc.ast.ConstructorInvocation 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 ConstructorInvocation

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

the class TreeConverter method convertConstructorInvocation.

private static TreeNode convertConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation node) {
    IMethodBinding binding = node.resolveConstructorBinding();
    ConstructorInvocation newNode = new ConstructorInvocation().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;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair)

Example 3 with ConstructorInvocation

use of com.google.devtools.j2objc.ast.ConstructorInvocation 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 4 with ConstructorInvocation

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

the class InitializationNormalizer method isDesignatedConstructor.

/**
   * Returns true if this is a constructor that doesn't call "this(...)".  This constructors are
   * skipped so initializers aren't run more than once per instance creation.
   */
public static boolean isDesignatedConstructor(MethodDeclaration node) {
    if (!node.isConstructor()) {
        return false;
    }
    Block body = node.getBody();
    if (body == null) {
        return false;
    }
    List<Statement> stmts = body.getStatements();
    return (stmts.isEmpty() || !(stmts.get(0) instanceof ConstructorInvocation));
}
Also used : ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Statement(com.google.devtools.j2objc.ast.Statement) Block(com.google.devtools.j2objc.ast.Block)

Aggregations

ConstructorInvocation (com.google.devtools.j2objc.ast.ConstructorInvocation)4 SuperConstructorInvocation (com.google.devtools.j2objc.ast.SuperConstructorInvocation)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 Statement (com.google.devtools.j2objc.ast.Statement)2 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)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 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 IfStatement (com.google.devtools.j2objc.ast.IfStatement)1 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)1 SuperMethodInvocation (com.google.devtools.j2objc.ast.SuperMethodInvocation)1 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)1 SynchronizedStatement (com.google.devtools.j2objc.ast.SynchronizedStatement)1