diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/UnnecessaryBoxingInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/UnnecessaryBoxingInspection.java index 71c1e37408f5..fd47cfe2baa0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/UnnecessaryBoxingInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/UnnecessaryBoxingInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2016 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -86,10 +86,6 @@ public class UnnecessaryBoxingInspection extends BaseInspection { @Override public void doFix(@NotNull Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiCallExpression expression = (PsiCallExpression)descriptor.getPsiElement(); - final PsiType boxedType = expression.getType(); - if (boxedType == null) { - return; - } final PsiExpressionList argumentList = expression.getArgumentList(); if (argumentList == null) { return; @@ -110,7 +106,7 @@ public class UnnecessaryBoxingInspection extends BaseInspection { return; } } - final String replacementText = getUnboxedExpressionText(unboxedExpression, boxedType); + final String replacementText = getUnboxedExpressionText(unboxedExpression, expression); if (replacementText == null) { return; } @@ -118,7 +114,11 @@ public class UnnecessaryBoxingInspection extends BaseInspection { } @Nullable - private static String getUnboxedExpressionText(@NotNull PsiExpression unboxedExpression, @NotNull PsiType boxedType) { + private static String getUnboxedExpressionText(@NotNull PsiExpression unboxedExpression, @NotNull PsiExpression boxedExpression) { + final PsiType boxedType = boxedExpression.getType(); + if (boxedType == null) { + return null; + } final PsiType expressionType = unboxedExpression.getType(); if (expressionType == null) { return null; @@ -129,7 +129,13 @@ public class UnnecessaryBoxingInspection extends BaseInspection { } final String text = unboxedExpression.getText(); if (expressionType.equals(unboxedType)) { - return text; + final PsiElement parent = boxedExpression.getParent(); + if (parent instanceof PsiExpression && ParenthesesUtils.areParenthesesNeeded(unboxedExpression, (PsiExpression) parent, false)) { + return '(' + text + ')'; + } + else { + return text; + } } if (unboxedExpression instanceof PsiLiteralExpression) { if (unboxedType.equals(PsiType.LONG) && expressionType.equals(PsiType.INT)) { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/UnnecessaryBoxingFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/UnnecessaryBoxingFixTest.java index ba793c847cec..b44098fb487a 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/UnnecessaryBoxingFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/migration/UnnecessaryBoxingFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2015 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -62,6 +62,18 @@ public class UnnecessaryBoxingFixTest extends IGQuickFixesTestCase { "final Boolean aBoolean = Boolean.TRUE;"); } + public void testStringConcatenation() { + doExpressionTest(InspectionGadgetsBundle.message("unnecessary.boxing.remove.quickfix"), + "\"a\" + Long/**/.valueOf(1L + 2L) + \"b\"", + "\"a\" + (1L + 2L) + \"b\""); + } + + public void testStringConcatenation2() { + doExpressionTest(InspectionGadgetsBundle.message("unnecessary.boxing.remove.quickfix"), + "\"a\" + Long/**/.valueOf(1L - 2L) + \"b\"", + "\"a\" + (1L - 2L) + \"b\""); + } + public void testCast() { doFixTest(); }