From 6c93e44f46d3005481b6286b0cd97a1362bc7b1f Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 17 May 2011 18:34:41 +0400 Subject: [PATCH] [ann] Exceptions with interfaces in multi-catch: completion fix --- .../psi/scope/util/PsiScopesUtil.java | 14 +- .../completion/dot/MultiCatch.java | 33 +++ .../completion/DotCompletionTest.java | 110 ++++++++++ .../codeInsight/completion/DotTest.java | 197 ------------------ .../completion/LightCompletionTestCase.java | 43 ++++ 5 files changed, 197 insertions(+), 200 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/dot/MultiCatch.java create mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/completion/DotCompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/codeInsight/completion/DotTest.java diff --git a/java/java-impl/src/com/intellij/psi/scope/util/PsiScopesUtil.java b/java/java-impl/src/com/intellij/psi/scope/util/PsiScopesUtil.java index 284478d52f35..4d3ebca740ef 100644 --- a/java/java-impl/src/com/intellij/psi/scope/util/PsiScopesUtil.java +++ b/java/java-impl/src/com/intellij/psi/scope/util/PsiScopesUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -117,11 +117,19 @@ public class PsiScopesUtil { substitutor = substitutor.put(arrayTypeParameters[0], ((PsiArrayType)type).getComponentType()); } arrayClass.processDeclarations(processor, ResolveState.initial().put(PsiSubstitutor.KEY, substitutor), arrayClass, place); - } else if (type instanceof PsiIntersectionType) { + } + else if (type instanceof PsiIntersectionType) { for (PsiType psiType : ((PsiIntersectionType)type).getConjuncts()) { processTypeDeclarations(psiType, place, processor); } - } else { + } + else if (type instanceof PsiDisjunctionType) { + final PsiType lub = ((PsiDisjunctionType)type).getLeastUpperBound(); + if (lub != null) { + processTypeDeclarations(lub, place, processor); + } + } + else { final JavaResolveResult result = PsiUtil.resolveGenericsClassInType(type); final PsiClass clazz = (PsiClass)result.getElement(); if (clazz != null) { diff --git a/java/java-tests/testData/codeInsight/completion/dot/MultiCatch.java b/java/java-tests/testData/codeInsight/completion/dot/MultiCatch.java new file mode 100644 index 000000000000..eb6983a3c135 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/dot/MultiCatch.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class C { + static class E extends Exception { } + interface I { void i(); } + static class E1 extends E implements I { public void i() { } } + static class E2 extends E implements I { public void i() { } } + + void m(boolean f) { + try { + if (f) + throw new E1(); + else + throw new E2(); + } + catch (E1 | E2 e) { + e. + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotCompletionTest.java new file mode 100644 index 000000000000..8ed25b3d38ff --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotCompletionTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.completion; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.CodeInsightSettings; + +/** + * @author ik + * Date: 21.01.2003 + */ +public class DotCompletionTest extends LightCompletionTestCase { + @Override + protected String getTestDataPath() { + return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/completion/dot/"; + } + + public void testInstance() throws Exception { + configureByFile("Dot1.java"); + assertEquals("", myPrefix); + assertContainsItems("a", "foo"); + } + + public void testClass() throws Exception { + configureByFile("Dot2.java"); + assertEquals("", myPrefix); + assertContainsItems("a", "foo"); + } + + public void testAnonymous() throws Exception { + configureByFile("Dot3.java"); + assertEquals("", myPrefix); + assertContainsItems("a", "foo"); + } + + public void testShowStatic() throws Exception { + CodeInsightSettings settings = CodeInsightSettings.getInstance(); + boolean oldSetting = settings.SHOW_STATIC_AFTER_INSTANCE; + settings.SHOW_STATIC_AFTER_INSTANCE = false; + try { + configureByFile("Dot4.java"); + assertEquals("", myPrefix); + assertContainsItems("foo"); + assertNotContainItems("a"); + } + finally { + settings.SHOW_STATIC_AFTER_INSTANCE = oldSetting; + } + } + + public void testImports() throws Exception { + configureByFile("Dot5.java"); + assertContainsItems("util", "lang"); + } + + public void testArrayElement() throws Exception { + configureByFile("Dot6.java"); + assertContainsItems("toString", "substring"); + } + + public void testArray() throws Exception { + configureByFile("Dot7.java"); + assertContainsItems("clone", "length"); + } + + public void testDuplicatesFromInheritance() throws Exception { + configureByFile("Dot8.java"); + assertContainsItems("toString"); + } + + public void testConstructorExclusion() throws Exception { + configureByFile("Dot9.java"); + assertContainsItems("foo"); + assertNotContainItems("A"); + } + + public void testPrimitiveArray() throws Exception { + configureByFile("Dot10.java"); + assertContainsItems("clone", "length"); + } + + public void testThisExpression() throws Exception { + configureByFile("Dot11.java"); + assertContainsItems("foo", "foo1"); + } + + public void testSuperExpression() throws Exception { + configureByFile("Dot12.java"); + assertContainsItems("foo"); + assertNotContainItems("foo1"); + } + + public void testMultiCatch() throws Exception { + configureByFile("MultiCatch.java"); + assertContainsItems("i", "addSuppressed", "getMessage", "printStackTrace"); + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotTest.java deleted file mode 100644 index b4d1ea68ddf8..000000000000 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/DotTest.java +++ /dev/null @@ -1,197 +0,0 @@ -package com.intellij.codeInsight.completion; - -import com.intellij.JavaTestUtil; -import com.intellij.codeInsight.CodeInsightSettings; -import com.intellij.codeInsight.lookup.LookupElement; - -/** - * Created by IntelliJ IDEA. - * User: ik - * Date: 21.01.2003 - * Time: 16:31:32 - * To change this template use Options | File Templates. - */ -public class DotTest extends LightCompletionTestCase { - - @Override - protected String getTestDataPath() { - return JavaTestUtil.getJavaTestDataPath(); - } - - public void testInstance() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot1.java"); - - assertEquals("", myPrefix); - assertNotNull(myItems); - int index = 0; - for (final LookupElement myItem : myItems) { - if ("a".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(2, index); - } - - public void testClass() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot2.java"); - - assertEquals("", myPrefix); - assertNotNull(myItems); - int index = 0; - for (final LookupElement myItem : myItems) { - if ("a".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(2, index); - } - - public void testAnonymous() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot3.java"); - - assertEquals("", myPrefix); - assertNotNull(myItems); - int index = 0; - for (final LookupElement myItem : myItems) { - if ("a".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(2, index); - } - - public void testShowStatic() throws Exception { - CodeInsightSettings settings = CodeInsightSettings.getInstance(); - boolean oldSetting = settings.SHOW_STATIC_AFTER_INSTANCE; - settings.SHOW_STATIC_AFTER_INSTANCE = false; - configureByFile("/codeInsight/completion/dot/Dot4.java"); - settings.SHOW_STATIC_AFTER_INSTANCE = oldSetting; - assertEquals("", myPrefix); - assertNotNull(myItems); - int index = 0; - for (final LookupElement myItem : myItems) { - if ("a".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(1, index); - } - - public void testImports() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot5.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("util".equals(myItem.getLookupString()) || "lang".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(2, index); - } - - public void testArrayElement() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot6.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("toString".equals(myItem.getLookupString()) || "substring".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(3, index); - } - - public void testArray() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot7.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("length".equals(myItem.getLookupString()) || "clone".equals(myItem.getLookupString())) { - index++; - } - - } - assertEquals(2, index); - } - - public void testDuplicatesFromInherance() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot8.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("toString".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(1, index); - } - - public void testConstructorExclusion() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot9.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("A".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(0, index); - } - - public void testPrimitiveArray() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot10.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("clone".equals(myItem.getLookupString()) || "length".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(2, index); - } - - public void testThisExpression() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot11.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("foo1".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(2, index); - } - - public void testSuperExpression() throws Exception { - configureByFile("/codeInsight/completion/dot/Dot12.java"); - - assertNotNull(myItems); - int index = 0; - - for (final LookupElement myItem : myItems) { - if ("foo1".equals(myItem.getLookupString()) || "foo".equals(myItem.getLookupString())) { - index++; - } - } - assertEquals(1, index); - } -} diff --git a/java/testFramework/src/com/intellij/codeInsight/completion/LightCompletionTestCase.java b/java/testFramework/src/com/intellij/codeInsight/completion/LightCompletionTestCase.java index 9ddb469b3b97..2ea0e76a0a81 100644 --- a/java/testFramework/src/com/intellij/codeInsight/completion/LightCompletionTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/completion/LightCompletionTestCase.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.codeInsight.completion; import com.intellij.codeInsight.lookup.LookupElement; @@ -6,9 +21,11 @@ import com.intellij.codeInsight.lookup.impl.LookupImpl; import com.intellij.testFramework.LightCodeInsightTestCase; import com.intellij.psi.statistics.impl.StatisticsManagerImpl; import com.intellij.psi.statistics.StatisticsManager; +import com.intellij.util.containers.HashSet; import org.jetbrains.annotations.NonNls; import java.util.Arrays; +import java.util.Set; /** * @author mike @@ -99,6 +116,32 @@ public abstract class LightCompletionTestCase extends LightCodeInsightTestCase { } } + protected void assertContainsItems(final String... expected) { + final Set actual = getLookupStrings(); + for (String s : expected) { + assertTrue("Expected '" + s + "' not found in " + actual, + actual.contains(s)); + } + } + + protected void assertNotContainItems(final String... unexpected) { + final Set actual = getLookupStrings(); + for (String s : unexpected) { + assertFalse("Unexpected '" + s + "' presented in " + actual, + actual.contains(s)); + } + } + + private Set getLookupStrings() { + final Set actual = new HashSet(); + if (myItems != null) { + for (LookupElement lookupElement : myItems) { + actual.add(lookupElement.getLookupString()); + } + } + return actual; + } + protected static LookupImpl getLookup() { return (LookupImpl)LookupManager.getActiveLookup(myEditor); }