private static boolean expressionIsArrayToCollectionCopy( PsiExpression expression, PsiVariable variable, boolean shouldBeOffsetArrayAccess) { expression = ParenthesesUtils.stripParentheses(expression); if (expression == null) { return false; } if (!(expression instanceof PsiMethodCallExpression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression; final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length != 1) { return false; } 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]; final PsiType argumentType = argument.getType(); if (argumentType instanceof PsiPrimitiveType) { return false; } if (SideEffectChecker.mayHaveSideEffects(argument)) { return false; } if (shouldBeOffsetArrayAccess) { if (!ExpressionUtils.isOffsetArrayAccess(argument, variable)) { return false; } } else if (!VariableAccessUtils.evaluatesToVariable(argument, variable)) { return false; } final PsiMethod method = methodCallExpression.resolveMethod(); if (method == null) { return false; } @NonNls final String name = method.getName(); if (!name.equals("add")) { return false; } final PsiClass containingClass = method.getContainingClass(); return InheritanceUtil.isInheritor(containingClass, CommonClassNames.JAVA_UTIL_COLLECTION); }
public static boolean mayHaveSideEffects(@Nullable final PsiElement element) { return element instanceof PsiExpression && SideEffectChecker.mayHaveSideEffects((PsiExpression) element); }