From 0dbf026ae34ee33e353e455b12045808da042ddd Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 6 Sep 2021 13:57:11 +0700 Subject: [PATCH] [java-templates] Remove 'new' template if all constructors are inaccessible GitOrigin-RevId: 1620d578f691a6071d70e5497029e30e5eca40d6 --- .../templates/NewExpressionPostfixTemplate.java | 14 +++++++++++++- .../postfix/templates/new/newInaccessible.java | 5 +++++ .../templates/new/newInaccessible_after.java | 5 +++++ .../NewExpressionPostfixTemplateTest.java | 6 ++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible.java create mode 100644 java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewExpressionPostfixTemplate.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewExpressionPostfixTemplate.java index 7c9bc780ae81..b8c3f5573d5f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewExpressionPostfixTemplate.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/templates/NewExpressionPostfixTemplate.java @@ -11,6 +11,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; +import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -36,7 +37,18 @@ public class NewExpressionPostfixTemplate extends StringBasedPostfixTemplate { JavaResolveResult result = ref.advancedResolve(true); PsiElement element = result.getElement(); - return element == null || element instanceof PsiClass; + if (element != null && !(element instanceof PsiClass)) return false; + if (element != null) { + PsiMethod[] constructors = ((PsiClass)element).getConstructors(); + if (constructors.length > 0) { + PsiResolveHelper helper = JavaPsiFacade.getInstance(element.getProject()).getResolveHelper(); + if (ContainerUtil.and(constructors, m -> !helper.isAccessible(m, ref, (PsiClass)element))) { + // All constructors aren't accessible + return false; + } + } + } + return true; }; protected NewExpressionPostfixTemplate() { diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible.java b/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible.java new file mode 100644 index 000000000000..a5b7d76804a5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + Test.new + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible_after.java b/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible_after.java new file mode 100644 index 000000000000..6df888161b4f --- /dev/null +++ b/java/java-tests/testData/codeInsight/template/postfix/templates/new/newInaccessible_after.java @@ -0,0 +1,5 @@ +public class Foo { + void m() { + Test.new + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewExpressionPostfixTemplateTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewExpressionPostfixTemplateTest.java index 63f2355cc33f..7b9fb270432d 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewExpressionPostfixTemplateTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/template/postfix/templates/NewExpressionPostfixTemplateTest.java @@ -62,4 +62,10 @@ public class NewExpressionPostfixTemplateTest extends PostfixTemplateTestCase { public void testNewAfterNew() { doTest(); } + + public void testNewInaccessible() { + myFixture.addClass("class Test {private Test() {}}"); + doTest(); + } + }