WrapperCandidate (CollectTerminal terminal, PsiElement element) { PsiExpression candidate; PsiType type; PsiLocalVariable var = null; if (element instanceof PsiReturnStatement) { candidate = ((PsiReturnStatement)element).getReturnValue(); type = PsiTypesUtil.getMethodReturnType(element); } else { PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(element); if (assignment != null) { candidate = assignment.getRExpression(); type = assignment.getType(); } else if (element instanceof PsiDeclarationStatement) { PsiElement[] elements = ((PsiDeclarationStatement)element).getDeclaredElements(); if (elements.length != 1 || !(elements[0] instanceof PsiLocalVariable)) return null; var = (PsiLocalVariable)elements[0]; candidate = var.getInitializer(); type = var.getType(); } else { return null; } if (candidate != null && terminal.targetReferences().anyMatch(ref -> !PsiTreeUtil.isAncestor(element, ref, false))) { return null; } } return new WrapperCandidate(candidate, type, var); }