Search in sources :

Example 1 with QualifiedName

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

the class TreeConverter method convertQualifiedName.

private static TreeNode convertQualifiedName(org.eclipse.jdt.core.dom.QualifiedName node) {
    QualifiedName newNode = new QualifiedName();
    convertName(node, newNode);
    return newNode.setQualifier((Name) TreeConverter.convert(node.getQualifier())).setName((SimpleName) TreeConverter.convert(node.getName()));
}
Also used : QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName)

Example 2 with QualifiedName

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

the class TreeConverter method convertFieldAccess.

private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
    String fieldName = node.name.toString();
    SourcePosition pos = getPosition(node);
    JCTree.JCExpression selected = node.getExpression();
    if (fieldName.equals("this")) {
        return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
    }
    if ("super".equals(getMemberName(selected))) {
        SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
        if (selected.getKind() == Kind.MEMBER_SELECT) {
            newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
        }
        return newNode;
    }
    if (node.getIdentifier().toString().equals("class")) {
        return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
    }
    if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
        if (selected.toString().equals("this")) {
            // Just return the constant.
            return new SimpleName(node.sym);
        }
        JCIdent ident = (JCTree.JCIdent) selected;
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
    }
    if (selected.getKind() == Kind.MEMBER_SELECT) {
        TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
        if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
            return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
        }
    }
    if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
        return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
    }
    return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) SimpleName(com.google.devtools.j2objc.ast.SimpleName) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) JCTree(com.sun.tools.javac.tree.JCTree) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess) VariableElement(javax.lang.model.element.VariableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 3 with QualifiedName

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

the class TreeConverter method convertFieldAccess.

private TreeNode convertFieldAccess(MemberSelectTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    String fieldName = node.getIdentifier().toString();
    SourcePosition pos = getPosition(node);
    ExpressionTree selected = node.getExpression();
    TreePath selectedPath = getTreePath(path, selected);
    Element element = getElement(path);
    TypeMirror typeMirror = getTypeMirror(path);
    if (fieldName.equals("this")) {
        return new ThisExpression().setQualifier((Name) convert(selected, path)).setTypeMirror(typeMirror);
    }
    if ("super".equals(getMemberName(selected))) {
        SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) element).setTypeMirror(typeMirror);
        if (selected.getKind() == Kind.MEMBER_SELECT) {
            newNode.setQualifier((Name) convert(((MemberSelectTree) selected).getExpression(), selectedPath));
        }
        return newNode;
    }
    if (node.getIdentifier().toString().equals("class")) {
        Type type = convertType(getTypeMirror(selectedPath), pos, false);
        type.setPosition(getPosition(node));
        return new TypeLiteral(typeMirror).setType(type);
    }
    if (selected.getKind() == Kind.IDENTIFIER && (!element.getKind().isField() || ElementUtil.isConstant((VariableElement) element))) {
        if (selected.toString().equals("this")) {
            // Just return the constant.
            return new SimpleName(element);
        }
        return new QualifiedName().setName(convertSimpleName(element, typeMirror, pos)).setQualifier(convertSimpleName(getElement(selectedPath), getTypeMirror(selectedPath), pos)).setElement(element);
    }
    if (selected.getKind() == Kind.MEMBER_SELECT) {
        TreeNode newSelected = convertFieldAccess((MemberSelectTree) selected, path).setPosition(pos);
        if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
            return new QualifiedName().setName(convertSimpleName(element, typeMirror, pos)).setQualifier((QualifiedName) newSelected).setElement(element);
        }
    }
    if (ElementUtil.isConstant((VariableElement) element) && ElementUtil.isStatic(element) && !(selected.getKind() == Kind.METHOD_INVOCATION) && !(selected.getKind() == Kind.MEMBER_SELECT) && !(selected.getKind() == Kind.PARENTHESIZED)) {
        return new QualifiedName().setName(convertSimpleName(element, typeMirror, pos)).setQualifier((Name) convert(selected, path)).setElement(element);
    }
    return new FieldAccess().setVariableElement((VariableElement) element).setExpression((Expression) convert(selected, path)).setName(convertSimpleName(element, typeMirror, pos).setTypeMirror(typeMirror));
}
Also used : GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) GeneratedPackageElement(com.google.devtools.j2objc.types.GeneratedPackageElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess) VariableElement(javax.lang.model.element.VariableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) TreePath(com.sun.source.util.TreePath) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) JCFunctionalExpression(com.sun.tools.javac.tree.JCTree.JCFunctionalExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) SourcePosition(com.google.devtools.j2objc.ast.SourcePosition) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 4 with QualifiedName

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

the class StatementGenerator method visit.

@Override
public boolean visit(QualifiedName node) {
    Element element = node.getElement();
    if (ElementUtil.isVariable(element)) {
        VariableElement var = (VariableElement) element;
        if (ElementUtil.isGlobalVar(var)) {
            buffer.append(nameTable.getVariableQualifiedName(var));
            return false;
        }
    }
    if (ElementUtil.isTypeElement(element)) {
        buffer.append(nameTable.getFullName((TypeElement) element));
        return false;
    }
    Name qualifier = node.getQualifier();
    qualifier.accept(this);
    buffer.append("->");
    node.getName().accept(this);
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) SimpleName(com.google.devtools.j2objc.ast.SimpleName) Name(com.google.devtools.j2objc.ast.Name)

Example 5 with QualifiedName

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

the class OuterReferenceResolverTest method testOuterVarAccess.

public void testOuterVarAccess() {
    resolveSource("Test", "class Test { int i; class Inner { void test() { i++; } } }");
    TypeDeclaration innerNode = (TypeDeclaration) nodesByType.get(Kind.TYPE_DECLARATION).get(1);
    assertTrue(captureInfo.needsOuterReference(innerNode.getTypeElement()));
    PostfixExpression increment = (PostfixExpression) nodesByType.get(Kind.POSTFIX_EXPRESSION).get(0);
    Expression iNode = increment.getOperand();
    assertTrue(iNode instanceof QualifiedName);
    VariableElement outerVar = TreeUtil.getVariableElement(((QualifiedName) iNode).getQualifier());
    assertNotNull(outerVar);
    assertEquals("Test", outerVar.asType().toString());
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) QualifiedName(com.google.devtools.j2objc.ast.QualifiedName) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) VariableElement(javax.lang.model.element.VariableElement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

QualifiedName (com.google.devtools.j2objc.ast.QualifiedName)5 Name (com.google.devtools.j2objc.ast.Name)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 VariableElement (javax.lang.model.element.VariableElement)4 Expression (com.google.devtools.j2objc.ast.Expression)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)3 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)2 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)2 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)2 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)2 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)2 SourcePosition (com.google.devtools.j2objc.ast.SourcePosition)2 SuperFieldAccess (com.google.devtools.j2objc.ast.SuperFieldAccess)2 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)2