Search in sources :

Example 56 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class DomPsiParser method parseXml.

@Nullable
@Override
public Document parseXml(@NonNull final XmlContext context) {
    assert myReadLock == null;
    myReadLock = ApplicationManager.getApplication().acquireReadActionLock();
    Document document = parse(context);
    if (document == null) {
        myReadLock.finish();
        myReadLock = null;
    }
    return document;
}
Also used : Document(org.w3c.dom.Document) Nullable(com.android.annotations.Nullable)

Example 57 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class DomPsiParser method parse.

@Nullable
private Document parse(XmlContext context) {
    // Should only be called from read thread
    assert ApplicationManager.getApplication().isReadAccessAllowed();
    final PsiFile psiFile = LintIdeUtils.getPsiFile(context);
    if (!(psiFile instanceof XmlFile)) {
        return null;
    }
    XmlFile xmlFile = (XmlFile) psiFile;
    try {
        return DomPsiConverter.convert(xmlFile);
    } catch (Throwable t) {
        myClient.log(t, "Failed converting PSI parse tree to DOM for file %1$s", context.file.getPath());
        return null;
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Nullable(com.android.annotations.Nullable)

Example 58 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class LintIdeUtils method getPsiFile.

/**
   * Returns the {@link PsiFile} associated with a given lint {@link Context}
   *
   * @param context the context to look up the file for
   * @return the corresponding {@link PsiFile}, or null
   */
@Nullable
public static PsiFile getPsiFile(@NonNull Context context) {
    LintRequest request = context.getDriver().getRequest();
    Project project = ((LintIdeRequest) request).getProject();
    if (project.isDisposed()) {
        return null;
    }
    VirtualFile file = VfsUtil.findFileByIoFile(context.file, false);
    if (file == null) {
        return null;
    }
    return AndroidPsiUtils.getPsiFileSafely(project, file);
}
Also used : LintRequest(com.android.tools.lint.client.api.LintRequest) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Nullable(com.android.annotations.Nullable)

Example 59 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class LombokPsiConverter method toExpression.

@Nullable
private static Expression toExpression(@NonNull PsiExpression expression) {
    if (expression instanceof PsiLiteralExpression) {
        // Literal does not implement Expression, but StringLiteral, BooleanLiteral, IntegralLiteral etc all do
        return (Expression) toLiteral((PsiLiteralExpression) expression);
    } else if (expression instanceof PsiBinaryExpression) {
        PsiBinaryExpression p = (PsiBinaryExpression) expression;
        BinaryExpression binary = new BinaryExpression();
        bind(binary, expression);
        PsiExpression rExpression = p.getROperand();
        if (rExpression == null) {
            return null;
        }
        Expression left = toExpression(p.getLOperand());
        Expression right = toExpression(rExpression);
        if (left == null || right == null) {
            // Errors in source code
            return null;
        }
        binary.astRight(right);
        binary.astLeft(left);
        IElementType operation = p.getOperationTokenType();
        BinaryOperator operator = convertOperation(operation);
        if (operator != null) {
            binary.astOperator(operator);
        } else {
            assert false : operation;
        }
        return binary;
    } else if (expression instanceof PsiAssignmentExpression) {
        PsiAssignmentExpression p = (PsiAssignmentExpression) expression;
        BinaryExpression binary = new BinaryExpression();
        bind(binary, expression);
        BinaryOperator operator = BinaryOperator.ASSIGN;
        IElementType operation = p.getOperationTokenType();
        if (operation == JavaTokenType.PLUSEQ) {
            operator = BinaryOperator.PLUS_ASSIGN;
        } else if (operation == JavaTokenType.MINUSEQ) {
            operator = BinaryOperator.MINUS_ASSIGN;
        } else if (operation == JavaTokenType.ASTERISKEQ) {
            operator = BinaryOperator.MULTIPLY_ASSIGN;
        } else if (operation == JavaTokenType.DIVEQ) {
            operator = BinaryOperator.DIVIDE_ASSIGN;
        } else if (operation == JavaTokenType.PERCEQ) {
            operator = BinaryOperator.REMAINDER_ASSIGN;
        } else if (operation == JavaTokenType.ANDEQ) {
            operator = BinaryOperator.AND_ASSIGN;
        } else if (operation == JavaTokenType.XOREQ) {
            operator = BinaryOperator.XOR_ASSIGN;
        } else if (operation == JavaTokenType.OREQ) {
            operator = BinaryOperator.OR_ASSIGN;
        } else if (operation == JavaTokenType.LTLTEQ) {
            operator = BinaryOperator.SHIFT_LEFT_ASSIGN;
        } else if (operation == JavaTokenType.GTGTEQ) {
            operator = BinaryOperator.SHIFT_RIGHT_ASSIGN;
        } else if (operation == JavaTokenType.GTGTGTEQ) {
            operator = BinaryOperator.BITWISE_SHIFT_RIGHT_ASSIGN;
        }
        binary.astOperator(operator);
        PsiExpression rExpression = p.getRExpression();
        if (rExpression == null) {
            return null;
        }
        Expression left = toExpression(p.getLExpression());
        Expression right = toExpression(rExpression);
        if (left == null || right == null) {
            // Error in source code
            return null;
        }
        binary.astLeft(left);
        binary.astRight(right);
        return binary;
    } else if (expression instanceof PsiQualifiedExpression) {
        PsiQualifiedExpression p = (PsiQualifiedExpression) expression;
        PsiJavaCodeReferenceElement qualifier = p.getQualifier();
        if (qualifier != null) {
            Select operand = toSelect(qualifier);
            Select select = new Select();
            bind(select, expression);
            select.astOperand(operand);
            PsiReference reference = p.getReference();
            if (reference != null) {
                PsiElement referenceElement = reference.getElement();
                Identifier identifier = toIdentifier(referenceElement.getText());
                bind(identifier, referenceElement);
                select.astIdentifier(identifier);
            }
            return select;
        }
        PsiReference reference = p.getReference();
        if (reference != null) {
            return toVariableReference(reference);
        }
        if (p instanceof PsiSuperExpression) {
            Super superExpression = new Super();
            bind(superExpression, p);
            return superExpression;
        }
        return toVariableReference(p.getText(), p);
    } else if (expression instanceof PsiReferenceExpression) {
        PsiReferenceExpression refExpression = (PsiReferenceExpression) expression;
        PsiElement qualifier = refExpression.getQualifier();
        if (qualifier == null) {
            assert refExpression.getReferenceName() != null;
            PsiReference reference = refExpression.getReference();
            if (reference != null) {
                return toVariableReference(reference);
            }
            return null;
        }
        return toSelect(refExpression);
    } else if (expression instanceof PsiCallExpression) {
        return toMethodInvocation(expression);
    } else if (expression instanceof PsiArrayAccessExpression) {
        PsiArrayAccessExpression p = (PsiArrayAccessExpression) expression;
        ArrayAccess arrayAccess = new ArrayAccess();
        bind(arrayAccess, p);
        PsiExpression indexExpression = p.getIndexExpression();
        if (indexExpression != null) {
            Expression e = toExpression(indexExpression);
            if (e != null) {
                arrayAccess.astIndexExpression(e);
            }
        }
        Expression e = toExpression(p.getArrayExpression());
        if (e == null) {
            return null;
        }
        arrayAccess.astOperand(e);
        bind(arrayAccess, expression);
        return arrayAccess;
    } else if (expression instanceof PsiArrayInitializerExpression) {
        return toArrayInitializer(expression);
    } else if (expression instanceof PsiInstanceOfExpression) {
        PsiInstanceOfExpression p = (PsiInstanceOfExpression) expression;
        InstanceOf instanceOf = new InstanceOf();
        bind(instanceOf, expression);
        PsiTypeElement checkType = p.getCheckType();
        if (checkType != null) {
            instanceOf.astTypeReference(toTypeReference(checkType));
        }
        Expression e = toExpression(p.getOperand());
        if (e == null) {
            return null;
        }
        instanceOf.astObjectReference(e);
        return instanceOf;
    } else if (expression instanceof PsiConditionalExpression) {
        PsiConditionalExpression p = (PsiConditionalExpression) expression;
        InlineIfExpression inlineIf = new InlineIfExpression();
        bind(inlineIf, expression);
        Expression condition = toExpression(p.getCondition());
        PsiExpression thenExpression = p.getThenExpression();
        PsiExpression elseExpression = p.getElseExpression();
        if (condition == null || thenExpression == null || elseExpression == null) {
            return null;
        }
        Expression ifTrue = toExpression(thenExpression);
        Expression ifFalse = toExpression(elseExpression);
        if (ifTrue == null || ifFalse == null) {
            return null;
        }
        inlineIf.astCondition(condition);
        inlineIf.astIfTrue(ifTrue);
        inlineIf.astIfFalse(ifFalse);
        return inlineIf;
    } else if (expression instanceof PsiClassObjectAccessExpression) {
        PsiClassObjectAccessExpression p = (PsiClassObjectAccessExpression) expression;
        ClassLiteral literal = new ClassLiteral();
        bind(literal, p);
        literal.astTypeReference(toTypeReference(p.getOperand()));
        return literal;
    } else if (expression instanceof PsiParenthesizedExpression) {
        PsiParenthesizedExpression p = (PsiParenthesizedExpression) expression;
        PsiExpression e = p.getExpression();
        if (e != null) {
            return toExpression(e);
        }
        return null;
    } else if (expression instanceof PsiTypeCastExpression) {
        PsiTypeCastExpression p = (PsiTypeCastExpression) expression;
        Cast cast = new Cast();
        bind(cast, expression);
        PsiTypeElement castType = p.getCastType();
        if (castType != null) {
            cast.astTypeReference(toTypeReference(castType));
        }
        PsiExpression operand = p.getOperand();
        if (operand != null) {
            Expression e = toExpression(operand);
            if (e == null) {
                return null;
            }
            cast.astOperand(e);
        }
        return cast;
    } else if (expression instanceof PsiPostfixExpression) {
        PsiPostfixExpression p = (PsiPostfixExpression) expression;
        UnaryExpression unary = new UnaryExpression();
        bind(unary, expression);
        IElementType operation = p.getOperationTokenType();
        UnaryOperator operator = null;
        if (operation == JavaTokenType.MINUSMINUS) {
            operator = UnaryOperator.POSTFIX_DECREMENT;
        } else if (operation == JavaTokenType.PLUSPLUS) {
            operator = UnaryOperator.POSTFIX_INCREMENT;
        }
        if (operator != null) {
            unary.astOperator(operator);
        } else {
            assert false : operation;
        }
        Expression operand = toExpression(p.getOperand());
        if (operand == null) {
            return null;
        }
        unary.astOperand(operand);
        return unary;
    } else if (expression instanceof PsiPrefixExpression) {
        PsiPrefixExpression p = (PsiPrefixExpression) expression;
        UnaryExpression unary = new UnaryExpression();
        bind(unary, expression);
        IElementType operation = p.getOperationTokenType();
        UnaryOperator operator = null;
        if (operation == JavaTokenType.MINUSMINUS) {
            operator = UnaryOperator.PREFIX_DECREMENT;
        } else if (operation == JavaTokenType.PLUSPLUS) {
            operator = UnaryOperator.PREFIX_INCREMENT;
        } else if (operation == JavaTokenType.MINUS) {
            operator = UnaryOperator.UNARY_MINUS;
        } else if (operation == JavaTokenType.PLUS) {
            operator = UnaryOperator.UNARY_PLUS;
        } else if (operation == JavaTokenType.EXCL) {
            operator = UnaryOperator.LOGICAL_NOT;
        } else if (operation == JavaTokenType.TILDE) {
            operator = UnaryOperator.BINARY_NOT;
        }
        if (operator != null) {
            unary.astOperator(operator);
        } else {
            assert false : operation;
        }
        PsiExpression operand = p.getOperand();
        if (operand != null) {
            Expression e = toExpression(operand);
            if (e == null) {
                return null;
            }
            unary.astOperand(e);
        }
        return unary;
    } else if (expression instanceof PsiPolyadicExpression) {
        // Example: A + B + C + D; IDEA parses this as a single
        // polyadic expression with operator type + of A, B, C and D.
        // Fold them into nested BinaryExpressions for Lombok.
        PsiPolyadicExpression p = (PsiPolyadicExpression) expression;
        IElementType operation = p.getOperationTokenType();
        BinaryOperator operator = convertOperation(operation);
        if (operator == null) {
            assert false : operation;
        }
        PsiExpression[] operands = p.getOperands();
        assert operands.length >= 1;
        Expression left = toExpression(operands[0]);
        if (left == null) {
            return null;
        }
        for (int i = 1, n = operands.length; i < n; i++) {
            Expression right = toExpression(operands[i]);
            if (right == null) {
                // Error in source code
                break;
            }
            BinaryExpression binary = new BinaryExpression();
            bind(binary, expression);
            binary.astOperator(operator);
            binary.astLeft(left);
            binary.astRight(right);
            left = binary;
        }
        return left;
    } else //noinspection IfStatementWithIdenticalBranches
    if (expression instanceof PsiLambdaExpression) {
        // We need a proper AST; Lombok is stuck at Java language level 6.
        return null;
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Nullable(com.android.annotations.Nullable)

Example 60 with Nullable

use of com.android.annotations.Nullable in project android by JetBrains.

the class EclipseProject method getInstrumentationTarget.

@Nullable
private static String getInstrumentationTarget(@NonNull GradleImport importer, @NonNull File manifest) throws IOException {
    Document doc = importer.getXmlDocument(manifest, true);
    if (doc != null) {
        NodeList list = doc.getElementsByTagName(NODE_INSTRUMENTATION);
        for (int i = 0; i < list.getLength(); i++) {
            Element tag = (Element) list.item(i);
            String target = tag.getAttributeNS(ANDROID_URI, ATTRIBUTE_TARGET_PACKAGE);
            if (target != null && !target.isEmpty()) {
                return target;
            }
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Nullable(com.android.annotations.Nullable)

Aggregations

Nullable (com.android.annotations.Nullable)83 File (java.io.File)21 IOException (java.io.IOException)9 ResourceType (com.android.resources.ResourceType)8 PsiClass (com.intellij.psi.PsiClass)7 PsiElement (com.intellij.psi.PsiElement)7 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)7 ResourceUrl (com.android.ide.common.resources.ResourceUrl)6 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)6 PsiExpression (com.intellij.psi.PsiExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiStatement (com.intellij.psi.PsiStatement)6 ArrayList (java.util.ArrayList)6 PsiField (com.intellij.psi.PsiField)5 PsiFile (com.intellij.psi.PsiFile)4 Node (lombok.ast.Node)4 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)4 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)3