PsiAnonymousClass (@NotNull Project project, @NotNull PsiLambdaExpression lambdaExpression) { final PsiParameter[] paramListCopy = ((PsiParameterList)lambdaExpression.getParameterList().copy()).getParameters(); final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType(); LOG.assertTrue(functionalInterfaceType != null); final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType); LOG.assertTrue(method != null); final String blockText = getBodyText(lambdaExpression); if (blockText == null) return null; final PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project); PsiCodeBlock blockFromText = psiElementFactory.createCodeBlockFromText(blockText, lambdaExpression); qualifyThisExpressions(lambdaExpression, psiElementFactory, blockFromText); blockFromText = psiElementFactory.createCodeBlockFromText(blockFromText.getText(), null); PsiNewExpression newExpression = (PsiNewExpression)psiElementFactory.createExpressionFromText("new " + functionalInterfaceType.getCanonicalText() + "(){}", lambdaExpression); newExpression = (PsiNewExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.replace(newExpression)); final PsiAnonymousClass anonymousClass = newExpression.getAnonymousClass(); LOG.assertTrue(anonymousClass != null); final List<PsiGenerationInfo<PsiMethod>> infos = OverrideImplementUtil.overrideOrImplement(anonymousClass, method); if (infos.size() == 1) { PsiMethod member = infos.get(0).getPsiMember(); final PsiParameter[] parameters = member.getParameterList().getParameters(); if (parameters.length == paramListCopy.length) { for (int i = 0; i < parameters.length; i++) { final PsiParameter parameter = parameters[i]; final String lambdaParamName = paramListCopy[i].getName(); parameter.setName(lambdaParamName); } } PsiCodeBlock codeBlock = member.getBody(); LOG.assertTrue(codeBlock != null); codeBlock.replace(blockFromText); final PsiElement parent = anonymousClass.getParent().getParent(); if (parent instanceof PsiTypeCastExpression && RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)parent)) { final PsiExpression operand = ((PsiTypeCastExpression)parent).getOperand(); LOG.assertTrue(operand != null); parent.replace(operand); } } return anonymousClass; }