PsiElement (@NotNull final PsiMember member, @NotNull final PsiClass targetClass, Set<PsiMember> membersToMove) { if (member instanceof GrField && member.hasModifierProperty(PsiModifier.STATIC)) { final List<PsiField> referencedFields = new ArrayList<>(); final GrExpression psiExpression = ((GrField)member).getInitializerGroovy(); if (psiExpression != null) { psiExpression.accept(new GroovyRecursiveElementVisitor() { @Override public void visitReferenceExpression(@NotNull final GrReferenceExpression expression) { super.visitReferenceExpression(expression); final PsiElement psiElement = expression.resolve(); if (psiElement instanceof GrField grField) { if (grField.getContainingClass() == targetClass && !referencedFields.contains(grField)) { referencedFields.add(grField); } } } }); } if (!referencedFields.isEmpty()) { referencedFields.sort((o1, o2) -> -PsiUtilCore.compareElementsByPosition(o1, o2)); return referencedFields.get(0); } } return null; }