PsiType (PsiAnonymousClass aClass, PsiMethod method) { final PsiExpression expression = (PsiExpression)aClass.getParent(); final PsiType psiType = PsiTypesUtil.getExpectedTypeByParent(expression); if (psiType != null) { return psiType; } PsiExpression topExpr = expression; while (topExpr.getParent() instanceof PsiParenthesizedExpression) { topExpr = (PsiExpression)topExpr.getParent(); } final PsiCall call = LambdaUtil.treeWalkUp(topExpr); if (call != null && call.resolveMethod() != null) { Object marker = new Object(); PsiTreeUtil.mark(aClass, marker); PsiCall copyCall = LambdaUtil.copyTopLevelCall(call); if (copyCall == null) return null; final PsiElement classArg = PsiTreeUtil.releaseMark(copyCall, marker); if (classArg instanceof PsiAnonymousClass) { PsiExpression lambda = JavaPsiFacade.getElementFactory(aClass.getProject()) .createExpressionFromText(ReplaceWithLambdaFix.composeLambdaText(method), expression); lambda = (PsiExpression)classArg.getParent().replace(lambda); ((PsiLambdaExpression)lambda).getBody().replace(method.getBody()); final PsiType interfaceType; if (copyCall.resolveMethod() == null) { return PsiTypes.nullType(); } else { interfaceType = ((PsiLambdaExpression)lambda).getFunctionalInterfaceType(); } return interfaceType; } } return PsiTypes.nullType(); }