diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java index 6964b5624ee1..97583663b634 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/CachedNumberConstructorCallInspection.java @@ -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 cachedNumberTypes = new HashSet(); @@ -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); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.after.java new file mode 100644 index 000000000000..48ed16c2fb0e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.after.java @@ -0,0 +1,5 @@ +class Foo { + { + Integer.valueOf(42) + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.java new file mode 100644 index 000000000000..cc5c6560d508 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/numeric/cached/Simple.java @@ -0,0 +1,5 @@ +class Foo { + { + new Integer(42) + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java new file mode 100644 index 000000000000..1c17d16a04df --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/numeric/CachedNumberConstructorCallTest.java @@ -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")); + } + +}