Search in sources :

Example 1 with TargetType

use of com.google.errorprone.util.ASTHelpers.TargetType in project error-prone by google.

the class ConditionalExpressionNumericPromotion method matchConditionalExpression.

@Override
public Description matchConditionalExpression(ConditionalExpressionTree conditionalExpression, VisitorState state) {
    Type expressionType = checkNotNull(ASTHelpers.getType(conditionalExpression));
    if (!expressionType.isPrimitive()) {
        return NO_MATCH;
    }
    ExpressionTree trueExpression = conditionalExpression.getTrueExpression();
    ExpressionTree falseExpression = conditionalExpression.getFalseExpression();
    Type trueType = checkNotNull(ASTHelpers.getType(trueExpression));
    Type falseType = checkNotNull(ASTHelpers.getType(falseExpression));
    if (trueType.isPrimitive() || falseType.isPrimitive()) {
        return NO_MATCH;
    }
    if (ASTHelpers.isSameType(trueType, falseType, state)) {
        return NO_MATCH;
    }
    TargetType targetType = ASTHelpers.targetType(state);
    if (targetType == null) {
        return NO_MATCH;
    }
    if (targetType.type().isPrimitive()) {
        return NO_MATCH;
    }
    Type numberType = state.getTypeFromString("java.lang.Number");
    if (ASTHelpers.isSubtype(targetType.type(), numberType, state) && !ASTHelpers.isSameType(targetType.type(), numberType, state)) {
        return NO_MATCH;
    }
    SuggestedFix.Builder builder = SuggestedFix.builder();
    String numberName = SuggestedFixes.qualifyType(state, builder, numberType);
    String prefix = "((" + numberName + ") ";
    builder.prefixWith(trueExpression, prefix).postfixWith(trueExpression, ")");
    builder.prefixWith(falseExpression, prefix).postfixWith(falseExpression, ")");
    return describeMatch(conditionalExpression, builder.build());
}
Also used : TargetType(com.google.errorprone.util.ASTHelpers.TargetType) Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TargetType(com.google.errorprone.util.ASTHelpers.TargetType) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 TargetType (com.google.errorprone.util.ASTHelpers.TargetType)1 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 Type (com.sun.tools.javac.code.Type)1