IDEA-122388 Replace with 'Integer.valueOf()' call raises Exception

This commit is contained in:
Anna Kozlova
2014-03-18 20:00:58 +01:00
parent a97bc5c796
commit d452ad6689
4 changed files with 57 additions and 14 deletions
@@ -19,6 +19,7 @@ import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
@@ -34,8 +35,7 @@ import javax.swing.*;
import java.util.HashSet;
import java.util.Set;
public class CachedNumberConstructorCallInspection
extends BaseInspection {
public class CachedNumberConstructorCallInspection extends BaseInspection {
private static final Set<String> cachedNumberTypes = new HashSet<String>();
@@ -78,15 +78,13 @@ public class CachedNumberConstructorCallInspection
@Override
public InspectionGadgetsFix buildFix(Object... infos) {
final PsiNewExpression expression = (PsiNewExpression)infos[0];
final PsiJavaCodeReferenceElement classReference =
expression.getClassReference();
final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
assert classReference != null;
final String className = classReference.getText();
return new CachedNumberConstructorCallFix(className);
}
private static class CachedNumberConstructorCallFix
extends InspectionGadgetsFix {
private static class CachedNumberConstructorCallFix extends InspectionGadgetsFix {
private final String className;
@@ -110,8 +108,8 @@ public class CachedNumberConstructorCallInspection
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
final PsiNewExpression expression =
(PsiNewExpression)descriptor.getPsiElement();
final PsiNewExpression expression = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiNewExpression.class, false);
assert expression != null;
final PsiExpressionList argList = expression.getArgumentList();
assert argList != null;
final PsiExpression[] args = argList.getExpressions();
@@ -121,8 +119,7 @@ public class CachedNumberConstructorCallInspection
}
}
private static class LongConstructorVisitor
extends BaseInspectionVisitor {
private static class LongConstructorVisitor extends BaseInspectionVisitor {
@Override
public void visitNewExpression(
@@ -140,8 +137,7 @@ public class CachedNumberConstructorCallInspection
return;
}
final PsiClass aClass = ClassUtils.getContainingClass(expression);
if (aClass != null &&
cachedNumberTypes.contains(aClass.getQualifiedName())) {
if (aClass != null && cachedNumberTypes.contains(aClass.getQualifiedName())) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
@@ -154,8 +150,7 @@ public class CachedNumberConstructorCallInspection
}
final PsiExpression argument = arguments[0];
final PsiType argumentType = argument.getType();
if (argumentType == null ||
(ignoreStringArguments && argumentType.equalsToText(CommonClassNames.JAVA_LANG_STRING))) {
if (argumentType == null || (ignoreStringArguments && argumentType.equalsToText(CommonClassNames.JAVA_LANG_STRING))) {
return;
}
registerNewExpressionError(expression, expression);
@@ -0,0 +1,5 @@
class Foo {
{
Integer.valueOf(42)
}
}
@@ -0,0 +1,5 @@
class Foo {
{
new Integ<caret>er(42)
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.fixes.numeric;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.numeric.CachedNumberConstructorCallInspection;
public class CachedNumberConstructorCallTest extends IGQuickFixesTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new CachedNumberConstructorCallInspection());
}
@Override
protected String getRelativePath() {
return "numeric/cached";
}
public void testSimple() {
doTest(InspectionGadgetsBundle.message("cached.number.constructor.call.quickfix", "Integer"));
}
}