PsiExpression (@NotNull PsiExpression expression) { PsiReferenceExpression referenceExpression = tryCast(PsiUtil.skipParenthesizedExprDown(expression), PsiReferenceExpression.class); if (referenceExpression == null) return null; PsiVariable variable = tryCast(referenceExpression.resolve(), PsiVariable.class); if (variable == null) return null; PsiCodeBlock block = tryCast(PsiUtil.getVariableCodeBlock(variable, null), PsiCodeBlock.class); if (block == null) return null; PsiElement[] defs = DefUseUtil.getDefs(block, variable, expression); if (defs.length != 1) return null; PsiElement def = defs[0]; if(def instanceof PsiLocalVariable) { return ((PsiLocalVariable)def).getInitializer(); } if(def instanceof PsiReferenceExpression) { PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(def.getParent()); if(assignment != null && assignment.getLExpression() == def) { return assignment.getRExpression(); } } if(def instanceof PsiExpression) { return (PsiExpression)def; } return null; }