void (final GrMethod method, final Collection<GrReferenceExpression> usagesToConvert) { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject()); StringBuilder builder = new StringBuilder(method.getTextLength()); String modifiers = method.getModifierList().getText(); if (modifiers.trim().isEmpty()) { modifiers = GrModifier.DEF; } builder.append(modifiers).append(' '); builder.append(method.getName()).append("={"); GrParameterList parameterList = method.getParameterList(); builder.append(parameterList.getParametersRange().shiftLeft( parameterList.getTextRange().getStartOffset() ).subSequence( parameterList.getText() )); builder.append(" ->"); final GrOpenBlock block = method.getBlock(); builder.append(block.getText().substring(1)); final GrVariableDeclaration variableDeclaration = GroovyPsiElementFactory.getInstance(method.getProject()).createFieldDeclarationFromText(builder.toString()); method.replace(variableDeclaration); for (GrReferenceExpression element : usagesToConvert) { final PsiElement qualifier = element.getQualifier(); final StringBuilder text = new StringBuilder(qualifier.getText()); element.setQualifier(null); text.append('.').append(element.getText()); element.replace(factory.createExpressionFromText(text.toString())); } } private static class MyPredicate implements PsiElementPredicate { @Override public boolean satisfiedBy(@NotNull PsiElement element) { if (element.getLanguage() != GroovyLanguage.INSTANCE) return false; GrMethod method; final PsiReference ref = element.getReference(); if (ref != null) { final PsiElement resolved = ref.resolve(); if (!(resolved instanceof GrMethod)) return false; method = (GrMethod)resolved; } else { final PsiElement parent = element.getParent(); if (!(parent instanceof GrMethod)) return false; if (((GrMethod)parent).getNameIdentifierGroovy() != element) return false; method = (GrMethod)parent; } return !method.isConstructor() && isJavaIdentifier(method.getName()) && method.hasBlock() && method.getParent() instanceof GrTypeDefinitionBody; } } }