@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;
 }
 @Override
 @NotNull
 public String buildErrorString(Object... infos) {
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) infos[0];
   return InspectionGadgetsBundle.message(
       "cast.conflicts.with.instanceof.problem.descriptor", referenceExpression.getText());
 }
 @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);
 }
 private static void replaceReferences(
     List<PsiReferenceExpression> references, PsiElement newExpression)
     throws IncorrectOperationException {
   for (PsiReferenceExpression reference : references) {
     reference.replace(newExpression);
   }
 }
 @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);
   }
 }
Exemplo n.º 6
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;
  }
Exemplo n.º 7
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;
 }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiIdentifier name = (PsiIdentifier) descriptor.getPsiElement();
   final PsiReferenceExpression expression = (PsiReferenceExpression) name.getParent();
   if (expression == null) {
     return;
   }
   final PsiMethodCallExpression call = (PsiMethodCallExpression) expression.getParent();
   final String methodName = expression.getReferenceName();
   if (call == null) {
     return;
   }
   final PsiMethod method = call.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   final PsiExpressionList argumentList = call.getArgumentList();
   if (containingClass == null) {
     return;
   }
   final String containingClassName = containingClass.getQualifiedName();
   final String argText = argumentList.getText();
   PsiReplacementUtil.replaceExpressionAndShorten(
       call, containingClassName + '.' + methodName + argText);
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) {
   super.visitMethodCallExpression(call);
   final PsiReferenceExpression methodExpression = call.getMethodExpression();
   final PsiElement qualifier = methodExpression.getQualifier();
   if (!(qualifier instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiMethod method = call.resolveMethod();
   if (method == null) {
     return;
   }
   if (!method.hasModifierProperty(PsiModifier.STATIC)) {
     return;
   }
   final PsiElement referent = ((PsiReference) qualifier).resolve();
   if (!(referent instanceof PsiClass)) {
     return;
   }
   final PsiClass referencedClass = (PsiClass) referent;
   final PsiClass declaringClass = method.getContainingClass();
   if (declaringClass == null) {
     return;
   }
   if (declaringClass.equals(referencedClass)) {
     return;
   }
   final PsiClass containingClass = ClassUtils.getContainingClass(call);
   if (!ClassUtils.isClassVisibleFromClass(containingClass, declaringClass)) {
     return;
   }
   registerMethodCallError(call, declaringClass, referencedClass);
 }
  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));
    }
  }
 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);
 }
 @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 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);
 }
Exemplo n.º 14
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);
      }
    }
  }
 @Nullable
 private static PsiClass getFieldOrMethodAccessedClass(
     PsiReferenceExpression ref, PsiClass fieldOrMethodClass) {
   PsiElement[] children = ref.getChildren();
   if (children.length > 1 && children[0] instanceof PsiExpression) {
     PsiExpression expr = (PsiExpression) children[0];
     PsiType type = expr.getType();
     if (type != null) {
       if (!(type instanceof PsiClassType)) return null;
       return PsiUtil.resolveClassInType(type);
     } else {
       if (expr instanceof PsiReferenceExpression) {
         PsiElement refElement = ((PsiReferenceExpression) expr).resolve();
         if (refElement instanceof PsiClass) return (PsiClass) refElement;
       }
       return null;
     }
   }
   PsiManager manager = ref.getManager();
   for (PsiElement parent = ref; parent != null; parent = parent.getParent()) {
     if (parent instanceof PsiClass
         && (manager.areElementsEquivalent(parent, fieldOrMethodClass)
             || ((PsiClass) parent).isInheritor(fieldOrMethodClass, true))) {
       return (PsiClass) parent;
     }
   }
   return null;
 }
 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 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;
 }
 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);
 }
Exemplo n.º 20
0
 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();
   }
 }
 @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);
 }
 public static String buildCollectionText(PsiForStatement forStatement) {
   PsiStatement body = forStatement.getBody();
   while (body instanceof PsiBlockStatement) {
     final PsiBlockStatement blockStatement = (PsiBlockStatement) body;
     final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
     final PsiStatement[] statements = codeBlock.getStatements();
     if (statements.length == 2) {
       body = statements[1];
     } else if (statements.length == 1) {
       body = statements[0];
     } else {
       return null;
     }
   }
   if (!(body instanceof PsiExpressionStatement)) {
     return null;
   }
   final PsiExpressionStatement expressionStatement = (PsiExpressionStatement) body;
   final PsiExpression expression = expressionStatement.getExpression();
   if (!(expression instanceof PsiMethodCallExpression)) {
     return null;
   }
   final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) expression;
   final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
   final PsiElement qualifier = methodExpression.getQualifier();
   if (qualifier == null) {
     // fixme for when the array is added to 'this'
     return null;
   }
   return qualifier.getText();
 }
 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;
 }
 @Override
 public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
   super.visitMethodCallExpression(expression);
   final PsiReferenceExpression methodExpression = expression.getMethodExpression();
   final String methodName = methodExpression.getReferenceName();
   @NonNls final String signal = "signal";
   if (!signal.equals(methodName)) {
     return;
   }
   final PsiExpressionList argumentList = expression.getArgumentList();
   if (argumentList.getExpressions().length != 0) {
     return;
   }
   final PsiMethod method = expression.resolveMethod();
   if (method == null) {
     return;
   }
   final PsiClass containingClass = method.getContainingClass();
   if (containingClass == null) {
     return;
   }
   if (!ClassUtils.isSubclass(containingClass, "java.util.concurrent.locks.Condition")) {
     return;
   }
   registerMethodCallError(expression);
 }
 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;
 }
  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;
  }
 public void testInferInCall1() throws Exception {
   PsiReferenceExpression ref = configure();
   PsiType type = ref.getType();
   assertTrue(type instanceof PsiClassType);
   PsiType[] paramTypes = ((PsiClassType) type).getParameters();
   assertEquals(1, paramTypes.length);
   assertEquals(CommonClassNames.JAVA_LANG_OBJECT, paramTypes[0].getCanonicalText());
 }
 public void testInferWithBounds1() throws Exception {
   PsiReferenceExpression ref = configure();
   JavaResolveResult resolveResult = ref.advancedResolve(false);
   PsiSubstitutor substitutor = resolveResult.getSubstitutor();
   PsiMethod method = (PsiMethod) resolveResult.getElement();
   PsiType type = substitutor.substitute(method.getTypeParameters()[0]);
   assertEquals("java.lang.String", type.getCanonicalText());
 }
 public void testInferInAssign1() throws Exception {
   PsiReferenceExpression ref = configure();
   PsiType type = ref.getType();
   assertTrue(type instanceof PsiClassType);
   PsiType[] paramTypes = ((PsiClassType) type).getParameters();
   assertEquals(1, paramTypes.length);
   assertEquals("java.lang.String", paramTypes[0].getCanonicalText());
 }
 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();
 }