diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java index 539c6785d272..a814e7ebbde2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionSorting.java @@ -28,7 +28,10 @@ import gnu.trove.TObjectIntHashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import static com.intellij.patterns.PsiJavaPatterns.psiElement; @@ -71,10 +74,10 @@ public class JavaCompletionSorting { if (smart) { afterStats.add(new PreferDefaultTypeWeigher(expectedTypes, parameters, true)); } else { - ContainerUtil.addIfNotNull(afterStats, preferStatics(position, expectedTypes)); if (!afterNew) { afterStats.add(new PreferExpected(false, expectedTypes, position)); } + ContainerUtil.addIfNotNull(afterStats, preferStatics(position, expectedTypes)); } ContainerUtil.addIfNotNull(afterStats, recursion(parameters, expectedTypes)); @@ -188,7 +191,10 @@ public class JavaCompletionSorting { }; } - private static ExpectedTypeMatching getExpectedTypeMatching(LookupElement item, ExpectedTypeInfo[] expectedInfos, @Nullable String expectedMemberName) { + private static ExpectedTypeMatching getExpectedTypeMatching(LookupElement item, + ExpectedTypeInfo[] expectedInfos, + @Nullable String expectedMemberName, + @NotNull PsiElement position) { PsiType itemType = JavaCompletionUtil.getLookupElementType(item); if (itemType != null) { @@ -216,7 +222,8 @@ public class JavaCompletionSorting { if (hasNonVoid(expectedInfos)) { if (item.getObject() instanceof PsiKeyword) { String keyword = ((PsiKeyword)item.getObject()).getText(); - if (PsiKeyword.NEW.equals(keyword) || PsiKeyword.NULL.equals(keyword)) { + if (PsiKeyword.NEW.equals(keyword) && !(position.getParent() instanceof PsiMethodReferenceExpression) || + PsiKeyword.NULL.equals(keyword)) { return ExpectedTypeMatching.maybeExpected; } } @@ -527,6 +534,7 @@ public class JavaCompletionSorting { private static class PreferExpected extends LookupElementWeigher { private final boolean myConstructorPossible; private final ExpectedTypeInfo[] myExpectedTypes; + private final PsiElement myPosition; private final List myExpectedClasses = new SmartList<>(); private final String myExpectedMemberName; @@ -534,6 +542,7 @@ public class JavaCompletionSorting { super("expectedType"); myConstructorPossible = constructorPossible; myExpectedTypes = expectedTypes; + myPosition = position; for (ExpectedTypeInfo info : expectedTypes) { ContainerUtil.addIfNotNull(myExpectedClasses, PsiUtil.substituteTypeParameter(info.getDefaultType(), CommonClassNames.JAVA_LANG_CLASS, 0, false)); } @@ -570,7 +579,7 @@ public class JavaCompletionSorting { } } - return getExpectedTypeMatching(item, myExpectedTypes, myExpectedMemberName); + return getExpectedTypeMatching(item, myExpectedTypes, myExpectedMemberName, myPosition); } } diff --git a/java/java-tests/testData/codeInsight/completion/normalSorting/PreferClassLiteralWhenClassIsExpected.java b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferClassLiteralWhenClassIsExpected.java new file mode 100644 index 000000000000..72c6a52b5bf0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normalSorting/PreferClassLiteralWhenClassIsExpected.java @@ -0,0 +1,3 @@ +class C { + Class c = String. +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy index 9ad6e72a8ba9..6c3cddc3dfc2 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalCompletionOrderingTest.groovy @@ -91,6 +91,10 @@ class NormalCompletionOrderingTest extends CompletionSortingTestCase { checkPreferredItems(0, "booleanMethod", "voidMethod", "AN_OBJECT", "BOOLEAN", "class") } + void testPreferClassLiteralWhenClassIsExpected() { + checkPreferredItems(0, "class") + } + void testJComponentInstanceMembers() throws Throwable { checkPreferredItems(0, "getAccessibleContext", "getUI", "getUIClassID") }