void (@NotNull TemplateState state, Template template) { final TextResult value = state.getVariableValue(""); assert value != null; final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(project).getResolveHelper(); final PsiVariable target = resolveHelper.resolveAccessibleReferencedVariable(value.getText(), instanceOfExpression); if (target instanceof PsiField field) { final CaretModel caretModel = editor.getCaretModel(); final PsiElement elementAt = file.findElementAt(caretModel.getOffset()); final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class); if (declarationStatement != null) { final PsiLocalVariable variable = (PsiLocalVariable)declarationStatement.getDeclaredElements()[0]; final PsiExpression initializer = variable.getInitializer(); assert initializer != null; ApplicationManager.getApplication().runWriteAction(() -> { initializer.accept(new JavaRecursiveElementVisitor() { @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) { final PsiExpression qualifierExpression = expression.getQualifierExpression(); if (qualifierExpression != null) { qualifierExpression.accept(this); } else if (expression.resolve() == variable) { RefactoringChangeUtil.qualifyReference(expression, field, field.hasModifierProperty(PsiModifier.STATIC) ? field.getContainingClass() : null); } } }); }); PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); } } }