From 4f773ca89bc5d775712f207a164eeabec7cf13b2 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 7 Jul 2008 14:53:13 +0400 Subject: [PATCH] IDEADEV-27885 --- .../ManualArrayToCollectionCopyInspection.java | 38 ++++++++++++++-------- .../performance/ManualArrayToCollectionCopy.java | 22 +++++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ManualArrayToCollectionCopyInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ManualArrayToCollectionCopyInspection.java index 66d992f4c4..6b3fe6bd81 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ManualArrayToCollectionCopyInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/ManualArrayToCollectionCopyInspection.java @@ -36,26 +36,24 @@ import org.jetbrains.annotations.Nullable; public class ManualArrayToCollectionCopyInspection extends BaseInspection { - @NotNull + @Override @NotNull public String getDisplayName() { return InspectionGadgetsBundle.message( "manual.array.to.collection.copy.display.name"); } + @Override public boolean isEnabledByDefault() { return true; } - @NotNull + @Override @NotNull protected String buildErrorString(Object... infos) { return InspectionGadgetsBundle.message( "manual.array.to.collection.copy.problem.descriptor"); } - public BaseInspectionVisitor buildVisitor() { - return new ManualArrayToCollectionCopyVisitor(); - } - + @Override public InspectionGadgetsFix buildFix(Object... infos) { return new ManualArrayToCollectionCopyFix(); } @@ -69,6 +67,7 @@ public class ManualArrayToCollectionCopyInspection "manual.array.to.collection.copy.replace.quickfix"); } + @Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement forElement = descriptor.getPsiElement(); @@ -292,6 +291,11 @@ public class ManualArrayToCollectionCopyInspection } } + @Override + public BaseInspectionVisitor buildVisitor() { + return new ManualArrayToCollectionCopyVisitor(); + } + private static class ManualArrayToCollectionCopyVisitor extends BaseInspectionVisitor { @@ -332,7 +336,8 @@ public class ManualArrayToCollectionCopyInspection registerStatementError(statement); } - @Override public void visitForeachStatement(PsiForeachStatement statement) { + @Override public void visitForeachStatement( + PsiForeachStatement statement) { super.visitForeachStatement(statement); final PsiExpression iteratedValue = statement.getIteratedValue(); if (iteratedValue == null) { @@ -380,27 +385,32 @@ public class ManualArrayToCollectionCopyInspection private static boolean expressionIsArrayToCollectionCopy( PsiExpression expression, PsiVariable variable, boolean shouldBeOffsetArrayAccess) { - final PsiExpression strippedExpression = + expression = PsiUtil.deparenthesizeExpression(expression); - if (strippedExpression == null) { + if (expression == null) { return false; } - if (!(strippedExpression instanceof PsiMethodCallExpression)) { + if (!(expression instanceof PsiMethodCallExpression)) { return false; } final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)strippedExpression; + (PsiMethodCallExpression)expression; final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return false; } - final PsiExpression argument = arguments[0]; - final PsiType argumentType = argument.getType(); - if (argumentType instanceof PsiPrimitiveType) { + final PsiReferenceExpression methodExpression = + methodCallExpression.getMethodExpression(); + final PsiExpression qualifier = + methodExpression.getQualifierExpression(); + if (!(qualifier instanceof PsiReferenceExpression) && + !(qualifier instanceof PsiThisExpression) && + !(qualifier instanceof PsiSuperExpression)) { return false; } + final PsiExpression argument = arguments[0]; if (SideEffectChecker.mayHaveSideEffects(argument)) { return false; } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/ManualArrayToCollectionCopy.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/ManualArrayToCollectionCopy.java index 747d515ad6..0b57e332e8 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/ManualArrayToCollectionCopy.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/ManualArrayToCollectionCopy.java @@ -3,6 +3,7 @@ package com.siyeh.igtest.performance; import java.util.List; import java.util.ArrayList; import java.util.Arrays; +import java.util.Random; public class ManualArrayToCollectionCopy { @@ -49,4 +50,25 @@ public class ManualArrayToCollectionCopy { parameters.add(args[i]); } } + + private static Random random = new Random(); + private static final int N = 10; + public static int nextInt() { + return random.nextInt(N); + } + + public static void main(String[] args) { + List[] li = new List[N]; + for (int i = 0; i < N; ++i) { + li[i] = new ArrayList(); + } + Integer[] values = new Integer[N]; + for (int i = 0; i < N; ++i) { + values[i] = i; + } + //This loop is highlighted. + for (int i = 1; i < N; ++i) { + li[nextInt()].add(values[i]); + } + } } -- 2.11.4.GIT