Пример #1
0
 public static String isValidMethodExpression(PsiElement psiElement) {
   if (psiElement instanceof PsiMethodCallExpression) {
     final PsiReferenceExpression methodExpression =
         ((PsiMethodCallExpression) psiElement).getMethodExpression();
     if (methodExpression != null
         && methodExpression.getQualifierExpression() != null
         && methodExpression.getQualifierExpression().getType() != null) {
       final PsiType type = methodExpression.getQualifierExpression().getType();
       if (type instanceof PsiClassReferenceType) {
         final PsiClassReferenceType classReferenceType = (PsiClassReferenceType) type;
         if (classReferenceType.getReference() != null) {
           final Project project = psiElement.getProject();
           if (AnnoRefConfigSettings.getInstance(project)
               .getAnnoRefState()
               .ANNOREF_UTIL_CLASS_FQN
               .equals(classReferenceType.getReference().getQualifiedName())) {
             if (((PsiCall) psiElement).getArgumentList().getExpressions().length == 1) {
               final PsiExpression psiExpression =
                   ((PsiCall) psiElement).getArgumentList().getExpressions()[0];
               return String.valueOf(((PsiLiteral) psiExpression).getValue());
             }
           }
         }
       }
     }
   }
   return null;
 }
Пример #2
0
 public static SQLRefReference getAnnoRefIdFromMethodCallExpressionElement(PsiElement psiElement) {
   if (psiElement instanceof PsiMethodCallExpression) {
     final PsiReferenceExpression methodExpression =
         ((PsiMethodCallExpression) psiElement).getMethodExpression();
     if (methodExpression != null
         && methodExpression.getQualifierExpression() != null
         && methodExpression.getQualifierExpression().getType() != null) {
       final PsiType type = methodExpression.getQualifierExpression().getType();
       final PsiClassReferenceType classReferenceType = (PsiClassReferenceType) type;
       if (type instanceof PsiClassReferenceType && classReferenceType.getReference() != null) {
         final Project project = psiElement.getProject();
         if (AnnoRefConfigSettings.getInstance(project)
             .getAnnoRefState()
             .ANNOREF_UTIL_CLASS_FQN
             .equals(classReferenceType.getReference().getQualifiedName())) {
           if (((PsiCall) psiElement).getArgumentList().getExpressions().length == 1) {
             final PsiExpression psiExpression =
                 ((PsiCall) psiElement).getArgumentList().getExpressions()[0];
             final String refId = String.valueOf(((PsiLiteral) psiExpression).getValue());
             final SQLRefReference refReference =
                 ServiceManager.getService(project, SQLRefRepository.class)
                     .getSQLRefReferenceForID(refId);
             return refReference;
           } else {
             throw new IllegalArgumentException(
                 String.format(
                     "Argument %s for parameter of %s.%s must not be null",
                     new Object[] {
                       "0",
                       classReferenceType.getReference().getQualifiedName(),
                       methodExpression.getQualifiedName()
                     }));
           }
         } else {
           log.warn(
               "getAnnoRefIdFromMethodCallExpressionElement(): PsiElement ClassReferenceType FQA="
                   + classReferenceType.getReference().getQualifiedName()
                   + " different"
                   + " from ANNOREF_UTIL_CLASS_FQN="
                   + AnnoRefConfigSettings.getInstance(project)
                       .getAnnoRefState()
                       .ANNOREF_UTIL_CLASS_FQN);
         }
       } else {
         log.warn(
             "getAnnoRefIdFromMethodCallExpressionElement(): PsiElement ReferenceType="
                 + classReferenceType);
       }
     } else {
       log.warn(
           "getAnnoRefIdFromMethodCallExpressionElement(): PsiElement is of type="
               + psiElement.getClass());
     }
   }
   return null;
 }
Пример #3
0
 @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();
     }
   }
 }
    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 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);
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   @NonNls final String methodName = methodExpression.getReferenceName();
   if (methodName == null) {
     return;
   }
   if (!methodName.startsWith("get") && !methodName.startsWith("update")) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length == 0) {
     return;
   }
   final PsiExpression argument = arguments[0];
   if (!TypeUtils.expressionHasType(argument, PsiKeyword.INT)) {
     return;
   }
   if (!PsiUtil.isConstantExpression(argument)) {
     return;
   }
   final Integer val = (Integer) ConstantExpressionUtil.computeCastTo(argument, PsiType.INT);
   if (val == null || val.intValue() != 0) {
     return;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (!TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.sql.ResultSet")) {
     return;
   }
   registerError(argument);
 }
 public FieldConflictsResolver(String name, PsiCodeBlock scope) {
   myScope = scope;
   if (myScope == null) {
     myField = null;
     myReferenceExpressions = null;
     return;
   }
   JavaPsiFacade facade = JavaPsiFacade.getInstance(myScope.getProject());
   final PsiVariable oldVariable =
       facade.getResolveHelper().resolveAccessibleReferencedVariable(name, myScope);
   myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null;
   if (!(oldVariable instanceof PsiField)) {
     myReferenceExpressions = null;
     return;
   }
   myReferenceExpressions = new ArrayList<PsiReferenceExpression>();
   for (PsiReference reference :
       ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) {
     final PsiElement element = reference.getElement();
     if (element instanceof PsiReferenceExpression) {
       final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) element;
       if (referenceExpression.getQualifierExpression() == null) {
         myReferenceExpressions.add(referenceExpression);
       }
     }
   }
   if (myField.hasModifierProperty(PsiModifier.STATIC)) {
     myQualifyingClass = myField.getContainingClass();
   }
 }
 @Override
 public void visitMethodCallExpression(PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   @NonNls final String referenceName = methodExpression.getReferenceName();
   if (!"toString".equals(referenceName)
       || ExpressionUtils.isConversionToStringNecessary(expression)) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 0) {
     return;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (qualifier != null) {
     if (qualifier.getType() instanceof PsiArrayType) {
       // do not warn on nonsensical code
       return;
     } else if (qualifier instanceof PsiSuperExpression) {
       return;
     }
   }
   registerMethodCallError(expression, calculateReplacementText(qualifier));
 }
  private void findUsagesForMethod(PsiMethod method, List<FixableUsageInfo> usages) {
    final PsiManager psiManager = method.getManager();
    final Project project = psiManager.getProject();
    final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
    final Iterable<PsiReference> calls = ReferencesSearch.search(method, scope);
    for (PsiReference reference : calls) {
      final PsiElement referenceElement = reference.getElement();
      final PsiElement parent = referenceElement.getParent();
      if (parent instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression call = (PsiMethodCallExpression) parent;
        if (isInMovedElement(call)) {
          continue;
        }
        final PsiReferenceExpression methodExpression = call.getMethodExpression();
        final PsiExpression qualifier = methodExpression.getQualifierExpression();
        if (qualifier == null || qualifier instanceof PsiThisExpression) {
          usages.add(new ReplaceThisCallWithDelegateCall(call, delegateFieldName));
        }
        delegationRequired = true;
      }
    }

    if (!delegationRequired && MethodInheritanceUtils.hasSiblingMethods(method)) {
      delegationRequired = true;
    }

    if (delegationRequired) {
      usages.add(new MakeMethodDelegate(method, delegateFieldName));
    } else {
      usages.add(new RemoveMethod(method));
    }
  }
Пример #10
0
  private void fixReferencesToStatic(PsiElement classMember) throws IncorrectOperationException {
    final StaticReferencesCollector collector = new StaticReferencesCollector();
    classMember.accept(collector);
    ArrayList<PsiJavaCodeReferenceElement> refs = collector.getReferences();
    ArrayList<PsiElement> members = collector.getReferees();
    ArrayList<PsiClass> classes = collector.getRefereeClasses();
    PsiElementFactory factory =
        JavaPsiFacade.getInstance(classMember.getProject()).getElementFactory();

    for (int i = 0; i < refs.size(); i++) {
      PsiJavaCodeReferenceElement ref = refs.get(i);
      PsiElement namedElement = members.get(i);
      PsiClass aClass = classes.get(i);

      if (namedElement instanceof PsiNamedElement) {
        PsiReferenceExpression newRef =
            (PsiReferenceExpression)
                factory.createExpressionFromText(
                    "a." + ((PsiNamedElement) namedElement).getName(), null);
        PsiExpression qualifierExpression = newRef.getQualifierExpression();
        assert qualifierExpression != null;
        qualifierExpression =
            (PsiExpression) qualifierExpression.replace(factory.createReferenceExpression(aClass));
        qualifierExpression.putCopyableUserData(PRESERVE_QUALIFIER, ref.isQualified());
        ref.replace(newRef);
      }
    }
  }
 private static boolean isIndexOfCall(@NotNull PsiMethodCallExpression expression) {
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   final String methodName = methodExpression.getReferenceName();
   if (!HardcodedMethodConstants.INDEX_OF.equals(methodName)) {
     return false;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 1) {
     return false;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (qualifier == null) {
     return false;
   }
   final PsiType qualifierType = qualifier.getType();
   if (qualifierType == null) {
     return false;
   }
   final Project project = expression.getProject();
   final GlobalSearchScope projectScope = GlobalSearchScope.allScope(project);
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiClass javaUtilListClass =
       psiFacade.findClass(CommonClassNames.JAVA_UTIL_LIST, projectScope);
   if (javaUtilListClass == null) {
     return false;
   }
   final PsiElementFactory factory = psiFacade.getElementFactory();
   final PsiClassType javaUtilListType = factory.createType(javaUtilListClass);
   return javaUtilListType.isAssignableFrom(qualifierType);
 }
 static String createContainsExpressionText(
     @NotNull PsiMethodCallExpression call, @NotNull PsiJavaToken sign, boolean flipped) {
   final IElementType tokenType = sign.getTokenType();
   final PsiReferenceExpression methodExpression = call.getMethodExpression();
   final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
   final String qualifierText;
   if (qualifierExpression == null) {
     qualifierText = "";
   } else {
     qualifierText = qualifierExpression.getText();
   }
   final PsiExpressionList argumentList = call.getArgumentList();
   final PsiExpression expression = argumentList.getExpressions()[0];
   @NonNls
   final String newExpressionText = qualifierText + ".contains(" + expression.getText() + ')';
   if (tokenType.equals(JavaTokenType.EQEQ)) {
     return '!' + newExpressionText;
   } else if (!flipped
       && (tokenType.equals(JavaTokenType.LT) || tokenType.equals(JavaTokenType.LE))) {
     return '!' + newExpressionText;
   } else if (flipped
       && (tokenType.equals(JavaTokenType.GT) || tokenType.equals(JavaTokenType.GE))) {
     return '!' + newExpressionText;
   }
   return newExpressionText;
 }
 @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 removeParensFromReferenceExpression(
     @NotNull PsiReferenceExpression referenceExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression qualifier = referenceExpression.getQualifierExpression();
   if (qualifier != null) {
     removeParentheses(qualifier, ignoreClarifyingParentheses);
   }
 }
 @Nullable
 private static PsiType guessElementTypeFromReference(
     MethodPatternMap methodPatternMap, PsiElement ref, TextRange rangeToIgnore) {
   PsiElement refParent = ref.getParent();
   if (refParent instanceof PsiReferenceExpression) {
     PsiReferenceExpression parentExpr = (PsiReferenceExpression) refParent;
     if (ref.equals(parentExpr.getQualifierExpression())
         && parentExpr.getParent() instanceof PsiMethodCallExpression) {
       String methodName = parentExpr.getReferenceName();
       PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parentExpr.getParent();
       PsiExpression[] args = methodCall.getArgumentList().getExpressions();
       MethodPattern pattern = methodPatternMap.findPattern(methodName, args.length);
       if (pattern != null) {
         if (pattern.parameterIndex < 0) { // return value
           if (methodCall.getParent() instanceof PsiTypeCastExpression
               && (rangeToIgnore == null || !rangeToIgnore.contains(methodCall.getTextRange()))) {
             return ((PsiTypeCastExpression) methodCall.getParent()).getType();
           }
         } else {
           return args[pattern.parameterIndex].getType();
         }
       }
     }
   }
   return null;
 }
  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;
  }
 @Override
 public void visitMethodCallExpression(PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   @NonNls final String name = methodExpression.getReferenceName();
   if (HardcodedMethodConstants.TO_STRING.equals(name)) {
     final PsiExpressionList argumentList = expression.getArgumentList();
     final PsiExpression[] arguments = argumentList.getExpressions();
     if (arguments.length != 0) {
       return;
     }
     final PsiExpression qualifier = methodExpression.getQualifierExpression();
     checkExpression(qualifier);
   } else if ("append".equals(name)) {
     final PsiExpression qualifier = methodExpression.getQualifierExpression();
     if (!TypeUtils.expressionHasTypeOrSubtype(
         qualifier, CommonClassNames.JAVA_LANG_ABSTRACT_STRING_BUILDER)) {
       return;
     }
     final PsiExpressionList argumentList = expression.getArgumentList();
     final PsiExpression[] arguments = argumentList.getExpressions();
     if (arguments.length != 1) {
       return;
     }
     final PsiExpression argument = arguments[0];
     checkExpression(argument);
   } else if ("valueOf".equals(name)) {
     final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
     if (!(qualifierExpression instanceof PsiReferenceExpression)) {
       return;
     }
     final PsiReferenceExpression referenceExpression =
         (PsiReferenceExpression) qualifierExpression;
     final String canonicalText = referenceExpression.getCanonicalText();
     if (!CommonClassNames.JAVA_LANG_STRING.equals(canonicalText)) {
       return;
     }
     final PsiExpressionList argumentList = expression.getArgumentList();
     final PsiExpression[] arguments = argumentList.getExpressions();
     if (arguments.length != 1) {
       return;
     }
     final PsiExpression argument = arguments[0];
     checkExpression(argument);
   }
 }
 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);
 }
 private static boolean isInvertedMethodCall(final PsiReferenceExpression methodExpression) {
   final PsiPrefixExpression prefixExpression =
       PsiTreeUtil.getParentOfType(methodExpression, PsiPrefixExpression.class);
   if (methodExpression.getQualifierExpression() instanceof PsiSuperExpression)
     return true; // don't flag super calls
   if (prefixExpression != null) {
     final IElementType tokenType = prefixExpression.getOperationTokenType();
     if (tokenType.equals(JavaTokenType.EXCL)) {
       return true;
     }
   }
   return false;
 }
Пример #20
0
 @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);
   }
 }
 @Override
 protected void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiMethodCallExpression methodCallExpression =
       (PsiMethodCallExpression) descriptor.getPsiElement().getParent().getParent();
   final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (qualifier == null) {
     PsiReplacementUtil.replaceExpression(methodCallExpression, "this");
   } else {
     methodCallExpression.replace(qualifier);
   }
 }
 private static PsiMethodCallExpression prepareMethodCall(
     PsiReferenceExpression expr, String text) {
   PsiExpression qualifier = expr.getQualifierExpression();
   if (qualifier != null) {
     final PsiElement referenceNameElement = expr.getReferenceNameElement();
     if (referenceNameElement != null) {
       text = expr.getText().substring(0, referenceNameElement.getStartOffsetInParent()) + text;
     }
   }
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(expr.getProject()).getElementFactory();
   return (PsiMethodCallExpression) factory.createExpressionFromText(text, expr);
 }
 @Override
 protected final void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement element = descriptor.getPsiElement();
   final PsiTypeElement castTypeElement;
   final PsiReferenceExpression reference;
   if (element instanceof PsiTypeCastExpression) {
     final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression) element;
     castTypeElement = typeCastExpression.getCastType();
     final PsiExpression operand = typeCastExpression.getOperand();
     if (!(operand instanceof PsiReferenceExpression)) {
       return;
     }
     reference = (PsiReferenceExpression) operand;
   } else if (element instanceof PsiMethodCallExpression) {
     final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) element;
     final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
     final PsiExpression qualifier = methodExpression.getQualifierExpression();
     if (!(qualifier instanceof PsiClassObjectAccessExpression)) {
       return;
     }
     final PsiClassObjectAccessExpression classObjectAccessExpression =
         (PsiClassObjectAccessExpression) qualifier;
     castTypeElement = classObjectAccessExpression.getOperand();
     final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
     final PsiExpression[] arguments = argumentList.getExpressions();
     if (arguments.length != 1) {
       return;
     }
     final PsiExpression argument = arguments[0];
     if (!(argument instanceof PsiReferenceExpression)) {
       return;
     }
     reference = (PsiReferenceExpression) argument;
   } else {
     return;
   }
   if (castTypeElement == null) {
     return;
   }
   final PsiInstanceOfExpression conflictingInstanceof =
       InstanceOfUtils.getConflictingInstanceof(castTypeElement.getType(), reference, element);
   final PsiTypeElement instanceofTypeElement = conflictingInstanceof.getCheckType();
   if (instanceofTypeElement == null) {
     return;
   }
   final PsiElement newElement = replace(castTypeElement, instanceofTypeElement);
   final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
   codeStyleManager.shortenClassReferences(newElement);
 }
 private static PsiReferenceExpression getReference(
     PsiMethodCallExpression newMethodCallExpression) {
   final PsiReferenceExpression methodExpression = newMethodCallExpression.getMethodExpression();
   final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
   if (qualifierExpression instanceof PsiMethodCallExpression) {
     final PsiMethodCallExpression methodCallExpression =
         (PsiMethodCallExpression) qualifierExpression;
     return getReference(methodCallExpression);
   }
   if (!(qualifierExpression instanceof PsiReferenceExpression)) {
     return null;
   }
   return (PsiReferenceExpression) qualifierExpression;
 }
  private static boolean isSameField(
      @NotNull PsiMember enclosingCtrOrInitializer,
      @NotNull PsiField field,
      @NotNull PsiReferenceExpression reference,
      @NotNull PsiFile containingFile) {

    if (!containingFile
        .getManager()
        .areElementsEquivalent(
            enclosingCtrOrInitializer.getContainingClass(), field.getContainingClass()))
      return false;
    PsiExpression qualifierExpression = reference.getQualifierExpression();
    return qualifierExpression == null || qualifierExpression instanceof PsiThisExpression;
  }
 @Override
 public void visitMethodCallExpression(PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   final String methodName = methodExpression.getReferenceName();
   if (!"cast".equals(methodName)) {
     return;
   }
   final PsiMethod method = expression.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   if (containingClass == null) {
     return;
   }
   final String qualifiedName = containingClass.getQualifiedName();
   if (!"java.lang.Class".equals(qualifiedName)) {
     return;
   }
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   if (!(qualifier instanceof PsiClassObjectAccessExpression)) {
     return;
   }
   final PsiClassObjectAccessExpression classObjectAccessExpression =
       (PsiClassObjectAccessExpression) qualifier;
   final PsiTypeElement operand = classObjectAccessExpression.getOperand();
   final PsiType castType = operand.getType();
   if (!(castType instanceof PsiClassType)) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   if (arguments.length != 1) {
     return;
   }
   final PsiExpression argument = arguments[0];
   if (!(argument instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) argument;
   final PsiInstanceOfExpression conflictingInstanceof =
       InstanceOfUtils.getConflictingInstanceof(castType, referenceExpression, expression);
   if (conflictingInstanceof == null) {
     return;
   }
   final PsiTypeElement instanceofTypeElement = conflictingInstanceof.getCheckType();
   registerError(expression, referenceExpression, operand, instanceofTypeElement);
 }
 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();
       }
     }
   }
 }
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiElement methodNameElement = descriptor.getPsiElement();
   final PsiReferenceExpression methodExpression =
       (PsiReferenceExpression) methodNameElement.getParent();
   assert methodExpression != null;
   final PsiExpression qualifier = methodExpression.getQualifierExpression();
   @NonNls final String signalAll = "signalAll";
   if (qualifier == null) {
     replaceExpression(methodExpression, signalAll);
   } else {
     final String qualifierText = qualifier.getText();
     replaceExpression(methodExpression, qualifierText + '.' + signalAll);
   }
 }
 public static PsiReferenceExpression qualifyReference(
     PsiReferenceExpression referenceExpression,
     final PsiMember member,
     @Nullable final PsiClass qualifyingClass)
     throws IncorrectOperationException {
   PsiManager manager = referenceExpression.getManager();
   PsiMethodCallExpression methodCallExpression =
       PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true);
   while ((methodCallExpression) != null) {
     if (HighlightUtil.isSuperOrThisMethodCall(methodCallExpression)) {
       return referenceExpression;
     }
     methodCallExpression =
         PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true);
   }
   PsiReferenceExpression expressionFromText;
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
   if (qualifyingClass == null) {
     PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
     final PsiClass containingClass = member.getContainingClass();
     if (parentClass != null
         && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
       while (parentClass != null
           && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
         parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
       }
       LOG.assertTrue(parentClass != null);
       expressionFromText =
           (PsiReferenceExpression)
               factory.createExpressionFromText("A.this." + member.getName(), null);
       ((PsiThisExpression) expressionFromText.getQualifierExpression())
           .getQualifier()
           .replace(factory.createClassReferenceElement(parentClass));
     } else {
       expressionFromText =
           (PsiReferenceExpression)
               factory.createExpressionFromText("this." + member.getName(), null);
     }
   } else {
     expressionFromText =
         (PsiReferenceExpression) factory.createExpressionFromText("A." + member.getName(), null);
     expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass));
   }
   CodeStyleManager codeStyleManager = manager.getCodeStyleManager();
   expressionFromText = (PsiReferenceExpression) codeStyleManager.reformat(expressionFromText);
   return (PsiReferenceExpression) referenceExpression.replace(expressionFromText);
 }
 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);
 }