diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/IntLiteralMayBeLongLiteralInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/IntLiteralMayBeLongLiteralInspection.java
index a87c52d30f63..81f136819106 100644
--- a/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/IntLiteralMayBeLongLiteralInspection.java
+++ b/plugins/InspectionGadgets/src/com/siyeh/ig/numeric/IntLiteralMayBeLongLiteralInspection.java
@@ -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);
}
}
}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/IntLiteralMayBeLong.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/IntLiteralMayBeLong.java
index 16af0f5f87f6..5fcd7f9e2116 100644
--- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/IntLiteralMayBeLong.java
+++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/IntLiteralMayBeLong.java
@@ -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)));
}
}
diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/expected.xml
index bfe7dd253337..c91e4262b3d4 100644
--- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/expected.xml
+++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/int_literal_may_be_long/expected.xml
@@ -12,4 +12,10 @@
int literal cast to long could be long literal
<code>(long) -/*yes, minus*/1</code> could be replaced with '-/*yes, minus*/1L' #loc
+
+ IntLiteralMayBeLong.java
+ 8
+ int literal cast to long could be long literal
+ <code>(long)-(-(6))</code> could be replaced with '-(-(6L))' #loc
+
\ No newline at end of file