PsiElement (final PsiVariable variable, final PsiElement context) { PsiElement codeBlock; if (variable instanceof PsiParameter) { codeBlock = ((PsiParameter)variable).getDeclarationScope(); if (codeBlock instanceof PsiMethod) { codeBlock = ((PsiMethod)codeBlock).getBody(); } } else if (variable instanceof PsiLocalVariable) { codeBlock = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); } else { codeBlock = DfaPsiUtil.getTopmostBlockInSameClass(context); } while (codeBlock != null) { PsiAnonymousClass anon = PsiTreeUtil.getParentOfType(codeBlock, PsiAnonymousClass.class); if (anon == null) break; codeBlock = PsiTreeUtil.getParentOfType(anon, PsiCodeBlock.class); } return codeBlock; }