@Override
 public void visitBinaryExpression(PsiBinaryExpression expression) {
   final IElementType tokenType = expression.getOperationTokenType();
   if (JavaTokenType.OROR != tokenType) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   final PsiExpression rhs = expression.getROperand();
   final Pair<PsiReferenceExpression, PsiExpression> pair1 = getReferenceExpressionPair(lhs);
   final Pair<PsiReferenceExpression, PsiExpression> pair2 = getReferenceExpressionPair(rhs);
   if (pair1 == null || pair2 == null) {
     return;
   }
   final PsiExpression expression1 = pair1.getSecond();
   final PsiExpression expression2 = pair2.getSecond();
   if (expression1 == null || expression2 == null) {
     return;
   }
   final Project project = expression1.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiConstantEvaluationHelper constantEvaluationHelper =
       psiFacade.getConstantEvaluationHelper();
   final Object constant1 = constantEvaluationHelper.computeConstantExpression(expression1);
   final Object constant2 = constantEvaluationHelper.computeConstantExpression(expression2);
   if (constant1 == null || constant2 == null || constant1 == constant2) {
     return;
   }
   final PsiReferenceExpression referenceExpression1 = pair1.getFirst();
   final PsiReferenceExpression referenceExpression2 = pair2.getFirst();
   if (referenceExpression1.resolve() == referenceExpression2.resolve()) {
     registerError(expression);
   }
 }
  public static int findFieldAssignmentAnchor(
      final PsiStatement[] statements,
      final @Nullable Ref<Pair<PsiField, Boolean>> anchorRef,
      final PsiClass targetClass,
      final PsiParameter myParameter) {
    int i = 0;
    for (; i < statements.length; i++) {
      PsiStatement psiStatement = statements[i];

      if (psiStatement instanceof PsiExpressionStatement) {
        PsiExpressionStatement expressionStatement = (PsiExpressionStatement) psiStatement;
        PsiExpression expression = expressionStatement.getExpression();

        if (expression instanceof PsiMethodCallExpression) {
          PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression;
          String text = methodCallExpression.getMethodExpression().getText();
          if (text.equals("super") || text.equals("this")) {
            continue;
          }
        } else if (expression instanceof PsiAssignmentExpression) {
          PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) expression;
          PsiExpression lExpression = assignmentExpression.getLExpression();
          PsiExpression rExpression = assignmentExpression.getRExpression();

          if (!(lExpression instanceof PsiReferenceExpression)) break;
          if (!(rExpression instanceof PsiReferenceExpression)) break;

          PsiReferenceExpression lReference = (PsiReferenceExpression) lExpression;
          PsiReferenceExpression rReference = (PsiReferenceExpression) rExpression;

          PsiElement lElement = lReference.resolve();
          PsiElement rElement = rReference.resolve();

          if (!(lElement instanceof PsiField)
              || ((PsiField) lElement).getContainingClass() != targetClass) break;
          if (!(rElement instanceof PsiParameter)) break;

          if (myParameter.getTextRange().getStartOffset()
              < rElement.getTextRange().getStartOffset()) {
            if (anchorRef != null) {
              anchorRef.set(Pair.create((PsiField) lElement, Boolean.TRUE));
            }
            break;
          }

          if (anchorRef != null) {
            anchorRef.set(Pair.create((PsiField) lElement, Boolean.FALSE));
          }
          continue;
        }
      }

      break;
    }
    return i;
  }
 private static boolean referencesEqual(
     PsiReferenceExpression reference1, PsiReferenceExpression reference2) {
   if (reference1 == null || reference2 == null) {
     return false;
   }
   final PsiElement target1 = reference1.resolve();
   if (target1 == null) {
     return false;
   }
   final PsiElement target2 = reference2.resolve();
   return target1.equals(target2);
 }
 private static boolean isFixApplicable(PsiExpression location) {
   // conservative check to see if the result value of the postfix
   // expression is used later in the same expression statement.
   // Applying the quick fix in such a case would break the code
   // because the explicit unboxing code would split the expression in
   // multiple statements.
   final PsiElement parent = location.getParent();
   if (!(parent instanceof PsiPostfixExpression)) {
     return true;
   }
   final PsiReferenceExpression reference;
   if (location instanceof PsiReferenceExpression) {
     reference = (PsiReferenceExpression) location;
   } else if (location instanceof PsiArrayAccessExpression) {
     final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression) location;
     final PsiExpression expression = arrayAccessExpression.getArrayExpression();
     if (!(expression instanceof PsiReferenceExpression)) {
       return true;
     }
     reference = (PsiReferenceExpression) expression;
   } else {
     return true;
   }
   final PsiElement element = reference.resolve();
   if (element == null) {
     return true;
   }
   final PsiStatement statement = PsiTreeUtil.getParentOfType(parent, PsiStatement.class);
   final LocalSearchScope scope = new LocalSearchScope(statement);
   final Query<PsiReference> query = ReferencesSearch.search(element, scope);
   final Collection<PsiReference> references = query.findAll();
   return references.size() <= 1;
 }
 @NonNls
 private static String computeConstantBooleanText(PsiExpression expression) {
   if (!(expression instanceof PsiReferenceExpression)) {
     return null;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression;
   final PsiElement target = referenceExpression.resolve();
   if (!(target instanceof PsiField)) {
     return null;
   }
   final PsiField field = (PsiField) target;
   final PsiClass containingClass = field.getContainingClass();
   if (containingClass == null) {
     return null;
   }
   final String qualifiedName = containingClass.getQualifiedName();
   if (!CommonClassNames.JAVA_LANG_BOOLEAN.equals(qualifiedName)) {
     return null;
   }
   @NonNls final String name = field.getName();
   if ("TRUE".equals(name)) {
     return "true";
   } else if ("FALSE".equals(name)) {
     return "false";
   } else {
     return null;
   }
 }
 private static void checkCodeBlock(
     final PsiCodeBlock body, final Set<PsiField> candidates, Set<PsiField> usedFields) {
   try {
     final ControlFlow controlFlow =
         ControlFlowFactory.getInstance(body.getProject())
             .getControlFlow(body, AllVariablesControlFlowPolicy.getInstance());
     final List<PsiVariable> usedVars =
         ControlFlowUtil.getUsedVariables(controlFlow, 0, controlFlow.getSize());
     for (PsiVariable usedVariable : usedVars) {
       if (usedVariable instanceof PsiField) {
         final PsiField usedField = (PsiField) usedVariable;
         if (!usedFields.add(usedField)) {
           candidates.remove(usedField); // used in more than one code block
         }
       }
     }
     final List<PsiReferenceExpression> readBeforeWrites =
         ControlFlowUtil.getReadBeforeWrite(controlFlow);
     for (final PsiReferenceExpression readBeforeWrite : readBeforeWrites) {
       final PsiElement resolved = readBeforeWrite.resolve();
       if (resolved instanceof PsiField) {
         final PsiField field = (PsiField) resolved;
         PsiElement parent = body.getParent();
         if (!(parent instanceof PsiMethod)
             || !((PsiMethod) parent).isConstructor()
             || field.getInitializer() == null
             || field.hasModifierProperty(PsiModifier.STATIC)) {
           candidates.remove(field);
         }
       }
     }
   } catch (AnalysisCanceledException e) {
     candidates.clear();
   }
 }
Exemple #7
0
  private static boolean isParameterUsedRecursively(
      @NotNull PsiElement element, @NotNull List<PsiReference> array) {
    if (!(element instanceof PsiParameter)) return false;
    PsiParameter parameter = (PsiParameter) element;
    PsiElement scope = parameter.getDeclarationScope();
    if (!(scope instanceof PsiMethod)) return false;
    PsiMethod method = (PsiMethod) scope;
    int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter);

    for (PsiReference reference : array) {
      if (!(reference instanceof PsiElement)) return false;
      PsiElement argument = (PsiElement) reference;

      PsiMethodCallExpression methodCallExpression =
          (PsiMethodCallExpression)
              new PsiMatcherImpl(argument)
                  .dot(PsiMatchers.hasClass(PsiReferenceExpression.class))
                  .parent(PsiMatchers.hasClass(PsiExpressionList.class))
                  .parent(PsiMatchers.hasClass(PsiMethodCallExpression.class))
                  .getElement();
      if (methodCallExpression == null) return false;
      PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
      if (method != methodExpression.resolve()) return false;
      PsiExpressionList argumentList = methodCallExpression.getArgumentList();
      PsiExpression[] arguments = argumentList.getExpressions();
      int argumentIndex = ArrayUtilRt.find(arguments, argument);
      if (paramIndex != argumentIndex) return false;
    }

    return true;
  }
 private boolean isArrayLookup(PsiElement element, String indexName, PsiVariable arrayVariable) {
   if (element == null) {
     return false;
   }
   if (!(element instanceof PsiArrayAccessExpression)) {
     return false;
   }
   final PsiArrayAccessExpression arrayAccess = (PsiArrayAccessExpression) element;
   final PsiExpression indexExpression = arrayAccess.getIndexExpression();
   if (indexExpression == null) {
     return false;
   }
   if (!indexName.equals(indexExpression.getText())) {
     return false;
   }
   final PsiExpression arrayExpression = arrayAccess.getArrayExpression();
   if (!(arrayExpression instanceof PsiReferenceExpression)) {
     return false;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) arrayExpression;
   final PsiExpression qualifier = referenceExpression.getQualifierExpression();
   if (qualifier != null
       && !(qualifier instanceof PsiThisExpression)
       && !(qualifier instanceof PsiSuperExpression)) {
     return false;
   }
   final PsiElement target = referenceExpression.resolve();
   return arrayVariable.equals(target);
 }
  private static void makeVariableFinalIfNeeded(
      InsertionContext context, @Nullable PsiReferenceExpression ref) {
    if (!Registry.is("java.completion.make.outer.variables.final")
        || ref == null
        || PsiUtil.isLanguageLevel8OrHigher(ref)
        || JspPsiUtil.isInJspFile(ref)) {
      return;
    }

    PsiElement target = ref.resolve();
    if (target instanceof PsiLocalVariable || target instanceof PsiParameter) {
      PsiClass placeClass =
          PsiTreeUtil.findElementOfClassAtOffset(
              context.getFile(), context.getTailOffset() - 1, PsiClass.class, false);
      if (placeClass != null
          && !PsiTreeUtil.isAncestor(placeClass, target, true)
          && !HighlightControlFlowUtil.isReassigned(
              (PsiVariable) target,
              new HashMap<PsiElement, Collection<ControlFlowUtil.VariableInfo>>())) {
        PsiModifierList modifierList = ((PsiVariable) target).getModifierList();
        if (modifierList != null) {
          modifierList.setModifierProperty(PsiModifier.FINAL, true);
        }
      }
    }
  }
 private boolean checkConstantValueVariableUse(
     @Nullable PsiExpression expression,
     @NotNull PsiExpression constantExpression,
     @NotNull PsiElement body) {
   final PsiType constantType = constantExpression.getType();
   if (PsiType.DOUBLE.equals(constantType)) {
     final Object result = ExpressionUtils.computeConstantExpression(constantExpression, false);
     if (Double.valueOf(0.0).equals(result) || Double.valueOf(-0.0).equals(result)) {
       return false;
     }
   }
   if (!(expression instanceof PsiReferenceExpression)) {
     return false;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression;
   final PsiElement target = referenceExpression.resolve();
   if (!(target instanceof PsiVariable)) {
     return false;
   }
   if (target instanceof PsiField) {
     return false;
   }
   final PsiVariable variable = (PsiVariable) target;
   final VariableReadVisitor visitor = new VariableReadVisitor(variable);
   body.accept(visitor);
   if (!visitor.isRead()) {
     return false;
   }
   registerError(visitor.getReference(), constantExpression);
   return true;
 }
 public static PsiExpression findDefinition(
     @NotNull PsiReferenceExpression referenceExpression, @Nullable PsiVariable variable) {
   if (variable == null) {
     final PsiElement target = referenceExpression.resolve();
     if (!(target instanceof PsiVariable)) {
       return null;
     }
     variable = (PsiVariable) target;
   }
   final PsiCodeBlock block = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class);
   if (block == null) {
     return null;
   }
   final PsiElement[] defs = DefUseUtil.getDefs(block, variable, referenceExpression);
   if (defs.length != 1) {
     return null;
   }
   final PsiElement def = defs[0];
   if (def instanceof PsiVariable) {
     final PsiVariable target = (PsiVariable) def;
     final PsiExpression initializer = target.getInitializer();
     return ParenthesesUtils.stripParentheses(initializer);
   } else if (def instanceof PsiReferenceExpression) {
     final PsiElement parent = def.getParent();
     if (!(parent instanceof PsiAssignmentExpression)) {
       return null;
     }
     final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
     if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) {
       return null;
     }
     return ParenthesesUtils.stripParentheses(assignmentExpression.getRExpression());
   }
   return null;
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
   if (FileTypeUtils.isInServerPageFile(expression)) {
     // disable for jsp files IDEADEV-12957
     return;
   }
   super.visitReferenceExpression(expression);
   if (expression.getQualifierExpression() == null) {
     return;
   }
   final PsiElement referenceNameElement = expression.getReferenceNameElement();
   if (referenceNameElement == null) {
     return;
   }
   final PsiElement containingClass = getContainingContextClass(expression);
   if (containingClass == null) {
     return;
   }
   final PsiElement element = expression.resolve();
   if (!(element instanceof PsiMethod || element instanceof PsiField)) {
     return;
   }
   final PsiMember member = (PsiMember) element;
   if (!member.hasModifierProperty(PsiModifier.PRIVATE)) {
     return;
   }
   final PsiClass memberClass = ClassUtils.getContainingClass(member);
   if (memberClass == null) {
     return;
   }
   if (memberClass.equals(containingClass)) {
     return;
   }
   registerError(referenceNameElement, memberClass, member);
 }
 private static void checkSillyAssignment(
     PsiAssignmentExpression assignment, ProblemsHolder holder) {
   if (assignment.getOperationTokenType() != JavaTokenType.EQ) return;
   PsiExpression lExpression = assignment.getLExpression();
   PsiExpression rExpression = assignment.getRExpression();
   if (rExpression == null) return;
   lExpression = PsiUtil.deparenthesizeExpression(lExpression);
   rExpression = PsiUtil.deparenthesizeExpression(rExpression);
   if (!(lExpression instanceof PsiReferenceExpression)) return;
   PsiReferenceExpression rRef;
   if (!(rExpression instanceof PsiReferenceExpression)) {
     if (!(rExpression instanceof PsiAssignmentExpression)) return;
     final PsiAssignmentExpression rAssignmentExpression = (PsiAssignmentExpression) rExpression;
     final PsiExpression assignee =
         PsiUtil.deparenthesizeExpression(rAssignmentExpression.getLExpression());
     if (!(assignee instanceof PsiReferenceExpression)) return;
     rRef = (PsiReferenceExpression) assignee;
   } else {
     rRef = (PsiReferenceExpression) rExpression;
   }
   PsiReferenceExpression lRef = (PsiReferenceExpression) lExpression;
   PsiManager manager = assignment.getManager();
   if (!sameInstanceReferences(lRef, rRef, manager)) return;
   final PsiVariable variable = (PsiVariable) lRef.resolve();
   if (variable == null) return;
   holder.registerProblem(
       assignment,
       InspectionsBundle.message("assignment.to.itself.problem.descriptor", variable.getName()),
       ProblemHighlightType.LIKE_UNUSED_SYMBOL);
 }
  private static boolean isCollectCall(PsiStatement body, final PsiParameter parameter) {
    PsiIfStatement ifStatement = extractIfStatement(body);
    final PsiMethodCallExpression methodCallExpression = extractAddCall(body, ifStatement);
    if (methodCallExpression != null) {
      final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
      final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
      PsiClass qualifierClass = null;
      if (qualifierExpression instanceof PsiReferenceExpression) {
        if (ReferencesSearch.search(parameter, new LocalSearchScope(qualifierExpression))
                .findFirst()
            != null) {
          return false;
        }
        final PsiElement resolve = ((PsiReferenceExpression) qualifierExpression).resolve();
        if (resolve instanceof PsiVariable) {
          if (ReferencesSearch.search(
                      resolve, new LocalSearchScope(methodCallExpression.getArgumentList()))
                  .findFirst()
              != null) {
            return false;
          }
        }
        qualifierClass = PsiUtil.resolveClassInType(qualifierExpression.getType());
      } else if (qualifierExpression == null) {
        final PsiClass enclosingClass = PsiTreeUtil.getParentOfType(body, PsiClass.class);
        if (PsiUtil.getEnclosingStaticElement(body, enclosingClass) == null) {
          qualifierClass = enclosingClass;
        }
      }

      if (qualifierClass != null
          && InheritanceUtil.isInheritor(
              qualifierClass, false, CommonClassNames.JAVA_UTIL_COLLECTION)) {

        while (ifStatement != null && PsiTreeUtil.isAncestor(body, ifStatement, false)) {
          final PsiExpression condition = ifStatement.getCondition();
          if (condition != null
              && isConditionDependsOnUpdatedCollections(condition, qualifierExpression))
            return false;
          ifStatement = PsiTreeUtil.getParentOfType(ifStatement, PsiIfStatement.class);
        }

        final PsiElement resolve = methodExpression.resolve();
        if (resolve instanceof PsiMethod
            && "add".equals(((PsiMethod) resolve).getName())
            && ((PsiMethod) resolve).getParameterList().getParametersCount() == 1) {
          final PsiExpression[] args = methodCallExpression.getArgumentList().getExpressions();
          if (args.length == 1) {
            if (args[0] instanceof PsiCallExpression) {
              final PsiMethod method = ((PsiCallExpression) args[0]).resolveMethod();
              return method != null && !method.hasTypeParameters() && !isThrowsCompatible(method);
            }
            return true;
          }
        }
      }
    }
    return false;
  }
 private PsiField getReferencedField(PsiExpression expression) {
   if (expression instanceof PsiParenthesizedExpression) {
     final PsiExpression contents = ((PsiParenthesizedExpression) expression).getExpression();
     return getReferencedField(contents);
   }
   final PsiReferenceExpression reference = (PsiReferenceExpression) expression;
   return (PsiField) reference.resolve();
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   if (expression.getQualifierExpression() instanceof PsiSuperExpression) {
     PsiElement resolved = expression.resolve();
     if (resolved == null
         || resolved instanceof PsiMethod && shouldFixSuper((PsiMethod) resolved)) {
       expression.getQualifierExpression().delete();
     }
   }
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   final PsiElement psiElement = expression.resolve();
   if ((psiElement instanceof PsiLocalVariable || psiElement instanceof PsiParameter)
       && !PsiTreeUtil.isAncestor(myInitializer, psiElement, false)) {
     myElementReference = expression;
   } else {
     super.visitReferenceExpression(expression);
   }
 }
 static boolean isArrayLoopStatement(PsiForStatement forStatement) {
   final PsiStatement initialization = forStatement.getInitialization();
   if (!(initialization instanceof PsiDeclarationStatement)) {
     return false;
   }
   final PsiDeclarationStatement declaration = (PsiDeclarationStatement)initialization;
   final PsiElement[] declaredElements = declaration.getDeclaredElements();
   final PsiElement secondDeclaredElement;
   if (declaredElements.length == 1) {
     secondDeclaredElement = null;
   }
   else if (declaredElements.length == 2) {
     secondDeclaredElement = declaredElements[1];
   }
   else {
     return false;
   }
   final PsiElement declaredElement = declaredElements[0];
   if (!(declaredElement instanceof PsiVariable)) {
     return false;
   }
   final PsiVariable indexVariable = (PsiVariable)declaredElement;
   final PsiExpression initialValue = indexVariable.getInitializer();
   if (initialValue == null) {
     return false;
   }
   final Object constant =
     ExpressionUtils.computeConstantExpression(initialValue);
   if (!(constant instanceof Integer)) {
     return false;
   }
   final Integer integer = (Integer)constant;
   if (integer.intValue() != 0) {
     return false;
   }
   final PsiStatement update = forStatement.getUpdate();
   if (!VariableAccessUtils.variableIsIncremented(indexVariable, update)) {
     return false;
   }
   final PsiExpression condition = forStatement.getCondition();
   final PsiReferenceExpression arrayReference = getVariableReferenceFromCondition(condition, indexVariable, secondDeclaredElement);
   if (arrayReference == null) {
     return false;
   }
   final PsiElement element = arrayReference.resolve();
   if (!(element instanceof PsiVariable)) {
     return false;
   }
   final PsiVariable arrayVariable = (PsiVariable)element;
   final PsiStatement body = forStatement.getBody();
   return body == null ||
          isIndexVariableOnlyUsedAsIndex(arrayVariable, indexVariable, body) &&
          !VariableAccessUtils.variableIsAssigned(arrayVariable, body) &&
          !VariableAccessUtils.arrayContentsAreAssigned(arrayVariable, body);
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression reference) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(reference);
   final PsiElement element = reference.resolve();
   if (parameter.equals(element)) {
     used = true;
   }
 }
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   super.visitReferenceExpression(expression);
   final PsiElement referent = expression.resolve();
   if (!(referent instanceof PsiParameter)) {
     return;
   }
   final PsiParameter parameter = (PsiParameter) referent;
   if (paramsToMerge.contains(parameter)) {
     parameterUsages.add(expression);
   }
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression ref) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(ref);
   final PsiElement resolvedElement = ref.resolve();
   if (variable.equals(resolvedElement)) {
     used = true;
   }
 }
 public void fix() throws IncorrectOperationException {
   if (myField == null) return;
   final PsiManager manager = myScope.getManager();
   for (PsiReferenceExpression referenceExpression : myReferenceExpressions) {
     if (!referenceExpression.isValid()) continue;
     final PsiElement newlyResolved = referenceExpression.resolve();
     if (!manager.areElementsEquivalent(newlyResolved, myField)) {
       qualifyReference(referenceExpression, myField, myQualifyingClass);
     }
   }
 }
 private static boolean referenceExpressionsAreEquivalent(
     PsiReferenceExpression referenceExpression1, PsiReferenceExpression referenceExpression2) {
   final PsiElement element1 = referenceExpression1.resolve();
   final PsiElement element2 = referenceExpression2.resolve();
   if (element1 != null) {
     if (!element1.equals(element2)) {
       return false;
     }
   } else {
     return element2 == null;
   }
   if (element1 instanceof PsiMember) {
     final PsiMember member1 = (PsiMember) element1;
     if (member1.hasModifierProperty(PsiModifier.STATIC)) {
       return true;
     } else if (member1 instanceof PsiClass) {
       return true;
     }
   } else {
     return true;
   }
   final PsiExpression qualifier1 = referenceExpression1.getQualifierExpression();
   final PsiExpression qualifier2 = referenceExpression2.getQualifierExpression();
   if (qualifier1 != null
       && !(qualifier1 instanceof PsiThisExpression || qualifier1 instanceof PsiSuperExpression)) {
     if (qualifier2 == null) {
       return false;
     } else if (!expressionsAreEquivalent(qualifier1, qualifier2)) {
       return false;
     }
   } else {
     if (qualifier2 != null
         && !(qualifier2 instanceof PsiThisExpression
             || qualifier2 instanceof PsiSuperExpression)) {
       return false;
     }
   }
   final String text1 = referenceExpression1.getText();
   final String text2 = referenceExpression2.getText();
   return text1.equals(text2);
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   if (argumentsContainCatchParameter || !visited.add(expression)) {
     return;
   }
   super.visitReferenceExpression(expression);
   final PsiElement target = expression.resolve();
   if (!parameter.equals(target)) {
     if (target instanceof PsiLocalVariable) {
       final PsiLocalVariable variable = (PsiLocalVariable) target;
       final Query<PsiReference> query =
           ReferencesSearch.search(variable, variable.getUseScope(), false);
       query.forEach(
           reference -> {
             final PsiElement element = reference.getElement();
             final PsiElement parent =
                 PsiTreeUtil.skipParentsOfType(element, PsiParenthesizedExpression.class);
             if (!(parent instanceof PsiReferenceExpression)) {
               return true;
             }
             final PsiElement grandParent = parent.getParent();
             if (!(grandParent instanceof PsiMethodCallExpression)) {
               return true;
             }
             final PsiMethodCallExpression methodCallExpression =
                 (PsiMethodCallExpression) grandParent;
             final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
             final PsiExpression[] arguments = argumentList.getExpressions();
             for (PsiExpression argument : arguments) {
               argument.accept(ReferenceFinder.this);
             }
             return true;
           });
       final PsiExpression initializer = variable.getInitializer();
       if (initializer != null) {
         initializer.accept(this);
       }
     }
     return;
   }
   if (ignoreGetMessage) {
     argumentsContainCatchParameter = true;
   } else {
     final PsiElement parent = expression.getParent();
     if (parent instanceof PsiReferenceExpression) {
       final PsiElement grandParent = parent.getParent();
       if (grandParent instanceof PsiMethodCallExpression) {
         return;
       }
     }
     argumentsContainCatchParameter = true;
   }
 }
 @Override
 public void visitReferenceExpression(final PsiReferenceExpression reference) {
   if (myLineRange.intersects(reference.getTextRange())) {
     final PsiElement psiElement = reference.resolve();
     if (psiElement instanceof PsiVariable) {
       final PsiVariable var = (PsiVariable) psiElement;
       if (var instanceof PsiField) {
         if (myCollectExpressions
             && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars(
                 reference, myVisibleLocals)) {
           /*
           if (var instanceof PsiEnumConstant && reference.getQualifier() == null) {
             final PsiClass enumClass = ((PsiEnumConstant)var).getContainingClass();
             if (enumClass != null) {
               final PsiExpression expression = JavaPsiFacade.getInstance(var.getProject()).getParserFacade().createExpressionFromText(enumClass.getName() + "." + var.getName(), var);
               final PsiReference ref = expression.getReference();
               if (ref != null) {
                 ref.bindToElement(var);
                 myExpressions.add(new TextWithImportsImpl(expression));
               }
             }
           }
           else {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
           */
           final PsiModifierList modifierList = var.getModifierList();
           boolean isConstant =
               (var instanceof PsiEnumConstant)
                   || (modifierList != null
                       && modifierList.hasModifierProperty(PsiModifier.STATIC)
                       && modifierList.hasModifierProperty(PsiModifier.FINAL));
           if (!isConstant) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       } else {
         if (myVisibleLocals.contains(var.getName())) {
           myVars.add(var.getName());
         } else {
           // fix for variables used in inner classes
           if (!Comparing.equal(
               PsiTreeUtil.getParentOfType(reference, PsiClass.class),
               PsiTreeUtil.getParentOfType(var, PsiClass.class))) {
             myExpressions.add(new TextWithImportsImpl(reference));
           }
         }
       }
     }
   }
   super.visitReferenceExpression(reference);
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   final PsiExpression qualifierExpression = expression.getQualifierExpression();
   if (qualifierExpression != null && !(qualifierExpression instanceof PsiThisExpression)) {
     return;
   }
   final PsiElement resolved = expression.resolve();
   if (resolved instanceof PsiParameter) {
     myUsedParameters.add((PsiParameter) resolved);
   } else if (myMovedFields.contains(resolved)) {
     myUsedFields.add((PsiField) resolved);
   }
 }
 private static void shortenWithStaticImports(
     PsiElement affectedElement, int startOffset, int endOffset) {
   final int elementOffset = affectedElement.getTextOffset();
   final int finalStartOffset = startOffset + elementOffset;
   final int finalEndOffset = endOffset + elementOffset;
   final List<PsiReferenceExpression> references = new ArrayList<PsiReferenceExpression>();
   final JavaRecursiveElementVisitor collector =
       new JavaRecursiveElementVisitor() {
         @Override
         public void visitReferenceExpression(PsiReferenceExpression expression) {
           final int offset = expression.getTextOffset();
           if (offset > finalEndOffset) {
             return;
           }
           super.visitReferenceExpression(expression);
           if (offset + expression.getTextLength() < finalStartOffset)
             if (expression.getQualifierExpression() == null) {
               return;
             }
           references.add(expression);
         }
       };
   affectedElement.accept(collector);
   for (PsiReferenceExpression expression : references) {
     final PsiElement target = expression.resolve();
     if (!(target instanceof PsiMember)) {
       continue;
     }
     final PsiMember member = (PsiMember) target;
     final PsiClass containingClass = member.getContainingClass();
     if (containingClass == null) {
       continue;
     }
     final String className = containingClass.getQualifiedName();
     if (className == null) {
       continue;
     }
     final String name = member.getName();
     if (name == null) {
       continue;
     }
     if (ImportUtils.addStaticImport(className, name, expression)) {
       final PsiExpression qualifierExpression = expression.getQualifierExpression();
       if (qualifierExpression != null) {
         qualifierExpression.delete();
       }
     }
   }
 }
    private boolean isListGetLookup(PsiElement element,
                                    String indexName,
                                    PsiVariable listVariable) {
      if (!(element instanceof PsiExpression)) {
        return false;
      }
      final PsiExpression expression = (PsiExpression)element;
      if (!expressionIsListGetLookup(expression)) {
        return false;
      }
      final PsiMethodCallExpression methodCallExpression =
        (PsiMethodCallExpression)
          ParenthesesUtils.stripParentheses(expression);
      if (methodCallExpression == null) {
        return false;
      }
      final PsiReferenceExpression methodExpression =
        methodCallExpression.getMethodExpression();
      final PsiExpression qualifierExpression =
        methodExpression.getQualifierExpression();

      final PsiExpressionList argumentList =
        methodCallExpression.getArgumentList();
      final PsiExpression[] expressions = argumentList.getExpressions();
      if (expressions.length != 1) {
        return false;
      }
      if (!indexName.equals(expressions[0].getText())) {
        return false;
      }
      if (qualifierExpression == null ||
          qualifierExpression instanceof PsiThisExpression ||
          qualifierExpression instanceof PsiSuperExpression) {
        return listVariable == null;
      }
      if (!(qualifierExpression instanceof PsiReferenceExpression)) {
        return false;
      }
      final PsiReferenceExpression referenceExpression =
        (PsiReferenceExpression)qualifierExpression;
      final PsiExpression qualifier =
        referenceExpression.getQualifierExpression();
      if (qualifier != null && !(qualifier instanceof PsiThisExpression) &&
          !(qualifier instanceof PsiSuperExpression)) {
        return false;
      }
      final PsiElement target = referenceExpression.resolve();
      return listVariable.equals(target);
    }
 private static void makeMemberPackageLocal(PsiElement element) {
   final PsiElement parent = element.getParent();
   final PsiReferenceExpression reference = (PsiReferenceExpression) parent;
   final PsiModifierListOwner member = (PsiModifierListOwner) reference.resolve();
   if (member == null) {
     return;
   }
   final PsiModifierList modifiers = member.getModifierList();
   if (modifiers == null) {
     return;
   }
   modifiers.setModifierProperty(PsiModifier.PUBLIC, false);
   modifiers.setModifierProperty(PsiModifier.PROTECTED, false);
   modifiers.setModifierProperty(PsiModifier.PRIVATE, false);
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(expression);
   final PsiElement referent = expression.resolve();
   if (referent == null) {
     return;
   }
   if (referent.equals(variable)) {
     reference = expression;
     used = true;
   }
 }