mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-141007 (Java | Java language level migration aids | Unnecessary boxing inspection suggests fix which lead to exception)
This commit is contained in:
+21
-17
@@ -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();
|
||||
|
||||
+25
@@ -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);
|
||||
}
|
||||
}
|
||||
+3
-32
@@ -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;
|
||||
|
||||
+5
@@ -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 != <warning descr="Unnecessary boxing 'Boolean.valueOf(false)'">Boolean.valueOf(false)</warning>;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user