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(); + } + }