boolean (@NotNull PsiFile file, @NotNull PsiElement element, @NotNull List<@NotNull PsiJavaCodeReferenceElement> dequalifiedElements) { final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement)element.getParent(); final PsiClass aClass = (PsiClass)refExpr.resolve(); if (aClass == null) { return false; } final PsiClass containingClass = PsiUtil.getTopLevelClass(refExpr); if (aClass != containingClass || !ClassUtils.isInsideClassBody(element, aClass)) { PsiImportList importList = ((PsiJavaFile)file).getImportList(); if (importList == null) { return false; } boolean alreadyImported = false; for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) { if (!statement.isOnDemand()) continue; PsiClass staticResolve = statement.resolveTargetClass(); if (aClass == staticResolve) { alreadyImported = true; break; } } if (!alreadyImported) { PsiImportStaticStatement importStaticStatement = JavaPsiFacade.getElementFactory(file.getProject()).createImportStaticStatement(aClass, "*"); importList.add(importStaticStatement); } } Ref<Boolean> conflict = new Ref<>(false); List<PsiFile> roots = file.getViewProvider().getAllFiles(); for (final PsiFile root : roots) { PsiElement copy = root.copy(); final PsiManager manager = root.getManager(); List<PsiJavaCodeReferenceElement> expressionsToDequalify = new ArrayList<>(); copy.accept(new JavaRecursiveElementWalkingVisitor() { int delta; @Override public void visitReferenceElement(@NotNull PsiJavaCodeReferenceElement expression) { PsiElement qualifierExpression; if (!isParameterizedReference(expression) && !(expression instanceof PsiMethodReferenceExpression) && !(expression.getParent() instanceof PsiErrorElement) && (qualifierExpression = expression.getQualifier()) instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)qualifierExpression).isReferenceTo(aClass)) { try { JavaResolveResult[] resolved = expression.multiResolve(false); int end = expression.getTextRange().getEndOffset(); qualifierExpression.delete(); PsiElement firstChild = expression.getFirstChild(); if (firstChild instanceof PsiJavaToken && ((PsiJavaToken)firstChild).getTokenType() == JavaTokenType.DOT) { firstChild.delete(); } delta += end - expression.getTextRange().getEndOffset(); JavaResolveResult[] resolvedAfter = expression.multiResolve(false); if (resolvesToSame(manager, resolved, resolvedAfter)) { int offset = expression.getTextRange().getStartOffset() + delta; PsiJavaCodeReferenceElement originalExpression = PsiTreeUtil.findElementOfClassAtOffset(root, offset, PsiJavaCodeReferenceElement.class, false); if (originalExpression != null) { PsiElement originalQualifier = originalExpression.getQualifier(); if (originalQualifier instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)originalQualifier).isReferenceTo(aClass)) { expressionsToDequalify.add(originalExpression); } } } else { conflict.set(true); } } catch (IncorrectOperationException e) { LOG.error(e); } } super.visitReferenceElement(expression); } }); for (PsiJavaCodeReferenceElement expression : expressionsToDequalify) { new CommentTracker().deleteAndRestoreComments(Objects.requireNonNull(expression.getQualifier())); } dequalifiedElements.addAll(expressionsToDequalify); } return conflict.get(); }