diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/BasicExpressionCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/BasicExpressionCompletionContributor.java index 19fbe8b95441..bb80a1b4a0c5 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/BasicExpressionCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/BasicExpressionCompletionContributor.java @@ -21,7 +21,6 @@ import com.intellij.codeInsight.template.SmartCompletionContextType; import com.intellij.codeInsight.template.impl.TemplateImpl; import com.intellij.codeInsight.template.impl.TemplateSettings; import com.intellij.patterns.ElementPattern; -import com.intellij.patterns.PsiJavaPatterns; import com.intellij.psi.*; import com.intellij.psi.filters.getters.ClassLiteralGetter; import com.intellij.psi.filters.getters.JavaMembersGetter; @@ -36,7 +35,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Map; -import static com.intellij.patterns.PsiJavaPatterns.psiClass; import static com.intellij.patterns.PsiJavaPatterns.psiElement; /** @@ -63,10 +61,7 @@ public class BasicExpressionCompletionContributor { final Consumer result, PrefixMatcher matcher) { final PsiElement element = parameters.getPosition(); - if (PsiJavaPatterns.psiElement().afterLeaf( - PsiJavaPatterns.psiElement().withText(".").afterLeaf( - PsiJavaPatterns.psiElement().withParent( - PsiJavaPatterns.psiElement().referencing(psiClass())))).accepts(element)) { + if (JavaCompletionData.isAfterTypeDot(element)) { addKeyword(result, element, PsiKeyword.CLASS); addKeyword(result, element, PsiKeyword.THIS); diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java index c85a9e5e0582..2c99dcb62d5d 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java @@ -35,7 +35,6 @@ import com.intellij.psi.filters.getters.JavaMembersGetter; import com.intellij.psi.filters.position.*; import com.intellij.psi.filters.types.TypeCodeFragmentIsVoidEnabledFilter; import com.intellij.psi.impl.source.jsp.jspJava.JspClassLevelDeclarationStatement; -import com.intellij.psi.impl.source.tree.ElementType; import com.intellij.psi.jsp.JspElementType; import com.intellij.psi.templateLanguages.OuterLanguageElement; import com.intellij.psi.util.PsiTreeUtil; @@ -530,7 +529,9 @@ public class JavaCompletionData extends JavaAwareCompletionData{ addPrimitiveTypes(result, position); - addClassLiteral(result, position); + if (isAfterTypeDot(position)) { + result.addElement(createKeyword(position, PsiKeyword.CLASS)); + } final ProcessingContext context = new ProcessingContext(); if (psiElement().afterLeaf( @@ -564,33 +565,19 @@ public class JavaCompletionData extends JavaAwareCompletionData{ } static boolean isAfterPrimitiveOrArrayType(PsiElement element) { - element = PsiTreeUtil.prevVisibleLeaf(element); - if (element == null || !element.textMatches(".")) return false; - - boolean array = false; - while (true) { - element = PsiTreeUtil.prevVisibleLeaf(element); - if (element == null) return false; - if (element.textMatches("]")) { - array = true; - element = PsiTreeUtil.prevVisibleLeaf(element); - if (element == null || !element.textMatches("[")) return false; - } else { - break; - } - } - return psiElement().withElementType(ElementType.PRIMITIVE_TYPE_BIT_SET).accepts(element) || array && CLASS_REFERENCE.accepts(element); + return psiElement().withParent( + psiReferenceExpression().withFirstChild( + psiElement(PsiClassObjectAccessExpression.class).withLastChild( + not(psiElement().withText(PsiKeyword.CLASS))))).accepts(element); } - private static void addClassLiteral(CompletionResultSet result, PsiElement position) { + static boolean isAfterTypeDot(PsiElement position) { if (INSIDE_PARAMETER_LIST.accepts(position) || position.getContainingFile() instanceof PsiJavaCodeReferenceCodeFragment) { - return; + return false; } - if (psiElement().afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE)).accepts(position) || - isAfterPrimitiveOrArrayType(position)) { - result.addElement(createKeyword(position, PsiKeyword.CLASS)); - } + return psiElement().afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE)).accepts(position) || + isAfterPrimitiveOrArrayType(position); } private static void addPrimitiveTypes(CompletionResultSet result, PsiElement position) { diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/ReferenceExpressionCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/ReferenceExpressionCompletionContributor.java index 7462719fc8cb..ff2c5ed27f3a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/ReferenceExpressionCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/ReferenceExpressionCompletionContributor.java @@ -109,6 +109,7 @@ public class ReferenceExpressionCompletionContributor { public void run() { final PsiElement element = parameters.getPosition(); if (JavaSmartCompletionContributor.INSIDE_TYPECAST_EXPRESSION.accepts(element)) return; + if (JavaCompletionData.isAfterPrimitiveOrArrayType(element)) return; final int offset = parameters.getParameters().getOffset(); final PsiReference reference = element.getContainingFile().findReferenceAt(offset); diff --git a/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod-out.java b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod-out.java new file mode 100644 index 000000000000..693cebd5b4bf --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod-out.java @@ -0,0 +1,5 @@ +class Bar { + public static void main(String[] args) { + Class cls = byte[].class; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod.java b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod.java new file mode 100644 index 000000000000..eefcb73f5922 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveArrayClassInMethod.java @@ -0,0 +1,5 @@ +class Bar { + public static void main(String[] args) { + Class cls = byte[]. + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno-out.java b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno-out.java new file mode 100644 index 000000000000..b3b43cefe9c5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno-out.java @@ -0,0 +1,10 @@ +class Bar { + @interface B { + Class value(); + } + + @B(byte[].class) + public static void main(String[] args) { + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno.java b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno.java new file mode 100644 index 000000000000..fb5791fc7e21 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/PrimitiveClassInAnno.java @@ -0,0 +1,10 @@ +class Bar { + @interface B { + Class value(); + } + + @B(byte[].) + public static void main(String[] args) { + } + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java index 40e581afb46f..cc42507fbda3 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java @@ -951,6 +951,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase { public void testStaticallyImportedField() throws Throwable { doTest(); } public void testSiblingOfAStaticallyImportedField() throws Throwable { doTest(); } + public void testPrimitiveArrayClassInMethod() throws Throwable { doTest(); } + public void testPrimitiveClassInAnno() throws Throwable { doTest(); } public void testInferFromCall() throws Throwable { doTest();