IDEADEV-40452
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / psiutils / RecursionVisitor.java
blob63bf08f582c5289115270bee5319926e95ccf4f1
1 /*
2 * Copyright 2003-2006 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 org.jetbrains.annotations.NotNull;
21 public class RecursionVisitor extends JavaRecursiveElementVisitor{
23 private boolean recursive = false;
24 private final PsiMethod method;
25 private final String methodName;
27 public RecursionVisitor(@NotNull PsiMethod method){
28 super();
29 this.method = method;
30 methodName = method.getName();
33 @Override public void visitElement(@NotNull PsiElement element){
34 if(!recursive){
35 super.visitElement(element);
39 @Override public void visitMethodCallExpression(
40 @NotNull PsiMethodCallExpression call){
41 if(recursive){
42 return;
44 super.visitMethodCallExpression(call);
45 final PsiReferenceExpression methodExpression =
46 call.getMethodExpression();
47 final String calledMethodName = methodExpression.getReferenceName();
48 if(calledMethodName == null){
49 return;
51 if(!calledMethodName.equals(methodName)){
52 return;
54 final PsiMethod calledMethod = call.resolveMethod();
55 if(!method.equals(calledMethod)){
56 return;
58 if(method.hasModifierProperty(PsiModifier.STATIC) ||
59 method.hasModifierProperty(PsiModifier.PRIVATE)){
60 recursive = true;
61 return;
63 final PsiExpression qualifier = methodExpression.getQualifierExpression();
64 if(qualifier == null || qualifier instanceof PsiThisExpression){
65 recursive = true;
69 public boolean isRecursive(){
70 return recursive;