Exemple #1
0
 @Nullable
 private static PsiImportStaticStatement findOnDemandImportStaticStatement(
     PsiImportList importList, String qualifierClass) {
   final PsiImportStaticStatement[] importStaticStatements =
       importList.getImportStaticStatements();
   for (PsiImportStaticStatement importStaticStatement : importStaticStatements) {
     if (!importStaticStatement.isOnDemand()) {
       continue;
     }
     final PsiJavaCodeReferenceElement importReference =
         importStaticStatement.getImportReference();
     if (importReference == null) {
       continue;
     }
     final String text = importReference.getText();
     if (qualifierClass.equals(text)) {
       return importStaticStatement;
     }
   }
   return null;
 }
Exemple #2
0
 private static boolean hasExactImportStaticConflict(
     String qualifierClass, String memberName, PsiElement context) {
   final PsiFile file = context.getContainingFile();
   if (!(file instanceof PsiJavaFile)) {
     return false;
   }
   final PsiJavaFile javaFile = (PsiJavaFile) file;
   final PsiImportList importList = javaFile.getImportList();
   if (importList == null) {
     return false;
   }
   final PsiImportStaticStatement[] importStaticStatements =
       importList.getImportStaticStatements();
   for (PsiImportStaticStatement importStaticStatement : importStaticStatements) {
     if (importStaticStatement.isOnDemand()) {
       continue;
     }
     final String name = importStaticStatement.getReferenceName();
     if (!memberName.equals(name)) {
       continue;
     }
     final PsiJavaCodeReferenceElement importReference =
         importStaticStatement.getImportReference();
     if (importReference == null) {
       continue;
     }
     final PsiElement qualifier = importReference.getQualifier();
     if (qualifier == null) {
       continue;
     }
     final String qualifierText = qualifier.getText();
     if (!qualifierClass.equals(qualifierText)) {
       return true;
     }
   }
   return false;
 }
Exemple #3
0
  private static void addNamesToImport(
      @NotNull Set<String> names,
      @NotNull PsiElement scope,
      @NotNull String thisPackageName,
      @NotNull Set<String> namesToImportStaticly,
      PsiFile context) {
    if (scope instanceof PsiImportList) return;

    final LinkedList<PsiElement> stack = new LinkedList<PsiElement>();
    stack.add(scope);
    while (!stack.isEmpty()) {
      final PsiElement child = stack.removeFirst();
      if (child instanceof PsiImportList) continue;
      stack.addAll(Arrays.asList(child.getChildren()));

      for (final PsiReference reference : child.getReferences()) {
        if (!(reference instanceof PsiJavaReference)) continue;
        final PsiJavaReference javaReference = (PsiJavaReference) reference;
        if (javaReference instanceof JavaClassReference) {
          if (((JavaClassReference) javaReference).getContextReference() != null) continue;
        }
        PsiJavaCodeReferenceElement referenceElement = null;
        if (reference instanceof PsiJavaCodeReferenceElement) {
          referenceElement = (PsiJavaCodeReferenceElement) child;
          if (referenceElement.getQualifier() != null) {
            continue;
          }
          if (reference instanceof PsiJavaCodeReferenceElementImpl
              && ((PsiJavaCodeReferenceElementImpl) reference).getKind()
                  == PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND) {
            continue;
          }
        }

        final JavaResolveResult resolveResult = javaReference.advancedResolve(true);
        PsiElement refElement = resolveResult.getElement();
        if (refElement == null && referenceElement != null) {
          refElement = ResolveClassUtil.resolveClass(referenceElement); // might be uncomplete code
        }

        PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope();
        if (!(currentFileResolveScope instanceof PsiImportStatementBase)) continue;
        if (context != null
            && currentFileResolveScope instanceof JspxImportStatement
            && context != ((JspxImportStatement) currentFileResolveScope).getDeclarationFile()) {
          continue;
        }

        if (refElement != null) {
          // Add names imported statically
          if (referenceElement != null) {
            if (currentFileResolveScope instanceof PsiImportStaticStatement) {
              PsiImportStaticStatement importStaticStatement =
                  (PsiImportStaticStatement) currentFileResolveScope;
              String name = importStaticStatement.getImportReference().getCanonicalText();
              if (importStaticStatement.isOnDemand()) {
                String refName = referenceElement.getReferenceName();
                if (refName != null) name = name + "." + refName;
              }
              names.add(name);
              namesToImportStaticly.add(name);
              continue;
            }
          }

          if (refElement instanceof PsiClass) {
            String qName = ((PsiClass) refElement).getQualifiedName();
            if (hasPackage(qName, thisPackageName)) continue;
            names.add(qName);
          }
        }
      }
    }
  }