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();
 }
 @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);
 }
 public static int getPrecedence(PsiExpression expression) {
   if (expression instanceof PsiThisExpression
       || expression instanceof PsiLiteralExpression
       || expression instanceof PsiSuperExpression
       || expression instanceof PsiClassObjectAccessExpression
       || expression instanceof PsiArrayAccessExpression
       || expression instanceof PsiArrayInitializerExpression) {
     return LITERAL_PRECEDENCE;
   }
   if (expression instanceof PsiReferenceExpression) {
     final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression;
     if (referenceExpression.getQualifier() != null) {
       return METHOD_CALL_PRECEDENCE;
     } else {
       return LITERAL_PRECEDENCE;
     }
   }
   if (expression instanceof PsiMethodCallExpression || expression instanceof PsiNewExpression) {
     return METHOD_CALL_PRECEDENCE;
   }
   if (expression instanceof PsiTypeCastExpression) {
     return TYPE_CAST_PRECEDENCE;
   }
   if (expression instanceof PsiPrefixExpression) {
     return PREFIX_PRECEDENCE;
   }
   if (expression instanceof PsiPostfixExpression) {
     return POSTFIX_PRECEDENCE;
   }
   if (expression instanceof PsiPolyadicExpression) {
     final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) expression;
     return getPrecedenceForOperator(polyadicExpression.getOperationTokenType());
   }
   if (expression instanceof PsiInstanceOfExpression) {
     return RELATIONAL_PRECEDENCE;
   }
   if (expression instanceof PsiConditionalExpression) {
     return CONDITIONAL_PRECEDENCE;
   }
   if (expression instanceof PsiAssignmentExpression) {
     return ASSIGNMENT_PRECEDENCE;
   }
   if (expression instanceof PsiParenthesizedExpression) {
     return PARENTHESIZED_PRECEDENCE;
   }
   return -1;
 }
예제 #4
0
  @Nullable
  private static LookupElementWeigher preferStatics(
      PsiElement position, final ExpectedTypeInfo[] infos) {
    if (PsiTreeUtil.getParentOfType(position, PsiDocComment.class) != null) {
      return null;
    }
    if (position.getParent() instanceof PsiReferenceExpression) {
      final PsiReferenceExpression refExpr = (PsiReferenceExpression) position.getParent();
      final PsiElement qualifier = refExpr.getQualifier();
      if (qualifier == null) {
        return null;
      }
      if (!(qualifier instanceof PsiJavaCodeReferenceElement)
          || !(((PsiJavaCodeReferenceElement) qualifier).resolve() instanceof PsiClass)) {
        return null;
      }
    }

    return new LookupElementWeigher("statics") {
      @NotNull
      @Override
      public Comparable weigh(@NotNull LookupElement element) {
        final Object o = element.getObject();
        if (o instanceof PsiKeyword) return -3;
        if (!(o instanceof PsiMember)
            || element.getUserData(JavaGenerateMemberCompletionContributor.GENERATE_ELEMENT)
                != null) {
          return 0;
        }

        if (((PsiMember) o).hasModifierProperty(PsiModifier.STATIC) && !hasNonVoid(infos)) {
          if (o instanceof PsiMethod) return -5;
          if (o instanceof PsiField) return -4;
        }

        if (o instanceof PsiClass) return -3;

        // instance method or field
        return -5;
      }
    };
  }
  @Nullable
  private static LookupElementWeigher preferStatics(PsiElement position) {
    if (PsiTreeUtil.getParentOfType(position, PsiDocComment.class) != null) {
      return null;
    }
    if (position.getParent() instanceof PsiReferenceExpression) {
      final PsiReferenceExpression refExpr = (PsiReferenceExpression) position.getParent();
      final PsiElement qualifier = refExpr.getQualifier();
      if (qualifier == null) {
        return null;
      }
      if (!(qualifier instanceof PsiJavaCodeReferenceElement)
          || !(((PsiJavaCodeReferenceElement) qualifier).resolve() instanceof PsiClass)) {
        return null;
      }
    }

    return new LookupElementWeigher("statics") {
      @NotNull
      @Override
      public Comparable weigh(@NotNull LookupElement element) {
        final Object o = element.getObject();
        if (o instanceof PsiKeyword) return -3;
        if (!(o instanceof PsiMember)) return 0;

        if (((PsiMember) o).hasModifierProperty(PsiModifier.STATIC)) {
          if (o instanceof PsiMethod) return -5;
          if (o instanceof PsiField) return -4;
        }

        if (o instanceof PsiClass) return -3;

        // instance method or field
        return -5;
      }
    };
  }
 @Nullable
 private static String getCollectionsAddAllText(PsiForeachStatement foreachStatement)
     throws IncorrectOperationException {
   final PsiStatement body = getBody(foreachStatement);
   if (!(body instanceof PsiExpressionStatement)) {
     return null;
   }
   final PsiExpressionStatement expressionStatement = (PsiExpressionStatement) body;
   final PsiMethodCallExpression methodCallExpression =
       (PsiMethodCallExpression) expressionStatement.getExpression();
   final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression();
   final PsiElement collection = methodExpression.getQualifier();
   if (collection == null) {
     // fixme for when the array is added to 'this'
     return null;
   }
   final String collectionText = collection.getText();
   final PsiExpression iteratedValue = foreachStatement.getIteratedValue();
   if (iteratedValue == null) {
     return null;
   }
   final String arrayText = iteratedValue.getText();
   @NonNls final StringBuilder buffer = new StringBuilder();
   if (PsiUtil.isLanguageLevel5OrHigher(foreachStatement)) {
     buffer.append("java.util.Collections.addAll(");
     buffer.append(collectionText);
     buffer.append(',');
     buffer.append(arrayText);
     buffer.append(");");
   } else {
     buffer.append(collectionText);
     buffer.append(".addAll(java.util.Arrays.asList(");
     buffer.append(arrayText);
     buffer.append("));");
   }
   return buffer.toString();
 }