@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 void addExprTypesWhenContainerElement(LinkedHashSet<PsiType> set, PsiExpression expr) {
   if (expr instanceof PsiMethodCallExpression) {
     PsiMethodCallExpression callExpr = (PsiMethodCallExpression) expr;
     PsiReferenceExpression methodExpr = callExpr.getMethodExpression();
     String methodName = methodExpr.getReferenceName();
     MethodPattern pattern =
         myMethodPatternMap.findPattern(
             methodName, callExpr.getArgumentList().getExpressions().length);
     if (pattern != null && pattern.parameterIndex < 0 /* return value */) {
       PsiExpression qualifier = methodExpr.getQualifierExpression();
       if (qualifier != null) {
         PsiType[] types = guessContainerElementType(qualifier, null);
         for (PsiType type : types) {
           if (type instanceof PsiClassType) {
             if (((PsiClassType) type).resolve() instanceof PsiAnonymousClass) continue;
           }
           set.add(type);
         }
       }
     }
   }
 }
Exemple #3
0
  private NamesByExprInfo suggestVariableNameByExpressionOnly(
      PsiExpression expr,
      final VariableKind variableKind,
      boolean correctKeywords,
      boolean useAllMethodNames) {
    if (expr instanceof PsiMethodCallExpression) {
      PsiReferenceExpression methodExpr = ((PsiMethodCallExpression) expr).getMethodExpression();
      String methodName = methodExpr.getReferenceName();
      if (methodName != null) {
        if ("of".equals(methodName) || "ofNullable".equals(methodName)) {
          if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) {
            PsiExpression[] expressions =
                ((PsiMethodCallExpression) expr).getArgumentList().getExpressions();
            if (expressions.length > 0) {
              return suggestVariableNameByExpressionOnly(
                  expressions[0], variableKind, correctKeywords, useAllMethodNames);
            }
          }
        }
        if ("map".equals(methodName)
            || "flatMap".equals(methodName)
            || "filter".equals(methodName)) {
          if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) {
            return new NamesByExprInfo(null);
          }
        }

        String[] words = NameUtil.nameToWords(methodName);
        if (words.length > 0) {
          final String firstWord = words[0];
          if (GET_PREFIX.equals(firstWord)
              || IS_PREFIX.equals(firstWord)
              || FIND_PREFIX.equals(firstWord)
              || CREATE_PREFIX.equals(firstWord)) {
            if (words.length > 1) {
              final String propertyName = methodName.substring(firstWord.length());
              String[] names =
                  getSuggestionsByName(propertyName, variableKind, false, correctKeywords);
              final PsiExpression qualifierExpression = methodExpr.getQualifierExpression();
              if (qualifierExpression instanceof PsiReferenceExpression
                  && ((PsiReferenceExpression) qualifierExpression).resolve()
                      instanceof PsiVariable) {
                names =
                    ArrayUtil.append(
                        names,
                        StringUtil.sanitizeJavaIdentifier(
                            changeIfNotIdentifier(
                                qualifierExpression.getText()
                                    + StringUtil.capitalize(propertyName))));
              }
              return new NamesByExprInfo(propertyName, names);
            }
          } else if (words.length == 1 || useAllMethodNames) {
            return new NamesByExprInfo(
                methodName, getSuggestionsByName(methodName, variableKind, false, correctKeywords));
          }
        }
      }
    } else if (expr instanceof PsiReferenceExpression) {
      String propertyName = ((PsiReferenceExpression) expr).getReferenceName();
      PsiElement refElement = ((PsiReferenceExpression) expr).resolve();
      if (refElement instanceof PsiVariable) {
        VariableKind refVariableKind = getVariableKind((PsiVariable) refElement);
        propertyName = variableNameToPropertyName(propertyName, refVariableKind);
      }
      if (refElement != null && propertyName != null) {
        String[] names = getSuggestionsByName(propertyName, variableKind, false, correctKeywords);
        return new NamesByExprInfo(propertyName, names);
      }
    } else if (expr instanceof PsiArrayAccessExpression) {
      PsiExpression arrayExpr = ((PsiArrayAccessExpression) expr).getArrayExpression();
      if (arrayExpr instanceof PsiReferenceExpression) {
        String arrayName = ((PsiReferenceExpression) arrayExpr).getReferenceName();
        PsiElement refElement = ((PsiReferenceExpression) arrayExpr).resolve();
        if (refElement instanceof PsiVariable) {
          VariableKind refVariableKind = getVariableKind((PsiVariable) refElement);
          arrayName = variableNameToPropertyName(arrayName, refVariableKind);
        }

        if (arrayName != null) {
          String name = StringUtil.unpluralize(arrayName);
          if (name != null) {
            String[] names = getSuggestionsByName(name, variableKind, false, correctKeywords);
            return new NamesByExprInfo(name, names);
          }
        }
      }
    } else if (expr instanceof PsiLiteralExpression
        && variableKind == VariableKind.STATIC_FINAL_FIELD) {
      final PsiLiteralExpression literalExpression = (PsiLiteralExpression) expr;
      final Object value = literalExpression.getValue();
      if (value instanceof String) {
        final String stringValue = (String) value;
        String[] names = getSuggestionsByValue(stringValue);
        if (names.length > 0) {
          return new NamesByExprInfo(null, constantValueToConstantName(names));
        }
      }
    } else if (expr instanceof PsiParenthesizedExpression) {
      return suggestVariableNameByExpressionOnly(
          ((PsiParenthesizedExpression) expr).getExpression(),
          variableKind,
          correctKeywords,
          useAllMethodNames);
    } else if (expr instanceof PsiTypeCastExpression) {
      return suggestVariableNameByExpressionOnly(
          ((PsiTypeCastExpression) expr).getOperand(),
          variableKind,
          correctKeywords,
          useAllMethodNames);
    } else if (expr instanceof PsiLiteralExpression) {
      final String text = StringUtil.stripQuotesAroundValue(expr.getText());
      if (isIdentifier(text)) {
        return new NamesByExprInfo(
            text, getSuggestionsByName(text, variableKind, false, correctKeywords));
      }
    }

    return new NamesByExprInfo(null, ArrayUtil.EMPTY_STRING_ARRAY);
  }