Matchers.<ExpressionTree>anything()),
state);
if (binaryTreeMatches == null) {
throw new IllegalStateException("Expected one of the operands to be a literal");
}
JCLiteral literal = (JCLiteral) binaryTreeMatches.get(0);
JCTree nonLiteralOperand = (JCTree) binaryTreeMatches.get(1);
boolean byteMatch = state.getTypes().isSameType(nonLiteralOperand.type,
state.getSymtab().byteType);
boolean willEvaluateTo = (tree.getKind() != Kind.EQUAL_TO);
Fix fix;
String customDiagnosticMessage;
if (byteMatch) {
String replacement = Byte.toString(((Number) literal.getValue()).byteValue());
// Correct for poor javac 6 literal parsing.
int actualStart = ASTHelpers.getActualStartPosition(literal, state.getSourceCode());
if (actualStart != literal.getStartPosition()) {
fix =
SuggestedFix.replace(literal, replacement, actualStart - literal.getStartPosition(), 0);
} else {
fix = SuggestedFix.replace(literal, replacement);
}
customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, "byte", (int) Byte.MIN_VALUE,
(int) Byte.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
} else {
fix = SuggestedFix.replace(tree, Boolean.toString(willEvaluateTo));
customDiagnosticMessage = String.format(MESSAGE_TEMPLATE, "char", (int) Character.MIN_VALUE,
(int) Character.MAX_VALUE, literal.toString(), Boolean.toString(willEvaluateTo));
}
return Description.builder(tree, pattern)
.addFix(fix)
.setMessage(customDiagnosticMessage)
.build();