Search in sources :

Example 1 with ResolvedClass

use of com.android.tools.klint.client.api.JavaParser.ResolvedClass in project kotlin by JetBrains.

the class JavaContext method isContextMethod.

/**
     * Returns true if the given method invocation node corresponds to a call on a
     * {@code android.content.Context}
     *
     * @param node the method call node
     * @return true iff the method call is on a class extending context
     * @deprecated use {@link JavaEvaluator#isMemberInSubClassOf(PsiMember, String, boolean)} instead
     */
@Deprecated
public boolean isContextMethod(@NonNull MethodInvocation node) {
    // Method name used in many other contexts where it doesn't have the
    // same semantics; only use this one if we can resolve types
    // and we're certain this is the Context method
    ResolvedNode resolved = resolve(node);
    if (resolved instanceof JavaParser.ResolvedMethod) {
        JavaParser.ResolvedMethod method = (JavaParser.ResolvedMethod) resolved;
        ResolvedClass containingClass = method.getContainingClass();
        if (containingClass.isSubclassOf(CLASS_CONTEXT, false)) {
            return true;
        }
    }
    return false;
}
Also used : JavaParser(com.android.tools.klint.client.api.JavaParser) ResolvedNode(com.android.tools.klint.client.api.JavaParser.ResolvedNode) ResolvedClass(com.android.tools.klint.client.api.JavaParser.ResolvedClass)

Example 2 with ResolvedClass

use of com.android.tools.klint.client.api.JavaParser.ResolvedClass in project kotlin by JetBrains.

the class JavaVisitor method getInterfaceNames.

@Nullable
private static Set<String> getInterfaceNames(@Nullable Set<String> addTo, @NonNull ResolvedClass cls) {
    Iterable<ResolvedClass> interfaces = cls.getInterfaces();
    for (ResolvedClass resolvedInterface : interfaces) {
        String name = resolvedInterface.getName();
        if (addTo == null) {
            addTo = Sets.newHashSet();
        } else if (addTo.contains(name)) {
            // more than once.
            continue;
        }
        addTo.add(name);
        getInterfaceNames(addTo, resolvedInterface);
    }
    return addTo;
}
Also used : ResolvedClass(com.android.tools.klint.client.api.JavaParser.ResolvedClass) Nullable(com.android.annotations.Nullable)

Example 3 with ResolvedClass

use of com.android.tools.klint.client.api.JavaParser.ResolvedClass in project kotlin by JetBrains.

the class TypeEvaluator method evaluate.

/**
     * Returns the inferred type of the given node
     * @deprecated Use {@link #evaluate(PsiElement)} instead
     */
@Deprecated
@Nullable
public TypeDescriptor evaluate(@NonNull Node node) {
    ResolvedNode resolved = null;
    if (mContext != null) {
        resolved = mContext.resolve(node);
    }
    if (resolved instanceof ResolvedMethod) {
        TypeDescriptor type;
        ResolvedMethod method = (ResolvedMethod) resolved;
        if (method.isConstructor()) {
            ResolvedClass containingClass = method.getContainingClass();
            type = containingClass.getType();
        } else {
            type = method.getReturnType();
        }
        return type;
    }
    if (resolved instanceof ResolvedField) {
        ResolvedField field = (ResolvedField) resolved;
        Node astNode = field.findAstNode();
        if (astNode instanceof VariableDeclaration) {
            VariableDeclaration declaration = (VariableDeclaration) astNode;
            VariableDefinition definition = declaration.astDefinition();
            if (definition != null) {
                VariableDefinitionEntry first = definition.astVariables().first();
                if (first != null) {
                    Expression initializer = first.astInitializer();
                    if (initializer != null) {
                        TypeDescriptor type = evaluate(initializer);
                        if (type != null) {
                            return type;
                        }
                    }
                }
            }
        }
        return field.getType();
    }
    if (node instanceof VariableReference) {
        Statement statement = getParentOfType(node, Statement.class, false);
        if (statement != null) {
            ListIterator<Node> iterator = statement.getParent().getChildren().listIterator();
            while (iterator.hasNext()) {
                if (iterator.next() == statement) {
                    if (iterator.hasPrevious()) {
                        // should always be true
                        iterator.previous();
                    }
                    break;
                }
            }
            String targetName = ((VariableReference) node).astIdentifier().astValue();
            while (iterator.hasPrevious()) {
                Node previous = iterator.previous();
                if (previous instanceof VariableDeclaration) {
                    VariableDeclaration declaration = (VariableDeclaration) previous;
                    VariableDefinition definition = declaration.astDefinition();
                    for (VariableDefinitionEntry entry : definition.astVariables()) {
                        if (entry.astInitializer() != null && entry.astName().astValue().equals(targetName)) {
                            return evaluate(entry.astInitializer());
                        }
                    }
                } else if (previous instanceof ExpressionStatement) {
                    ExpressionStatement expressionStatement = (ExpressionStatement) previous;
                    Expression expression = expressionStatement.astExpression();
                    if (expression instanceof BinaryExpression && ((BinaryExpression) expression).astOperator() == BinaryOperator.ASSIGN) {
                        BinaryExpression binaryExpression = (BinaryExpression) expression;
                        if (targetName.equals(binaryExpression.astLeft().toString())) {
                            return evaluate(binaryExpression.astRight());
                        }
                    }
                }
            }
        }
    } else if (node instanceof Cast) {
        Cast cast = (Cast) node;
        if (mContext != null) {
            ResolvedNode typeReference = mContext.resolve(cast.astTypeReference());
            if (typeReference instanceof ResolvedClass) {
                return ((ResolvedClass) typeReference).getType();
            }
        }
        TypeDescriptor viewType = evaluate(cast.astOperand());
        if (viewType != null) {
            return viewType;
        }
    } else if (node instanceof Literal) {
        if (node instanceof NullLiteral) {
            return null;
        } else if (node instanceof BooleanLiteral) {
            return new DefaultTypeDescriptor(TYPE_BOOLEAN);
        } else if (node instanceof StringLiteral) {
            return new DefaultTypeDescriptor(TYPE_STRING);
        } else if (node instanceof CharLiteral) {
            return new DefaultTypeDescriptor(TYPE_CHAR);
        } else if (node instanceof IntegralLiteral) {
            IntegralLiteral literal = (IntegralLiteral) node;
            // Don't combine to ?: since that will promote astIntValue to a long
            if (literal.astMarkedAsLong()) {
                return new DefaultTypeDescriptor(TYPE_LONG);
            } else {
                return new DefaultTypeDescriptor(TYPE_INT);
            }
        } else if (node instanceof FloatingPointLiteral) {
            FloatingPointLiteral literal = (FloatingPointLiteral) node;
            // Don't combine to ?: since that will promote astFloatValue to a double
            if (literal.astMarkedAsFloat()) {
                return new DefaultTypeDescriptor(TYPE_FLOAT);
            } else {
                return new DefaultTypeDescriptor(TYPE_DOUBLE);
            }
        }
    } else if (node instanceof UnaryExpression) {
        return evaluate(((UnaryExpression) node).astOperand());
    } else if (node instanceof InlineIfExpression) {
        InlineIfExpression expression = (InlineIfExpression) node;
        if (expression.astIfTrue() != null) {
            return evaluate(expression.astIfTrue());
        } else if (expression.astIfFalse() != null) {
            return evaluate(expression.astIfFalse());
        }
    } else if (node instanceof BinaryExpression) {
        BinaryExpression expression = (BinaryExpression) node;
        BinaryOperator operator = expression.astOperator();
        switch(operator) {
            case LOGICAL_OR:
            case LOGICAL_AND:
            case EQUALS:
            case NOT_EQUALS:
            case GREATER:
            case GREATER_OR_EQUAL:
            case LESS:
            case LESS_OR_EQUAL:
                return new DefaultTypeDescriptor(TYPE_BOOLEAN);
        }
        TypeDescriptor type = evaluate(expression.astLeft());
        if (type != null) {
            return type;
        }
        return evaluate(expression.astRight());
    }
    if (resolved instanceof ResolvedVariable) {
        ResolvedVariable variable = (ResolvedVariable) resolved;
        return variable.getType();
    }
    return null;
}
Also used : Cast(lombok.ast.Cast) VariableDefinition(lombok.ast.VariableDefinition) BooleanLiteral(lombok.ast.BooleanLiteral) Node(lombok.ast.Node) ResolvedNode(com.android.tools.klint.client.api.JavaParser.ResolvedNode) UnaryExpression(lombok.ast.UnaryExpression) DefaultTypeDescriptor(com.android.tools.klint.client.api.JavaParser.DefaultTypeDescriptor) IntegralLiteral(lombok.ast.IntegralLiteral) ResolvedNode(com.android.tools.klint.client.api.JavaParser.ResolvedNode) ResolvedField(com.android.tools.klint.client.api.JavaParser.ResolvedField) BinaryExpression(lombok.ast.BinaryExpression) IntegralLiteral(lombok.ast.IntegralLiteral) BooleanLiteral(lombok.ast.BooleanLiteral) Literal(lombok.ast.Literal) FloatingPointLiteral(lombok.ast.FloatingPointLiteral) CharLiteral(lombok.ast.CharLiteral) StringLiteral(lombok.ast.StringLiteral) NullLiteral(lombok.ast.NullLiteral) VariableDeclaration(lombok.ast.VariableDeclaration) InlineIfExpression(lombok.ast.InlineIfExpression) BinaryOperator(lombok.ast.BinaryOperator) ResolvedVariable(com.android.tools.klint.client.api.JavaParser.ResolvedVariable) VariableReference(lombok.ast.VariableReference) CharLiteral(lombok.ast.CharLiteral) Statement(lombok.ast.Statement) PsiStatement(com.intellij.psi.PsiStatement) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) ExpressionStatement(lombok.ast.ExpressionStatement) PsiDeclarationStatement(com.intellij.psi.PsiDeclarationStatement) ResolvedClass(com.android.tools.klint.client.api.JavaParser.ResolvedClass) ResolvedMethod(com.android.tools.klint.client.api.JavaParser.ResolvedMethod) TypeDescriptor(com.android.tools.klint.client.api.JavaParser.TypeDescriptor) DefaultTypeDescriptor(com.android.tools.klint.client.api.JavaParser.DefaultTypeDescriptor) StringLiteral(lombok.ast.StringLiteral) VariableDefinitionEntry(lombok.ast.VariableDefinitionEntry) UExpression(org.jetbrains.uast.UExpression) UCallExpression(org.jetbrains.uast.UCallExpression) PsiAssignmentExpression(com.intellij.psi.PsiAssignmentExpression) UReferenceExpression(org.jetbrains.uast.UReferenceExpression) UnaryExpression(lombok.ast.UnaryExpression) InlineIfExpression(lombok.ast.InlineIfExpression) PsiExpression(com.intellij.psi.PsiExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) Expression(lombok.ast.Expression) BinaryExpression(lombok.ast.BinaryExpression) FloatingPointLiteral(lombok.ast.FloatingPointLiteral) PsiExpressionStatement(com.intellij.psi.PsiExpressionStatement) ExpressionStatement(lombok.ast.ExpressionStatement) NullLiteral(lombok.ast.NullLiteral) Nullable(com.android.annotations.Nullable)

Aggregations

ResolvedClass (com.android.tools.klint.client.api.JavaParser.ResolvedClass)3 Nullable (com.android.annotations.Nullable)2 ResolvedNode (com.android.tools.klint.client.api.JavaParser.ResolvedNode)2 JavaParser (com.android.tools.klint.client.api.JavaParser)1 DefaultTypeDescriptor (com.android.tools.klint.client.api.JavaParser.DefaultTypeDescriptor)1 ResolvedField (com.android.tools.klint.client.api.JavaParser.ResolvedField)1 ResolvedMethod (com.android.tools.klint.client.api.JavaParser.ResolvedMethod)1 ResolvedVariable (com.android.tools.klint.client.api.JavaParser.ResolvedVariable)1 TypeDescriptor (com.android.tools.klint.client.api.JavaParser.TypeDescriptor)1 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)1 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)1 PsiExpression (com.intellij.psi.PsiExpression)1 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)1 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)1 PsiStatement (com.intellij.psi.PsiStatement)1 BinaryExpression (lombok.ast.BinaryExpression)1 BinaryOperator (lombok.ast.BinaryOperator)1 BooleanLiteral (lombok.ast.BooleanLiteral)1 Cast (lombok.ast.Cast)1 CharLiteral (lombok.ast.CharLiteral)1