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());
}
Aggregations