From 8cbdbab21a92251ea430f7a075c36c2134dac8bf Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 2 Nov 2023 16:11:32 +0100 Subject: [PATCH] Java: move old check to quirks inspection GitOrigin-RevId: f1201462a504c09c1d4ca10e813c343d03be55ac --- .../messages/JavaAnalysisBundle.properties | 5 +++-- .../impl/analysis/GenericsHighlightUtil.java | 12 +--------- .../JavacQuirksInspectionVisitor.java | 22 ++++++++++++++++++- .../advHighlighting6/JavacQuirks.java | 4 ++-- ...ardReferenceInTypeParameterDefinition.java | 3 ++- .../daemon/GenericsHighlightingTest.java | 18 ++------------- 6 files changed, 31 insertions(+), 33 deletions(-) diff --git a/java/java-analysis-api/resources/messages/JavaAnalysisBundle.properties b/java/java-analysis-api/resources/messages/JavaAnalysisBundle.properties index 13a938a0c97e..aeae847cce27 100644 --- a/java/java-analysis-api/resources/messages/JavaAnalysisBundle.properties +++ b/java/java-analysis-api/resources/messages/JavaAnalysisBundle.properties @@ -251,10 +251,11 @@ inspection.common.if.parts.family=Extract common parts of 'if' statement inspection.common.if.parts.settings.highlight.when.tail.call=Highlight when the last common statement is a call inspection.common.if.parts.settings.highlight.else.if=Highlight else-if chains that can be simplified inspection.compiler.javac.quirks.anno.array.comma.fix=Remove trailing comma -inspection.compiler.javac.quirks.anno.array.comma.problem=Trailing comma in annotation array initializer may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6). +inspection.compiler.javac.quirks.anno.array.comma.problem=Trailing comma in annotation array initializer may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6) inspection.compiler.javac.quirks.name=Javac quirks inspection.compiler.javac.quirks.qualifier.type.args.fix=Remove generic parameter -inspection.compiler.javac.quirks.qualifier.type.args.problem=Generics in qualifier reference may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6). +inspection.compiler.javac.quirks.qualifier.type.args.problem=Generics in qualifier reference may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6) +inspection.compiler.javac.quirks.illegal.forward.reference=Forward reference may cause compilation error in some Javac versions (e.g. JDK 5 and JDK 6) inspection.quirk.method.reference.return.type.message=Target method return type mentions inaccessible class {0}. This will cause IllegalAccessError at runtime. # | is replaced with 'left' or 'right' inspection.constant.on.wrong.side.of.a.comparison.side.option=Constant should be on the|side of a comparison diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index 250309892bf2..d424f56f4cba 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -968,15 +968,6 @@ public final class GenericsHighlightUtil { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(typeParameter2).descriptionAndTooltip(message); } } - if (!level.isAtLeast(LanguageLevel.JDK_1_7)) { - for (PsiJavaCodeReferenceElement referenceElement : typeParameter1.getExtendsList().getReferenceElements()) { - PsiElement resolve = referenceElement.resolve(); - if (resolve instanceof PsiTypeParameter && ArrayUtilRt.find(parameters, resolve) > i) { - return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(referenceElement).descriptionAndTooltip( - JavaErrorBundle.message("illegal.forward.reference")); - } - } - } } return null; } @@ -1246,8 +1237,7 @@ public final class GenericsHighlightUtil { return null; } - static HighlightInfo.Builder checkParametersOnRaw(@NotNull PsiReferenceParameterList refParamList, - LanguageLevel languageLevel) { + static HighlightInfo.Builder checkParametersOnRaw(@NotNull PsiReferenceParameterList refParamList, LanguageLevel languageLevel) { JavaResolveResult resolveResult = null; PsiElement parent = refParamList.getParent(); PsiElement qualifier = null; diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/compiler/JavacQuirksInspectionVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInspection/compiler/JavacQuirksInspectionVisitor.java index 7d086acdb8d4..b0d8b586d300 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/compiler/JavacQuirksInspectionVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/compiler/JavacQuirksInspectionVisitor.java @@ -8,7 +8,10 @@ import com.intellij.codeInsight.daemon.impl.analysis.JavaHighlightUtil; import com.intellij.codeInsight.daemon.impl.quickfix.AddTypeArgumentsFix; import com.intellij.codeInsight.intention.QuickFixFactory; import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo; -import com.intellij.codeInspection.*; +import com.intellij.codeInspection.LocalQuickFix; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ProblemsHolder; import com.intellij.codeInspection.miscGenerics.RedundantTypeArgsInspection; import com.intellij.java.analysis.JavaAnalysisBundle; import com.intellij.modcommand.ModPsiUpdater; @@ -26,6 +29,7 @@ import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil import com.intellij.psi.infos.MethodCandidateInfo; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.*; +import com.intellij.util.ArrayUtilRt; import com.intellij.util.ObjectUtils; import com.siyeh.ig.PsiReplacementUtil; import org.jetbrains.annotations.Nls; @@ -90,6 +94,22 @@ public class JavacQuirksInspectionVisitor extends JavaElementVisitor { } } + @Override + public void visitTypeParameterList(@NotNull PsiTypeParameterList list) { + if (PsiUtil.isLanguageLevel7OrHigher(list)) return; + PsiTypeParameter[] parameters = list.getTypeParameters(); + for (int i = 0; i < parameters.length; i++) { + PsiTypeParameter typeParameter = parameters[i]; + for (PsiJavaCodeReferenceElement referenceElement : typeParameter.getExtendsList().getReferenceElements()) { + PsiElement resolve = referenceElement.resolve(); + if (resolve instanceof PsiTypeParameter && ArrayUtilRt.find(parameters, resolve) > i) { + myHolder.registerProblem(referenceElement, + JavaAnalysisBundle.message("inspection.compiler.javac.quirks.illegal.forward.reference")); + } + } + } + } + @Override public void visitTypeCastExpression(final @NotNull PsiTypeCastExpression expression) { if (PsiUtil.isLanguageLevel7OrHigher(expression)) return; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting6/JavacQuirks.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting6/JavacQuirks.java index e95529cd16c1..20a30d3bdf80 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting6/JavacQuirks.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting6/JavacQuirks.java @@ -20,7 +20,7 @@ class C { int[] value(); } - @TestAnnotation({0, 1,}) + @TestAnnotation({0, 1,}) void m() { } class A { @@ -31,7 +31,7 @@ class C { void m(Object o) { if (o instanceof A.B) { - final A.B b = (A.B)o; + final A.B b = (A.B)o; b.m(null, null); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/IllegalForwardReferenceInTypeParameterDefinition.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/IllegalForwardReferenceInTypeParameterDefinition.java index cbc96adeadc9..8d67f1f565e6 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/IllegalForwardReferenceInTypeParameterDefinition.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/IllegalForwardReferenceInTypeParameterDefinition.java @@ -1,2 +1,3 @@ -class AS, S> { +class AS, S> { + T t; } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java index b1ebb7771bc8..3bbaa2c6d214 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/GenericsHighlightingTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 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. - */ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.daemon; import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; @@ -257,7 +243,7 @@ public class GenericsHighlightingTest extends LightDaemonAnalyzerTestCase { public void testIDEA107782() { doTest5(false);} public void testInheritedWithDifferentArgsInTypeParams() { doTest5(false);} public void testInheritedWithDifferentArgsInTypeParams1() { doTest5(false);} - public void testIllegalForwardReferenceInTypeParameterDefinition() { doTest5(false);} + public void testIllegalForwardReferenceInTypeParameterDefinition() { doTest5(true);} public void testIDEA57877() { doTest5(false);} public void testIDEA110568() { doTest5(false);} public void testTypeParamsCyclicInference() { doTest5(false);}