2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
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 com
.intellij
.psi
.tree
.IElementType
;
20 import org
.jetbrains
.annotations
.NotNull
;
22 public class VariableAssignedVisitor
extends JavaRecursiveElementVisitor
{
24 @NotNull private final PsiVariable variable
;
25 private final boolean recurseIntoClasses
;
26 private boolean assigned
= false;
28 public VariableAssignedVisitor(@NotNull PsiVariable variable
,
29 boolean recurseIntoClasses
){
31 this.variable
= variable
;
32 this.recurseIntoClasses
= recurseIntoClasses
;
35 @Override public void visitElement(@NotNull PsiElement element
){
37 super.visitElement(element
);
41 @Override public void visitAssignmentExpression(
42 @NotNull PsiAssignmentExpression assignment
){
46 super.visitAssignmentExpression(assignment
);
47 final PsiExpression arg
= assignment
.getLExpression();
48 if(VariableAccessUtils
.mayEvaluateToVariable(arg
, variable
)){
54 public void visitClass(PsiClass aClass
) {
55 if(!recurseIntoClasses
){
61 super.visitClass(aClass
);
64 @Override public void visitPrefixExpression(
65 @NotNull PsiPrefixExpression prefixExpression
){
69 super.visitPrefixExpression(prefixExpression
);
70 final PsiJavaToken operationSign
= prefixExpression
.getOperationSign();
71 final IElementType tokenType
= operationSign
.getTokenType();
72 if(!tokenType
.equals(JavaTokenType
.PLUSPLUS
) &&
73 !tokenType
.equals(JavaTokenType
.MINUSMINUS
)){
76 final PsiExpression operand
= prefixExpression
.getOperand();
77 if(VariableAccessUtils
.mayEvaluateToVariable(operand
, variable
)){
82 @Override public void visitPostfixExpression(
83 @NotNull PsiPostfixExpression postfixExpression
){
87 super.visitPostfixExpression(postfixExpression
);
88 final PsiJavaToken operationSign
= postfixExpression
.getOperationSign();
89 final IElementType tokenType
= operationSign
.getTokenType();
90 if(!tokenType
.equals(JavaTokenType
.PLUSPLUS
) &&
91 !tokenType
.equals(JavaTokenType
.MINUSMINUS
)){
94 final PsiExpression operand
= postfixExpression
.getOperand();
95 if(VariableAccessUtils
.mayEvaluateToVariable(operand
, variable
)){
100 public boolean isAssigned(){