"int literal cast to long could be long literal" inspection applicable in more cases

This commit is contained in:
Bas Leijdekkers
2010-12-07 22:06:45 +01:00
parent df115368bf
commit d119950ff9
3 changed files with 53 additions and 21 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2009 Bas Leijdekkers
* Copyright 2009-2010 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,14 +15,14 @@
*/
package com.siyeh.ig.numeric;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.InspectionGadgetsBundle;
import com.intellij.psi.*;
import com.intellij.openapi.project.Project;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -39,27 +39,51 @@ public class IntLiteralMayBeLongLiteralInspection extends BaseInspection {
@Override
@NotNull
protected String buildErrorString(Object... infos) {
final PsiLiteralExpression literalExpression =
(PsiLiteralExpression) infos[0];
final String replacementString = buildReplacementString(literalExpression);
final PsiTypeCastExpression typeCastExpression =
(PsiTypeCastExpression) infos[0];
final StringBuilder replacementText =
buildReplacementText(typeCastExpression, new StringBuilder());
return InspectionGadgetsBundle.message(
"int.literal.may.be.long.literal.problem.descriptor",
replacementString);
replacementText);
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
final PsiLiteralExpression literalExpression =
(PsiLiteralExpression) infos[0];
final String replacementString = buildReplacementString(literalExpression);
return new IntLiteralMayBeLongLiteralFix(replacementString);
final PsiTypeCastExpression typeCastExpression =
(PsiTypeCastExpression) infos[0];
final StringBuilder replacementText =
buildReplacementText(typeCastExpression, new StringBuilder());
return new IntLiteralMayBeLongLiteralFix(replacementText.toString());
}
private static String buildReplacementString(final PsiLiteralExpression literalExpression) {
final PsiElement parent = literalExpression.getParent();
final String originalText = parent instanceof PsiPrefixExpression ?
parent.getText() : literalExpression.getText();
return originalText + 'L';
private static StringBuilder buildReplacementText(
PsiExpression expression, StringBuilder out) {
if (expression instanceof PsiLiteralExpression) {
out.append(expression.getText());
out.append('L');
} else if (expression instanceof PsiPrefixExpression) {
final PsiPrefixExpression prefixExpression =
(PsiPrefixExpression) expression;
final PsiJavaToken sign = prefixExpression.getOperationSign();
out.append(sign.getText());
return buildReplacementText(prefixExpression.getOperand(), out);
} else if (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression =
(PsiParenthesizedExpression) expression;
out.append('(');
buildReplacementText(parenthesizedExpression.getExpression(),
out);
out.append(')');
} else if (expression instanceof PsiTypeCastExpression) {
final PsiTypeCastExpression typeCastExpression =
(PsiTypeCastExpression) expression;
final PsiExpression operand = typeCastExpression.getOperand();
buildReplacementText(operand, out);
} else {
assert false;
}
return out;
}
private static class IntLiteralMayBeLongLiteralFix
@@ -107,7 +131,8 @@ public class IntLiteralMayBeLongLiteralInspection extends BaseInspection {
return;
}
PsiElement parent = expression.getParent();
if (parent instanceof PsiPrefixExpression) {
while (parent instanceof PsiPrefixExpression ||
parent instanceof PsiParenthesizedExpression) {
parent = parent.getParent();
}
if (!(parent instanceof PsiTypeCastExpression)) {
@@ -119,7 +144,7 @@ public class IntLiteralMayBeLongLiteralInspection extends BaseInspection {
if (PsiType.LONG != castType) {
return;
}
registerError(typeCastExpression, expression);
registerError(typeCastExpression, typeCastExpression);
}
}
}
@@ -5,5 +5,6 @@ public class IntLiteralMayBeLong {
void foo() {
System.out.println((long) 1);
System.out.println((long) -/*yes, minus*/1);
System.out.println((long)-(-(6)));
}
}
@@ -12,4 +12,10 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">int literal cast to long could be long literal</problem_class>
<description>&lt;code&gt;(long) -/*yes, minus*/1&lt;/code&gt; could be replaced with '-/*yes, minus*/1L' #loc</description>
</problem>
<problem>
<file>IntLiteralMayBeLong.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">int literal cast to long could be long literal</problem_class>
<description>&lt;code&gt;(long)-(-(6))&lt;/code&gt; could be replaced with '-(-(6L))' #loc</description>
</problem>
</problems>