String (@NotNull PsiFile fragment) { final List<String> imports = StringUtil.split(((JavaCodeFragment)fragment).importsToString(), ","); final Map<String, String> importMap = imports.stream().collect(Collectors.toMap(s -> s.substring(s.lastIndexOf('.') + 1), Function.identity())); final StringBuilder result = new StringBuilder(); fragment.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { visitReferenceElement(expression); } @Override public void visitReferenceElement(@NotNull PsiJavaCodeReferenceElement reference) { if (!reference.isQualified()) { final String text = reference.getText(); final String fqName = importMap.get(text); result.append(fqName != null ? fqName : text); } else { super.visitReferenceElement(reference); } } @Override public void visitElement(@NotNull PsiElement element) { super.visitElement(element); if (element.getFirstChild() == null) { result.append(element.getText()); } } }); return result.toString(); }