PsiClass (@NotNull GrReferenceExpression ref, @NotNull PsiField field) { PsiElement context = ref; while (true) { PsiClass aClass = PsiUtil.getContextClass(context); if (aClass == null) return null; PsiManager manager = context.getManager(); PsiField found = aClass.findFieldByName(field.getName(), true); if (manager.areElementsEquivalent(found, field)) { return aClass; } if (context instanceof GroovyScriptClass) return null; if (context.getParent() instanceof GroovyFile) return null; context = aClass.getParent(); } }