From 3e8ef273d861ea3aa6003e61cab346f6abca89f6 Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 7 May 2013 14:30:24 +0200 Subject: [PATCH] select from type parameter error processing (IDEA-62453) --- .../impl/analysis/HighlightVisitorImpl.java | 17 ++++++++++++++++ .../SelectFromTypeParameter.java | 16 +++++++++++++++ .../TypeArgumentsGivenOnRawType.java | 20 ++++++++++++++++++- .../daemon/GenericsHighlightingTest.java | 3 ++- 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/SelectFromTypeParameter.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index e4bcfd86bac1..952cb600380c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -29,6 +29,8 @@ import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.IndexNotReadyException; import com.intellij.openapi.project.Project; +import com.intellij.openapi.projectRoots.JavaSdkVersion; +import com.intellij.openapi.projectRoots.JavaVersionService; import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; @@ -906,6 +908,21 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh myRefCountHolder.registerReference(ref, result); } myHolder.add(HighlightUtil.checkReference(ref, result)); + if (!myHolder.hasErrorResults() && resolved instanceof PsiTypeParameter) { + boolean cannotSelectFromTypeParameter = !JavaVersionService.getInstance().isAtLeast(ref, JavaSdkVersion.JDK_1_7); + if (!cannotSelectFromTypeParameter) { + final PsiClass containingClass = PsiTreeUtil.getParentOfType(ref, PsiClass.class); + if (containingClass != null) { + if (PsiTreeUtil.isAncestor(containingClass.getExtendsList(), ref, false) || + PsiTreeUtil.isAncestor(containingClass.getImplementsList(), ref, false)) { + cannotSelectFromTypeParameter = true; + } + } + } + if (cannotSelectFromTypeParameter) { + myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).descriptionAndTooltip("Cannot select from a type parameter").range(ref).create()); + } + } } if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkAbstractInstantiation(ref, resolved)); if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkExtendsDuplicate(ref, resolved)); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/SelectFromTypeParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/SelectFromTypeParameter.java new file mode 100644 index 000000000000..dc8f66f9f3d5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/SelectFromTypeParameter.java @@ -0,0 +1,16 @@ +import java.util.List; +interface Builder { + T build(); +} + +interface Test, X> { + static interface TestBuilder, X> extends Builder {} +} + +interface Algorithm> {} + +class SelectFromVariableType> + implements AlgorithmT.TestBuilder> { + List<T.TestBuilder> b; + T.TestBuilder b1; +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/TypeArgumentsGivenOnRawType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/TypeArgumentsGivenOnRawType.java index 147fd862fe77..09ff35bad8d4 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/TypeArgumentsGivenOnRawType.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/TypeArgumentsGivenOnRawType.java @@ -1,3 +1,4 @@ +import java.util.List; class A { abstract class C { void foo(T.C x) { @@ -28,4 +29,21 @@ class A1 { abstract S bar(); } -} \ No newline at end of file +} + +interface Builder { + T build(); +} + +interface Test, X> { + static interface TestBuilder, X> extends Builder {} +} + +interface Algorithm> {} + +class SelectFromVariableType> + implements AlgorithmT.TestBuilder> { + + List> b; + T.TestBuilder b1; +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/GenericsHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/GenericsHighlightingTest.java index 6e60b1178332..b53f1a45978d 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/GenericsHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/GenericsHighlightingTest.java @@ -177,7 +177,8 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase { public void testContinueInferenceAfterFirstRawResult() { doTest5(false); } public void testDoNotAcceptLowerBoundIfRaw() { doTest5(false); } public void testStaticOverride() { doTest5(false); } - public void testTypeArgumentsGivenOnRawType() { doTest5(false); } + public void testTypeArgumentsGivenOnRawType() { doTest7Incompatibility(false); } + public void testSelectFromTypeParameter() { doTest5(false); } public void testTypeArgumentsGivenOnAnonymousClassCreation() { doTest5(false); } //public void testIDEA94011() { doTest5(false); } public void testDifferentTypeParamsInOverloadedMethods() { doTest5(true); }