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 05c42e0ee022..a808e6011bda 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-2014 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2015 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. @@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.psiutils.ExpectedTypeUtils; +import com.siyeh.ig.psiutils.ExpressionUtils; import com.siyeh.ig.psiutils.MethodCallUtils; import com.siyeh.ig.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NonNls; @@ -276,27 +277,30 @@ public class UnnecessaryBoxingInspection extends BaseInspection { if (rhs == null) { return false; } - final PsiType rhsType = rhs.getType(); - if (rhsType == null) { - return false; - } - final PsiType lhsType = lhs.getType(); - if (lhsType == null) { - return false; - } - if (PsiTreeUtil.isAncestor(rhs, expression, false)) { - final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(rhsType); - return unboxedType != null && unboxedType.isAssignableFrom(lhsType); - } - else { - final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(lhsType); - return unboxedType != null && unboxedType.isAssignableFrom(rhsType); - } + return PsiTreeUtil.isAncestor(rhs, expression, false) + ? canBinaryExpressionBeUnboxed(lhs, rhs) + : canBinaryExpressionBeUnboxed(rhs, lhs); } final PsiCallExpression containingMethodCallExpression = getParentMethodCallExpression(expression); return containingMethodCallExpression == null || isSameMethodCalledWithoutBoxing(containingMethodCallExpression, expression); } + private boolean canBinaryExpressionBeUnboxed(PsiExpression lhs, PsiExpression rhs) { + final PsiType rhsType = rhs.getType(); + if (rhsType == null) { + return false; + } + final PsiType lhsType = lhs.getType(); + if (lhsType == null) { + return false; + } + if (!(lhsType instanceof PsiPrimitiveType) && !ExpressionUtils.isAnnotatedNotNull(lhs)) { + return false; + } + final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(rhsType); + return unboxedType != null && unboxedType.isAssignableFrom(lhsType); + } + @Nullable private PsiCallExpression getParentMethodCallExpression(@NotNull PsiElement expression) { final PsiElement parent = expression.getParent(); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java index 950d97de397c..deb3e8d933ef 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpressionUtils.java @@ -15,6 +15,7 @@ */ package com.siyeh.ig.psiutils; +import com.intellij.codeInsight.NullableNotNullManager; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; @@ -647,4 +648,28 @@ public class ExpressionUtils { final PsiType type = expression.getType(); return type != null && type.equalsToText(CommonClassNames.JAVA_LANG_STRING); } + + public static boolean isAnnotatedNotNull(PsiExpression expression) { + return isAnnotated(expression, false); + } + + public static boolean isAnnotatedNullable(PsiExpression expression) { + return isAnnotated(expression, true); + } + + private static boolean isAnnotated(PsiExpression expression, boolean nullable) { + expression = ParenthesesUtils.stripParentheses(expression); + if (!(expression instanceof PsiReferenceExpression)) { + return false; + } + final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression; + final PsiElement target = referenceExpression.resolve(); + if (!(target instanceof PsiModifierListOwner)) { + return false; + } + final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner)target; + return nullable ? + NullableNotNullManager.isNullable(modifierListOwner): + NullableNotNullManager.isNotNull(modifierListOwner); + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java index 115a38d93133..4e89562009cb 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/SwitchUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2014 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2015 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. @@ -15,7 +15,6 @@ */ package com.siyeh.ig.psiutils; -import com.intellij.codeInsight.NullableNotNullManager; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; @@ -121,7 +120,7 @@ public class SwitchUtils { return true; } else if (type instanceof PsiClassType) { - if (isAnnotatedNullable(expression)) { + if (ExpressionUtils.isAnnotatedNullable(expression)) { return false; } if (type.equalsToText(CommonClassNames.JAVA_LANG_CHARACTER) || type.equalsToText(CommonClassNames.JAVA_LANG_BYTE) || @@ -204,7 +203,7 @@ public class SwitchUtils { return null; } if (PsiUtil.isConstantExpression(qualifierExpression)) { - if (nullSafe && !isAnnotatedNotNull(argument)) { + if (nullSafe && !ExpressionUtils.isAnnotatedNotNull(argument)) { return null; } return argument; @@ -215,34 +214,6 @@ public class SwitchUtils { return null; } - private static boolean isAnnotatedNotNull(PsiExpression expression) { - expression = ParenthesesUtils.stripParentheses(expression); - if (!(expression instanceof PsiReferenceExpression)) { - return false; - } - final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression; - final PsiElement target = referenceExpression.resolve(); - if (!(target instanceof PsiModifierListOwner)) { - return false; - } - final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner)target; - return NullableNotNullManager.isNotNull(modifierListOwner); - } - - private static boolean isAnnotatedNullable(PsiExpression expression) { - expression = ParenthesesUtils.stripParentheses(expression); - if (!(expression instanceof PsiReferenceExpression)) { - return false; - } - final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression; - final PsiElement target = referenceExpression.resolve(); - if (!(target instanceof PsiModifierListOwner)) { - return false; - } - final PsiModifierListOwner modifierListOwner = (PsiModifierListOwner)target; - return NullableNotNullManager.isNullable(modifierListOwner); - } - private static boolean canBeCaseLabel(PsiExpression expression, LanguageLevel languageLevel) { if (expression == null) { return false; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/unnecessary_boxing/UnnecessaryBoxing.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/unnecessary_boxing/UnnecessaryBoxing.java index 0668f164a2e9..2704415a0bf2 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/unnecessary_boxing/UnnecessaryBoxing.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/unnecessary_boxing/UnnecessaryBoxing.java @@ -79,4 +79,9 @@ class IntIntegerTest { new IntIntegerTest(new Integer(1)); // <-- incorrectly triggered f(new Integer(1)); // <-- not triggered } + + void m(@org.jetbrains.annotations.NotNull Boolean p) { + Boolean o = null; + boolean b = o != Boolean.valueOf(false) || p != Boolean.valueOf(false); + } } \ No newline at end of file