IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / psiutils / ArrayContentsAccessedVisitor.java
blob2d34dabec1d6c8dfc19a8383d3ec9b399af50002
1 /*
2 * Copyright 2003-2005 Dave Griffith
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.siyeh.ig.psiutils;
18 import com.intellij.psi.*;
19 import org.jetbrains.annotations.NotNull;
21 public class ArrayContentsAccessedVisitor extends JavaRecursiveElementVisitor{
22 private boolean accessed = false;
23 private final PsiVariable variable;
25 public ArrayContentsAccessedVisitor(@NotNull PsiVariable variable){
26 super();
27 this.variable = variable;
30 @Override public void visitForeachStatement(@NotNull PsiForeachStatement statement){
31 if(accessed){
32 return;
34 super.visitForeachStatement(statement);
35 final PsiExpression qualifier = statement.getIteratedValue();
36 if(!(qualifier instanceof PsiReferenceExpression)){
37 return;
39 final PsiElement referent = ((PsiReference) qualifier).resolve();
40 if(referent == null){
41 return;
43 if(!referent.equals(variable)){
44 return;
46 accessed = true;
49 @Override public void visitArrayAccessExpression(PsiArrayAccessExpression arg){
50 if(accessed){
51 return;
53 super.visitArrayAccessExpression(arg);
54 if(arg.getParent() instanceof PsiAssignmentExpression &&
55 ((PsiAssignmentExpression) arg.getParent()).getLExpression()
56 .equals(arg)){
57 return;
59 final PsiExpression arrayExpression = arg.getArrayExpression();
60 if(!(arrayExpression instanceof PsiReferenceExpression)){
61 return;
63 final PsiElement referent = ((PsiReference) arrayExpression).resolve();
64 if(referent == null){
65 return;
67 if(referent.equals(variable)){
68 accessed = true;
72 public boolean isAccessed(){
73 return accessed;