From e3bf1c11ced5246cc36c2db280765afcf3d607dd Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 23 Jul 2021 19:00:30 +0200 Subject: [PATCH] [java] [highlighting] improved cannot inherit from raw & generics messages (IDEA-274330) GitOrigin-RevId: 711578354a7ceeec0528e9ba85d23e4f4eeda45c --- .../impl/analysis/GenericsHighlightUtil.java | 21 +++++++++++++------ .../src/messages/JavaErrorBundle.properties | 1 + ...InheritFromDifferentParameterizations.java | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) 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 63d7d4a95fe2..6eb4692df5a4 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 @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl.analysis; import com.intellij.codeInsight.daemon.JavaErrorBundle; @@ -359,8 +359,9 @@ public final class GenericsHighlightUtil { final PsiClass superClass = result.getElement(); if (superClass == null || visited.contains(superClass)) continue; PsiSubstitutor superTypeSubstitutor = result.getSubstitutor(); + PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(aClass.getProject()); //JLS 4.8 The superclasses (respectively, superinterfaces) of a raw type are the erasures of the superclasses (superinterfaces) of any of the parameterizations of the generic type. - superTypeSubstitutor = PsiUtil.isRawSubstitutor(aClass, derivedSubstitutor) ? JavaPsiFacade.getElementFactory(aClass.getProject()).createRawSubstitutor(superClass) + superTypeSubstitutor = PsiUtil.isRawSubstitutor(aClass, derivedSubstitutor) ? elementFactory.createRawSubstitutor(superClass) : MethodSignatureUtil.combineSubstitutors(superTypeSubstitutor, derivedSubstitutor); final PsiSubstitutor inheritedSubstitutor = inheritedClasses.get(superClass); @@ -371,10 +372,18 @@ public final class GenericsHighlightUtil { PsiType type2 = superTypeSubstitutor.substitute(typeParameter); if (!Comparing.equal(type1, type2)) { - String description = JavaErrorBundle.message("generics.cannot.be.inherited.with.different.type.arguments", - HighlightUtil.formatClass(superClass), - JavaHighlightUtil.formatType(type1), - JavaHighlightUtil.formatType(type2)); + String description; + if (type1 != null && type2 != null) { + description = JavaErrorBundle.message("generics.cannot.be.inherited.with.different.type.arguments", + HighlightUtil.formatClass(superClass), + JavaHighlightUtil.formatType(type1), + JavaHighlightUtil.formatType(type2)); + } + else { + description = JavaErrorBundle.message("generics.cannot.be.inherited.as.raw.and.generic", + HighlightUtil.formatClass(superClass), + JavaHighlightUtil.formatType(type1 != null ? type1 : type2)); + } return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(textRange).descriptionAndTooltip(description).create(); } } diff --git a/java/java-psi-impl/src/messages/JavaErrorBundle.properties b/java/java-psi-impl/src/messages/JavaErrorBundle.properties index f767d17d83b3..1ca581c5baed 100644 --- a/java/java-psi-impl/src/messages/JavaErrorBundle.properties +++ b/java/java-psi-impl/src/messages/JavaErrorBundle.properties @@ -50,6 +50,7 @@ generics.type.parameter.is.not.within.its.bound.implement=Type parameter ''{0}'' generics.type.or.method.does.not.have.type.parameters={0} ''{1}'' does not have type parameters generics.wrong.number.of.type.arguments=Wrong number of type arguments: {0}; required: {1} generics.cannot.be.inherited.with.different.type.arguments=''{0}'' cannot be inherited with different type arguments: ''{1}'' and ''{2}'' +generics.cannot.be.inherited.as.raw.and.generic=''{0}'' cannot be inherited as a raw type and with generic type arguments ''{1}'' generics.select.static.class.from.parameterized.type=Cannot select static class ''{0}'' from parameterized type generics.methods.have.same.erasure={0}; both methods have same erasure generics.methods.have.same.erasure.override={0}; both methods have same erasure, yet neither overrides the other diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InheritFromDifferentParameterizations.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InheritFromDifferentParameterizations.java index df81bc4ac29d..10b417dd6720 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InheritFromDifferentParameterizations.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/InheritFromDifferentParameterizations.java @@ -7,7 +7,7 @@ interface Z1 extends X1, Y1 { } interface X2 extends A {} interface Y2 extends A {} -interface Z2 extends X2, Y2 { } +interface Z2 extends X2, Y2 { } interface X3 extends A {} interface Y3 extends A {}