use of com.android.tools.klint.client.api.JavaParser.TypeDescriptor 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;
}
Aggregations