From 7d48d00ae96593b7f301db9c461db0aacd48c864 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 30 Jun 2016 19:36:08 +0300 Subject: [PATCH] anonymous -> lambda: restore comments (non-javadoc) outside method body (IDEA-157286) --- .../AnonymousCanBeLambdaInspection.java | 25 +++++++++++++++++-- .../LambdaCanBeMethodReferenceInspection.java | 8 +----- ...afterCommentsInsideAnonymousClassBody.java | 6 +++++ ...eforeCommentsInsideAnonymousClassBody.java | 8 ++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java index 20a5e6a63b2b..285593a66ee4 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/AnonymousCanBeLambdaInspection.java @@ -254,6 +254,7 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection final PsiCodeBlock body = method.getBody(); if (body == null) return null; + final Collection comments = collectCommentsOutsideMethodBody(anonymousClass, body); final Project project = element.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); @@ -277,12 +278,12 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection .giveUniqueNames(project, elementFactory, lambdaExpression, usedLocalNames, variables.toArray(new PsiVariable[variables.size()])); - final PsiExpression singleExpr = RedundantLambdaCodeBlockInspection.isCodeBlockRedundant( - lambdaExpression.getBody()); + final PsiExpression singleExpr = RedundantLambdaCodeBlockInspection.isCodeBlockRedundant(lambdaExpression.getBody()); if (singleExpr != null) { lambdaExpression.getBody().replace(singleExpr); } ChangeContextUtil.decodeContextInfo(lambdaExpression, null, null); + restoreComments(comments, lambdaExpression); final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); if (forceIgnoreTypeCast) { @@ -310,6 +311,16 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection return null; } + private static Collection collectCommentsOutsideMethodBody(PsiAnonymousClass anonymousClass, PsiCodeBlock body) { + final Collection psiComments = PsiTreeUtil.findChildrenOfType(anonymousClass, PsiComment.class); + for (Iterator iterator = psiComments.iterator(); iterator.hasNext(); ) { + if (PsiTreeUtil.isAncestor(body, iterator.next(), false)) { + iterator.remove(); + } + } + return ContainerUtil.map(psiComments, (comment) -> (PsiComment)comment.copy()); + } + private static void collectLocalVariablesDefinedInsideLambda(PsiLambdaExpression lambdaExpression, final Set variables, Set namesOfVariablesInTheBlock) { @@ -452,6 +463,16 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection return false; } + public static void restoreComments(Collection comments, PsiElement lambda) { + PsiElement anchor = PsiTreeUtil.getParentOfType(lambda, PsiStatement.class, PsiField.class); + if (anchor == null) { + anchor = lambda; + } + for (PsiComment comment : comments) { + anchor.getParent().addBefore(comment, anchor); + } + } + private static class ForbiddenRefsChecker extends JavaRecursiveElementWalkingVisitor { private boolean myBodyContainsForbiddenRefs; diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java index 10e87f61971b..3cc95bbb53db 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/LambdaCanBeMethodReferenceInspection.java @@ -518,13 +518,7 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp replace = replace.replace(cast); } - PsiElement anchor = PsiTreeUtil.getParentOfType(replace, PsiStatement.class); - if (anchor == null) { - anchor = replace; - } - for (PsiComment comment : comments) { - anchor.getParent().addBefore(comment, anchor); - } + AnonymousCanBeLambdaInspection.restoreComments(comments, replace); JavaCodeStyleManager.getInstance(project).shortenClassReferences(replace); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java new file mode 100644 index 000000000000..64800159149d --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/afterCommentsInsideAnonymousClassBody.java @@ -0,0 +1,6 @@ +// "Replace with lambda" "true" + +class Test { + //my comment + Runnable r = () -> {}; +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java new file mode 100644 index 000000000000..304804c3853f --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/anonymous2lambda/beforeCommentsInsideAnonymousClassBody.java @@ -0,0 +1,8 @@ +// "Replace with lambda" "true" + +class Test { + Runnable r = new Runnable() { + //my comment + public void run () {} + }; +} \ No newline at end of file