IG: use utility method to reduce code

This commit is contained in:
Bas Leijdekkers
2017-01-20 11:45:49 +01:00
parent 9ba1b0ad17
commit ef5e6d4d3d
4 changed files with 37 additions and 111 deletions
@@ -187,7 +187,7 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
}
final PsiExpression boxedExpression = arguments[0];
final PsiType argumentType = boxedExpression.getType();
if (!(argumentType instanceof PsiPrimitiveType) || !canRemainUnboxed(expression, boxedExpression)) {
if (!(argumentType instanceof PsiPrimitiveType) || isBoxingNecessary(expression, boxedExpression)) {
return;
}
if (onlyReportSuperfluouslyBoxed) {
@@ -223,7 +223,7 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
}
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression;
final String canonicalText = referenceExpression.getCanonicalText();
if (PsiTypesUtil.unboxIfPossible(canonicalText) == canonicalText || !canRemainUnboxed(expression, boxedExpression)) {
if (PsiTypesUtil.unboxIfPossible(canonicalText) == canonicalText || isBoxingNecessary(expression, boxedExpression)) {
return;
}
if (onlyReportSuperfluouslyBoxed) {
@@ -235,48 +235,43 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
registerError(expression);
}
private boolean canRemainUnboxed(PsiExpression expression, PsiExpression boxedExpression) {
PsiElement parent = expression.getParent();
private boolean isBoxingNecessary(PsiExpression boxingExpression, PsiExpression boxedExpression) {
PsiElement parent = boxingExpression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
expression = (PsiExpression)parent;
boxingExpression = (PsiExpression)parent;
parent = parent.getParent();
}
if (parent instanceof PsiExpressionStatement || parent instanceof PsiReferenceExpression) {
return false;
return true;
}
else if (parent instanceof PsiTypeCastExpression) {
final PsiTypeCastExpression castExpression = (PsiTypeCastExpression)parent;
return !TypeUtils.isTypeParameter(castExpression.getType());
return TypeUtils.isTypeParameter(castExpression.getType());
}
else if (parent instanceof PsiConditionalExpression) {
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)parent;
final PsiExpression thenExpression = conditionalExpression.getThenExpression();
final PsiExpression elseExpression = conditionalExpression.getElseExpression();
if (elseExpression == null || thenExpression == null) {
return false;
return true;
}
if (PsiTreeUtil.isAncestor(thenExpression, expression, false)) {
if (PsiTreeUtil.isAncestor(thenExpression, boxingExpression, false)) {
final PsiType type = elseExpression.getType();
return type instanceof PsiPrimitiveType;
return !(type instanceof PsiPrimitiveType);
}
else if (PsiTreeUtil.isAncestor(elseExpression, expression, false)) {
else if (PsiTreeUtil.isAncestor(elseExpression, boxingExpression, false)) {
final PsiType type = thenExpression.getType();
return type instanceof PsiPrimitiveType;
return !(type instanceof PsiPrimitiveType);
}
else {
return true;
return false;
}
}
else if (parent instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
return !isPossibleObjectComparison(expression, polyadicExpression);
return isPossibleObjectComparison(boxingExpression, polyadicExpression);
}
final PsiElement grandParent = parent.getParent();
if (!(grandParent instanceof PsiCallExpression)) { // method call or new expression
return true;
}
final PsiCallExpression containingCallExpression = (PsiCallExpression)grandParent;
return isSameMethodCalledWithoutBoxing(containingCallExpression, expression, boxedExpression);
return MethodCallUtils.isNecessaryForSurroundingMethodCall(boxingExpression, boxedExpression);
}
private boolean isPossibleObjectComparison(PsiExpression expression, PsiPolyadicExpression polyadicExpression) {
@@ -296,33 +291,5 @@ public class UnnecessaryBoxingInspection extends BaseInspection {
}
return false;
}
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);
}
private boolean isSameMethodCalledWithoutBoxing(@NotNull PsiCallExpression methodCallExpression,
@NotNull PsiExpression boxingExpression,
@NotNull PsiExpression boxedExpression) {
final PsiMethod originalMethod = methodCallExpression.resolveMethod();
if (originalMethod == null) {
return false;
}
final PsiMethod otherMethod =
MethodCallUtils.findMethodWithReplacedArgument(methodCallExpression, boxingExpression, boxedExpression);
return originalMethod == otherMethod;
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2017 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.
@@ -43,8 +43,7 @@ public class UnnecessaryUnboxingInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean onlyReportSuperfluouslyUnboxed = false;
@NonNls static final Map<String, String> s_unboxingMethods =
new HashMap<>(8);
@NonNls static final Map<String, String> s_unboxingMethods = new HashMap<>(8);
static {
s_unboxingMethods.put(CommonClassNames.JAVA_LANG_INTEGER, "intValue");
@@ -154,13 +153,13 @@ public class UnnecessaryUnboxingInspection extends BaseInspection {
}
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (qualifier == null || !canRemainBoxed(expression, qualifier)) {
if (qualifier == null || isUnboxingNecessary(expression, qualifier)) {
return;
}
registerError(expression);
}
private boolean canRemainBoxed(@NotNull PsiExpression expression, @NotNull PsiExpression unboxedExpression) {
private boolean isUnboxingNecessary(@NotNull PsiExpression expression, @NotNull PsiExpression unboxedExpression) {
PsiElement parent = expression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
expression = (PsiExpression)parent;
@@ -169,61 +168,54 @@ public class UnnecessaryUnboxingInspection extends BaseInspection {
if (parent instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent;
if (isPossibleObjectComparison(expression, polyadicExpression)) {
return false;
return true;
}
}
if (parent instanceof PsiTypeCastExpression) {
final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)parent;
final PsiTypeElement typeElement = typeCastExpression.getCastType();
if (typeElement == null) {
return false;
return true;
}
final PsiType castType = typeElement.getType();
final PsiType expressionType = expression.getType();
if (expressionType == null || !castType.isAssignableFrom(expressionType)) {
return false;
return true;
}
}
else if (parent instanceof PsiConditionalExpression) {
final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)parent;
final PsiExpression thenExpression = conditionalExpression.getThenExpression();
if (thenExpression == null) {
return false;
return true;
}
final PsiExpression elseExpression = conditionalExpression.getElseExpression();
if (elseExpression == null) {
return false;
return true;
}
if (PsiTreeUtil.isAncestor(thenExpression, expression, false)) {
final PsiType type = elseExpression.getType();
if (!(type instanceof PsiPrimitiveType)) {
return false;
return true;
}
}
else if (PsiTreeUtil.isAncestor(elseExpression, expression, false)) {
final PsiType type = thenExpression.getType();
if (!(type instanceof PsiPrimitiveType)) {
return false;
return true;
}
}
}
else if (parent instanceof PsiExpressionList) {
final PsiElement grandParent = parent.getParent();
if (!(grandParent instanceof PsiCallExpression)) {
return true;
}
final PsiCallExpression methodCallExpression = (PsiCallExpression)grandParent;
if (!isSameMethodCalledWithoutUnboxing(methodCallExpression, expression, unboxedExpression)) {
return false;
}
else if (MethodCallUtils.isNecessaryForSurroundingMethodCall(expression, unboxedExpression)) {
return true;
}
if (onlyReportSuperfluouslyUnboxed) {
final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false, true);
if (!(expectedType instanceof PsiClassType)) {
return false;
return true;
}
}
return true;
return false;
}
private boolean isPossibleObjectComparison(PsiExpression expression, PsiPolyadicExpression polyadicExpression) {
@@ -263,16 +255,5 @@ public class UnnecessaryUnboxingInspection extends BaseInspection {
final String unboxingMethod = s_unboxingMethods.get(qualifierTypeName);
return unboxingMethod.equals(methodName);
}
private boolean isSameMethodCalledWithoutUnboxing(@NotNull PsiCallExpression callExpression,
@NotNull PsiExpression unboxingExpression,
@NotNull PsiExpression unboxedExpression) {
final PsiMethod originalMethod = callExpression.resolveMethod();
if (originalMethod == null) {
return false;
}
final PsiMethod method = MethodCallUtils.findMethodWithReplacedArgument(callExpression, unboxingExpression, unboxedExpression);
return originalMethod == method;
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2015 Dave Griffith, Bas Leijdekkers
* Copyright 2006-2017 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.
@@ -128,22 +128,9 @@ public class UnaryPlusInspection extends BaseInspection {
return;
}
}
else if (TypeUtils.unaryNumericPromotion(type) != type) {
PsiExpression expression = prefixExpression;
PsiElement parent = expression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
expression = (PsiExpression)parent;
parent = parent.getParent();
}
final PsiElement grandParent = parent.getParent();
if (parent instanceof PsiExpressionList && grandParent instanceof PsiCall) {
// unary plus might have been used as cast to int
final PsiCall call = (PsiCall)grandParent;
final PsiMethod method = call.resolveMethod();
if (MethodCallUtils.findMethodWithReplacedArgument(call, expression, operand) != method) {
return;
}
}
else if (TypeUtils.unaryNumericPromotion(type) != type && MethodCallUtils.isNecessaryForSurroundingMethodCall(prefixExpression, operand)) {
// unary plus might have been used as cast to int
return;
}
registerError(token, ProblemHighlightType.LIKE_UNUSED_SYMBOL);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 Bas Leijdekkers
* Copyright 2011-2017 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -192,17 +192,8 @@ public class UnnecessaryExplicitNumericCastInspection extends BaseInspection {
final PsiType lhsType = variable.getType();
return !castType.equals(lhsType) || !isLegalAssignmentConversion(operand, lhsType);
}
else if (parent instanceof PsiExpressionList) {
final PsiExpressionList expressionList = (PsiExpressionList)parent;
final PsiElement grandParent = expressionList.getParent();
if (!(grandParent instanceof PsiCallExpression)) {
return true;
}
final PsiCallExpression callExpression = (PsiCallExpression)grandParent;
final PsiMethod targetMethod = callExpression.resolveMethod();
if (targetMethod == null || targetMethod != MethodCallUtils.findMethodWithReplacedArgument(callExpression, expression, operand)) {
return true;
}
else if (MethodCallUtils.isNecessaryForSurroundingMethodCall(expression, operand)) {
return true;
}
final PsiType expectedType = ExpectedTypeUtils.findExpectedType(expression, false);
return !castType.equals(expectedType) || !isLegalWideningConversion(operand, castType);