void (final GrField field, final Collection<PsiElement> fieldUsages) { final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(field.getProject()); final StringBuilder builder = new StringBuilder(field.getTextLength()); final GrClosableBlock block = (GrClosableBlock)field.getInitializerGroovy(); final GrModifierList modifierList = field.getModifierList(); if (modifierList.getModifiers().length > 0 || modifierList.getAnnotations().length > 0) { builder.append(modifierList.getText()); } else { builder.append(GrModifier.DEF); } builder.append(' ').append(field.getName()); builder.append('('); if (block.hasParametersSection()) { builder.append(block.getParameterList().getText()); } else { builder.append("def it = null"); } builder.append(") {"); ApplicationManager.getApplication().runWriteAction(() -> { block.getParameterList().delete(); block.getLBrace().delete(); final PsiElement psiElement = PsiUtil.skipWhitespacesAndComments(block.getFirstChild(), true); if (psiElement != null && "->".equals(psiElement.getText())) { psiElement.delete(); } builder.append(block.getText()); final GrMethod method = GroovyPsiElementFactory.getInstance(field.getProject()).createMethodFromText(builder.toString()); field.getParent().replace(method); for (PsiElement usage : fieldUsages) { if (usage instanceof GrReferenceExpression) { final PsiElement parent = usage.getParent(); StringBuilder newRefText = new StringBuilder(); if (parent instanceof GrReferenceExpression && usage == ((GrReferenceExpression)parent).getQualifier() && "call".equals(((GrReferenceExpression)parent).getReferenceName())) { newRefText.append(usage.getText()); usage = parent; } else { PsiElement qualifier = ((GrReferenceExpression)usage).getQualifier(); if (qualifier == null) { if (parent instanceof GrReferenceExpression && ((GrReferenceExpression)parent).getQualifier() != null && usage != ((GrReferenceExpression)parent).getQualifier()) { qualifier = ((GrReferenceExpression)parent).getQualifier(); usage = parent; } } if (qualifier != null) { newRefText.append(qualifier.getText()).append('.'); ((GrReferenceExpression)usage).setQualifier(null); } else { newRefText.append("this."); } newRefText.append('&').append(usage.getText()); } usage.replace(factory.createReferenceExpressionFromText(newRefText.toString())); } } }); } private static class MyPredicate implements PsiElementPredicate { @Override public boolean satisfiedBy(@NotNull PsiElement element) { if (element.getLanguage() != GroovyLanguage.INSTANCE) return false; final PsiElement parent = element.getParent(); if (!(parent instanceof GrField field)) return false; if (field.getNameIdentifierGroovy() != element) return false; final PsiElement varDeclaration = field.getParent(); if (!(varDeclaration instanceof GrVariableDeclaration)) return false; if (((GrVariableDeclaration)varDeclaration).getVariables().length != 1) return false; final GrExpression expression = field.getInitializerGroovy(); return expression instanceof GrClosableBlock; } } }