diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index ae1102291d..445a682d23 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -52,7 +52,6 @@ import consulo.util.lang.Comparing; import consulo.util.lang.Pair; import consulo.virtualFileSystem.VirtualFile; - import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; @@ -470,19 +469,25 @@ public static Collection checkOverrideEquivalentMethods(@Nonnull Set foundProblems = Sets.newHashSet(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY); for (HierarchicalMethodSignature signature : signaturesWithSupers) { - HighlightInfo info = checkSameErasureNotSubSignatureInner(signature, manager, aClass, sameErasureMethods); - if (info != null && foundProblems.add(signature)) { - result.add(info); + HighlightInfo.Builder hlBuilder = checkSameErasureNotSubSignatureInner(signature, manager, aClass, sameErasureMethods); + if (hlBuilder != null && foundProblems.add(signature)) { + HighlightInfo hlInfo = hlBuilder.create(); + if (hlInfo != null) { + result.add(hlInfo); + } } if (aClass instanceof PsiTypeParameter) { - info = HighlightMethodUtil.checkMethodIncompatibleReturnType( + hlBuilder = HighlightMethodUtil.checkMethodIncompatibleReturnType( signature, signature.getSuperSignatures(), true, HighlightNamesUtil.getClassDeclarationTextRange(aClass) ); - if (info != null) { - result.add(info); + if (hlBuilder != null) { + HighlightInfo hlInfo = hlBuilder.create(); + if (hlInfo != null) { + result.add(hlInfo); + } } } } @@ -514,8 +519,9 @@ public static HighlightInfo checkDefaultMethodOverrideEquivalentToObjectNonPriva return null; } + @Nullable @RequiredReadAction - public static HighlightInfo checkUnrelatedDefaultMethods(@Nonnull PsiClass aClass, @Nonnull PsiIdentifier classIdentifier) { + public static HighlightInfo.Builder checkUnrelatedDefaultMethods(@Nonnull PsiClass aClass, @Nonnull PsiIdentifier classIdentifier) { Map> overrideEquivalent = PsiSuperMethodUtil.collectOverrideEquivalents(aClass); boolean isInterface = aClass.isInterface(); @@ -568,8 +574,7 @@ public static HighlightInfo checkUnrelatedDefaultMethods(@Nonnull PsiClass aClas PsiSubstitutor.EMPTY )), defaultMethod.getSignature(PsiSubstitutor.EMPTY) - ) - ) { + )) { continue; } String c1 = HighlightUtil.formatClass(aClass, false); @@ -581,8 +586,7 @@ public static HighlightInfo checkUnrelatedDefaultMethods(@Nonnull PsiClass aClas return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(classIdentifier) .descriptionAndTooltip(message) - .registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(aClass)) - .create(); + .registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(aClass)); } if (isInterface || abstracts == null || unrelatedMethodContainingClass.isInterface()) { List defaultContainingClasses = ContainerUtil.mapNotNull(defaults, PsiMethod::getContainingClass); @@ -606,8 +610,7 @@ public static HighlightInfo checkUnrelatedDefaultMethods(@Nonnull PsiClass aClas return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(classIdentifier) .descriptionAndTooltip(description) - .registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(aClass)) - .create(); + .registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(aClass)); } } } @@ -618,10 +621,8 @@ private static boolean belongToOneHierarchy( @Nonnull PsiClass defaultMethodContainingClass, @Nonnull PsiClass unrelatedMethodContainingClass ) { - return defaultMethodContainingClass.isInheritor(unrelatedMethodContainingClass, true) || unrelatedMethodContainingClass.isInheritor( - defaultMethodContainingClass, - true - ); + return defaultMethodContainingClass.isInheritor(unrelatedMethodContainingClass, true) + || unrelatedMethodContainingClass.isInheritor(defaultMethodContainingClass, true); } private static boolean hasNotOverriddenAbstract( @@ -648,8 +649,9 @@ private static String hasUnrelatedDefaults(List defaults) { return null; } + @Nullable @RequiredReadAction - public static HighlightInfo checkUnrelatedConcrete(@Nonnull PsiClass psiClass, @Nonnull PsiIdentifier classIdentifier) { + public static HighlightInfo.Builder checkUnrelatedConcrete(@Nonnull PsiClass psiClass, @Nonnull PsiIdentifier classIdentifier) { PsiClass superClass = psiClass.getSuperClass(); if (superClass != null && superClass.hasTypeParameters()) { Collection visibleSignatures = superClass.getVisibleSignatures(); @@ -681,18 +683,15 @@ public static HighlightInfo checkUnrelatedConcrete(@Nonnull PsiClass psiClass, @ && !foundMethod.isAbstract() && !foundMethod.hasModifierProperty(PsiModifier.DEFAULT) && (foundMethodContainingClass = foundMethod.getContainingClass()) != null) { - String description = "Methods " + - JavaHighlightUtil.formatMethod(foundMethod) + " from " + HighlightUtil.formatClass(foundMethodContainingClass) + - " and " + - JavaHighlightUtil.formatMethod(method) + " from " + HighlightUtil.formatClass(containingClass) + - " are inherited with the same signature"; - - HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + //TODO: override fix + return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(classIdentifier) - .descriptionAndTooltip(description) - .create(); - //todo override fix - return info; + .descriptionAndTooltip(JavaErrorLocalize.classInheritanceMethodClash( + JavaHighlightUtil.formatMethod(foundMethod), + HighlightUtil.formatClass(foundMethodContainingClass), + JavaHighlightUtil.formatMethod(method), + HighlightUtil.formatClass(containingClass) + )); } overrideEquivalent.put(signature, method); } @@ -702,7 +701,7 @@ public static HighlightInfo checkUnrelatedConcrete(@Nonnull PsiClass psiClass, @ @Nullable @RequiredReadAction - private static HighlightInfo checkSameErasureNotSubSignatureInner( + private static HighlightInfo.Builder checkSameErasureNotSubSignatureInner( @Nonnull HierarchicalMethodSignature signature, @Nonnull PsiManager manager, @Nonnull PsiClass aClass, @@ -715,7 +714,7 @@ private static HighlightInfo checkSameErasureNotSubSignatureInner( } MethodSignature signatureToErase = method.getSignature(PsiSubstitutor.EMPTY); MethodSignatureBackedByPsiMethod sameErasure = sameErasureMethods.get(signatureToErase); - HighlightInfo info; + HighlightInfo.Builder hlBuilder; if (sameErasure != null) { if (aClass instanceof PsiTypeParameter || MethodSignatureUtil.findMethodBySuperMethod(aClass, sameErasure.getMethod(), false) != null @@ -725,9 +724,9 @@ private static HighlightInfo checkSameErasureNotSubSignatureInner( true ) || InheritanceUtil.isInheritorOrSelf(method.getContainingClass(), sameErasure.getMethod().getContainingClass(), true))) { - info = checkSameErasureNotSubSignatureOrSameClass(sameErasure, signature, aClass, method); - if (info != null) { - return info; + hlBuilder = checkSameErasureNotSubSignatureOrSameClass(sameErasure, signature, aClass, method); + if (hlBuilder != null) { + return hlBuilder; } } } @@ -736,9 +735,9 @@ private static HighlightInfo checkSameErasureNotSubSignatureInner( } List supers = signature.getSuperSignatures(); for (HierarchicalMethodSignature superSignature : supers) { - info = checkSameErasureNotSubSignatureInner(superSignature, manager, aClass, sameErasureMethods); - if (info != null) { - return info; + hlBuilder = checkSameErasureNotSubSignatureInner(superSignature, manager, aClass, sameErasureMethods); + if (hlBuilder != null) { + return hlBuilder; } if (superSignature.isRaw() && !signature.isRaw()) { @@ -762,7 +761,7 @@ private static HighlightInfo checkSameErasureNotSubSignatureInner( @Nullable @RequiredReadAction - private static HighlightInfo checkSameErasureNotSubSignatureOrSameClass( + private static HighlightInfo.Builder checkSameErasureNotSubSignatureOrSameClass( MethodSignatureBackedByPsiMethod signatureToCheck, HierarchicalMethodSignature superSignature, PsiClass aClass, @@ -817,7 +816,7 @@ else if (superMethod.isConstructor()) { PsiType[] erasedTypes = signatureToCheck.getErasedParameterTypes(); boolean erasure = erasedTypes.length > 0; for (PsiType type : superSignature.getParameterTypes()) { - erasure &= Comparing.equal(type, erasedTypes[idx]); + erasure &= Objects.equals(type, erasedTypes[idx]); idx++; } @@ -847,7 +846,8 @@ else if (superMethod.isConstructor()) { } } - private static HighlightInfo getSameErasureMessage( + @Nonnull + private static HighlightInfo.Builder getSameErasureMessage( boolean sameClass, @Nonnull PsiMethod method, @Nonnull PsiMethod superMethod, @@ -861,8 +861,7 @@ private static HighlightInfo getSameErasureMessage( : JavaErrorLocalize.genericsMethodsHaveSameErasureOverride(clashMethodMessage); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(textRange) - .descriptionAndTooltip(description) - .create(); + .descriptionAndTooltip(description); } @RequiredReadAction @@ -1099,7 +1098,7 @@ public static boolean isEnumSyntheticMethod(MethodSignature methodSignature, Pro @Nullable @RequiredReadAction - public static HighlightInfo checkTypeParametersList( + public static HighlightInfo.Builder checkTypeParametersList( PsiTypeParameterList list, PsiTypeParameter[] parameters, @Nonnull LanguageLevel level @@ -1108,25 +1107,22 @@ public static HighlightInfo checkTypeParametersList( if (parent instanceof PsiClass psiClass && psiClass.isEnum()) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(list) - .descriptionAndTooltip(JavaErrorLocalize.genericsEnumMayNotHaveTypeParameters()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.genericsEnumMayNotHaveTypeParameters()); } if (PsiUtil.isAnnotationMethod(parent)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(list) - .descriptionAndTooltip(JavaErrorLocalize.genericsAnnotationMembersMayNotHaveTypeParameters()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.genericsAnnotationMembersMayNotHaveTypeParameters()); } if (parent instanceof PsiClass psiClass && psiClass.isAnnotationType()) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(list) - .descriptionAndTooltip(JavaErrorLocalize.annotationMayNotHaveTypeParameters()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.annotationMayNotHaveTypeParameters()); } for (int i = 0; i < parameters.length; i++) { PsiTypeParameter typeParameter1 = parameters[i]; - HighlightInfo cyclicInheritance = HighlightClassUtil.checkCyclicInheritance(typeParameter1); + HighlightInfo.Builder cyclicInheritance = HighlightClassUtil.checkCyclicInheritance(typeParameter1); if (cyclicInheritance != null) { return cyclicInheritance; } @@ -1137,8 +1133,7 @@ public static HighlightInfo checkTypeParametersList( if (Comparing.strEqual(name1, name2)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(typeParameter2) - .descriptionAndTooltip(JavaErrorLocalize.genericsDuplicateTypeParameter(name1)) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.genericsDuplicateTypeParameter(name1)); } } if (!level.isAtLeast(LanguageLevel.JDK_1_7)) { @@ -1146,8 +1141,7 @@ public static HighlightInfo checkTypeParametersList( if (referenceElement.resolve() instanceof PsiTypeParameter typeParam && ArrayUtil.find(parameters, typeParam) > i) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(referenceElement.getTextRange()) - .descriptionAndTooltip(JavaErrorLocalize.illegalForwardReference()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.illegalForwardReference()); } } } @@ -1177,8 +1171,9 @@ public static Collection checkCatchParameterIsClass(PsiParameter return result; } + @Nullable @RequiredReadAction - public static HighlightInfo checkInstanceOfGenericType(PsiInstanceOfExpression expression) { + public static HighlightInfo.Builder checkInstanceOfGenericType(PsiInstanceOfExpression expression) { PsiTypeElement checkTypeElement = expression.getCheckType(); if (checkTypeElement == null) { return null; @@ -1190,20 +1185,19 @@ public static HighlightInfo checkInstanceOfGenericType(PsiInstanceOfExpression e * 15.20.2 Type Comparison Operator instanceof * ReferenceType mentioned after the instanceof operator is reifiable */ + @Nullable @RequiredReadAction - private static HighlightInfo isIllegalForInstanceOf(PsiType type, PsiTypeElement typeElement) { + private static HighlightInfo.Builder isIllegalForInstanceOf(PsiType type, PsiTypeElement typeElement) { if (PsiUtil.resolveClassInClassTypeOnly(type) instanceof PsiTypeParameter) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(typeElement) - .descriptionAndTooltip(JavaErrorLocalize.genericsCannotInstanceofTypeParameters()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.genericsCannotInstanceofTypeParameters()); } if (!JavaGenericsUtil.isReifiableType(type)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(typeElement) - .descriptionAndTooltip(JavaErrorLocalize.illegalGenericTypeForInstanceof()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.illegalGenericTypeForInstanceof()); } return null; diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightClassUtil.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightClassUtil.java index 3d893d11c0..bcfe67fb65 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightClassUtil.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightClassUtil.java @@ -213,7 +213,7 @@ public static HighlightInfo checkDuplicateTopLevelClass(PsiClass aClass) { @Nullable @RequiredReadAction - public static HighlightInfo checkDuplicateNestedClass(PsiClass aClass) { + public static HighlightInfo.Builder checkDuplicateNestedClass(PsiClass aClass) { if (aClass == null) { return null; } @@ -261,8 +261,7 @@ public static HighlightInfo checkDuplicateNestedClass(PsiClass aClass) { TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(textRange) - .descriptionAndTooltip(JavaErrorLocalize.duplicateClass(name)) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.duplicateClass(name)); } return null; } @@ -329,7 +328,7 @@ public static HighlightInfo.Builder checkClassAndPackageConflict(@Nonnull PsiCla @Nullable @RequiredReadAction - private static HighlightInfo checkStaticFieldDeclarationInInnerClass(@Nonnull PsiKeyword keyword) { + private static HighlightInfo.Builder checkStaticFieldDeclarationInInnerClass(@Nonnull PsiKeyword keyword) { if (getEnclosingStaticClass(keyword, PsiField.class) == null) { return null; } @@ -340,22 +339,22 @@ private static HighlightInfo checkStaticFieldDeclarationInInnerClass(@Nonnull Ps } QuickFixFactory factory = QuickFixFactory.getInstance(); - HighlightInfo.Builder result = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + HighlightInfo.Builder hlBuilder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(keyword) .descriptionAndTooltip(JavaErrorLocalize.staticDeclarationInInnerClass()) .registerFix(factory.createModifierFixBuilder(field).remove(PsiModifier.STATIC).create()); PsiClass aClass = field.getContainingClass(); if (aClass != null) { - result.registerFix(factory.createModifierFixBuilder(aClass).add(PsiModifier.STATIC).create()); + hlBuilder.registerFix(factory.createModifierFixBuilder(aClass).add(PsiModifier.STATIC).create()); } - return result.create(); + return hlBuilder; } @Nullable @RequiredReadAction - private static HighlightInfo checkStaticMethodDeclarationInInnerClass(PsiKeyword keyword, LanguageLevel languageLevel) { + private static HighlightInfo.Builder checkStaticMethodDeclarationInInnerClass(PsiKeyword keyword, LanguageLevel languageLevel) { if (languageLevel.isAtLeast(LanguageLevel.JDK_16)) { return null; } @@ -372,13 +371,12 @@ private static HighlightInfo checkStaticMethodDeclarationInInnerClass(PsiKeyword .range(keyword) .descriptionAndTooltip(JavaErrorLocalize.staticDeclarationInInnerClass()) .registerFix(factory.createModifierFixBuilder(method).remove(PsiModifier.STATIC).create()) - .registerFix(factory.createModifierFixBuilder((PsiClass)method.getParent()).add(PsiModifier.STATIC).create()) - .create(); + .registerFix(factory.createModifierFixBuilder((PsiClass)method.getParent()).add(PsiModifier.STATIC).create()); } @Nullable @RequiredReadAction - private static HighlightInfo checkStaticInitializerDeclarationInInnerClass(PsiKeyword keyword) { + private static HighlightInfo.Builder checkStaticInitializerDeclarationInInnerClass(PsiKeyword keyword) { if (getEnclosingStaticClass(keyword, PsiClassInitializer.class) == null) { return null; } @@ -386,14 +384,13 @@ private static HighlightInfo checkStaticInitializerDeclarationInInnerClass(PsiKe if (PsiUtilCore.hasErrorElementChild(initializer)) { return null; } - PsiClass owner = (PsiClass)keyword.getParent().getParent().getParent(); + PsiClass owner = (PsiClass)initializer.getParent(); QuickFixFactory factory = QuickFixFactory.getInstance(); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(keyword) .descriptionAndTooltip(JavaErrorLocalize.staticDeclarationInInnerClass()) .registerFix(factory.createModifierFixBuilder(initializer).remove(PsiModifier.STATIC).create()) - .registerFix(factory.createModifierFixBuilder(owner).add(PsiModifier.STATIC).create()) - .create(); + .registerFix(factory.createModifierFixBuilder(owner).add(PsiModifier.STATIC).create()); } private static PsiElement getEnclosingStaticClass(@Nonnull PsiKeyword keyword, @Nonnull Class parentClass) { @@ -402,20 +399,20 @@ private static PsiElement getEnclosingStaticClass(@Nonnull PsiKeyword keyword, @ .parent(PsiMatchers.hasClass(PsiModifierList.class)) .parent(PsiMatchers.hasClass(parentClass)) .parent(PsiMatchers.hasClass(PsiClass.class)) - .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, false)) + .dot(JavaMatchers.hasNoModifier(PsiModifier.STATIC)) .parent(PsiMatchers.hasClass(PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class)) .getElement(); } @Nullable @RequiredReadAction - private static HighlightInfo checkStaticClassDeclarationInInnerClass(PsiKeyword keyword) { + private static HighlightInfo.Builder checkStaticClassDeclarationInInnerClass(PsiKeyword keyword) { // keyword points to 'class' or 'interface' or 'enum' if (new PsiMatcherImpl(keyword) .parent(PsiMatchers.hasClass(PsiClass.class)) - .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, true)) + .dot(JavaMatchers.hasModifier(PsiModifier.STATIC)) .parent(PsiMatchers.hasClass(PsiClass.class)) - .dot(JavaMatchers.hasModifier(PsiModifier.STATIC, false)) + .dot(JavaMatchers.hasNoModifier(PsiModifier.STATIC)) .parent(PsiMatchers.hasClass(PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class)) .getElement() == null) { return null; @@ -440,52 +437,45 @@ private static HighlightInfo checkStaticClassDeclarationInInnerClass(PsiKeyword TextRange range = context != null ? context.getTextRange() : HighlightNamesUtil.getClassDeclarationTextRange(aClass); - HighlightInfo.Builder info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + HighlightInfo.Builder hlBuilder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(range) .descriptionAndTooltip(JavaErrorLocalize.staticDeclarationInInnerClass()); QuickFixFactory factory = QuickFixFactory.getInstance(); if (context != keyword) { - info.registerFix(factory.createModifierFixBuilder(aClass).remove(PsiModifier.STATIC).create()); + hlBuilder.registerFix(factory.createModifierFixBuilder(aClass).remove(PsiModifier.STATIC).create()); } PsiClass containingClass = aClass.getContainingClass(); if (containingClass != null) { - info.registerFix(factory.createModifierFixBuilder(containingClass).add(PsiModifier.STATIC).create()); + hlBuilder.registerFix(factory.createModifierFixBuilder(containingClass).add(PsiModifier.STATIC).create()); } - return info.create(); + return hlBuilder; } @Nullable @RequiredReadAction - public static HighlightInfo checkStaticDeclarationInInnerClass(PsiKeyword keyword, LanguageLevel languageLevel) { - HighlightInfo errorResult = checkStaticFieldDeclarationInInnerClass(keyword); - if (errorResult != null) { - return errorResult; - } - errorResult = checkStaticMethodDeclarationInInnerClass(keyword, languageLevel); - if (errorResult != null) { - return errorResult; + public static HighlightInfo.Builder checkStaticDeclarationInInnerClass(PsiKeyword keyword, LanguageLevel languageLevel) { + HighlightInfo.Builder errorResult = checkStaticFieldDeclarationInInnerClass(keyword); + if (errorResult == null) { + errorResult = checkStaticMethodDeclarationInInnerClass(keyword, languageLevel); } - errorResult = checkStaticClassDeclarationInInnerClass(keyword); - if (errorResult != null) { - return errorResult; + if (errorResult == null) { + errorResult = checkStaticClassDeclarationInInnerClass(keyword); } - errorResult = checkStaticInitializerDeclarationInInnerClass(keyword); - if (errorResult != null) { - return errorResult; + if (errorResult == null) { + errorResult = checkStaticInitializerDeclarationInInnerClass(keyword); } - return null; + return errorResult; } @Nullable @RequiredReadAction - public static HighlightInfo checkExtendsAllowed(PsiReferenceList list) { + public static HighlightInfo.Builder checkExtendsAllowed(PsiReferenceList list) { if (list.getParent() instanceof PsiClass aClass && aClass.isEnum()) { boolean isExtends = list.equals(aClass.getExtendsList()); if (isExtends) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(list) - .descriptionAndTooltip(JavaErrorLocalize.extendsAfterEnum()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.extendsAfterEnum()); } } return null; @@ -493,7 +483,7 @@ public static HighlightInfo checkExtendsAllowed(PsiReferenceList list) { @Nullable @RequiredReadAction - public static HighlightInfo checkImplementsAllowed(PsiReferenceList list) { + public static HighlightInfo.Builder checkImplementsAllowed(PsiReferenceList list) { if (list.getParent() instanceof PsiClass aClass && aClass.isInterface()) { boolean isImplements = list.equals(aClass.getImplementsList()); if (isImplements) { @@ -504,7 +494,7 @@ public static HighlightInfo checkImplementsAllowed(PsiReferenceList list) { if (referencedTypes.length > 0) { result.registerFix(QuickFixFactory.getInstance().createChangeExtendsToImplementsFix(aClass, referencedTypes[0])); } - return result.create(); + return result; } } return null; @@ -565,11 +555,8 @@ public static HighlightInfo.Builder checkAnonymousInheritFinal(PsiNewExpression return checkCannotInheritFromFinal(baseClass, aClass.getBaseClassReference()); } - @Nullable - private static LocalizeValue checkDefaultConstructorThrowsException( - PsiMethod constructor, - @Nonnull PsiClassType[] handledExceptions - ) { + @Nonnull + private static List collectUnhandledExceptions(PsiMethod constructor, @Nonnull PsiClassType[] handledExceptions) { PsiClassType[] referencedTypes = constructor.getThrowsList().getReferencedTypes(); List exceptions = new ArrayList<>(); for (PsiClassType referencedType : referencedTypes) { @@ -578,10 +565,7 @@ private static LocalizeValue checkDefaultConstructorThrowsException( exceptions.add(referencedType); } } - if (!exceptions.isEmpty()) { - return HighlightUtil.getUnhandledExceptionsDescriptor(exceptions); - } - return null; + return exceptions; } @Nullable @@ -629,11 +613,11 @@ public static HighlightInfo.Builder checkBaseClassDefaultConstructorProblem( int parametersCount = constructor.getParameterList().getParametersCount(); if (parametersCount == 0 || parametersCount == 1 && constructor.isVarArgs()) { // it is an error if base ctr throws exceptions - LocalizeValue description = checkDefaultConstructorThrowsException(constructor, handledExceptions); - if (description != null) { + List exceptions = collectUnhandledExceptions(constructor, handledExceptions); + if (!exceptions.isEmpty()) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(range) - .descriptionAndTooltip(description) + .descriptionAndTooltip(HighlightUtil.getUnhandledExceptionsDescriptor(exceptions)) .registerFix(QuickFixFactory.getInstance().createCreateConstructorMatchingSuperFix(aClass)); } if (refCountHolder != null) { @@ -652,25 +636,23 @@ public static HighlightInfo.Builder checkBaseClassDefaultConstructorProblem( @Nullable @RequiredReadAction - public static HighlightInfo checkInterfaceCannotBeLocal(PsiClass aClass) { + public static HighlightInfo.Builder checkInterfaceCannotBeLocal(PsiClass aClass) { if (PsiUtil.isLocalClass(aClass)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(HighlightNamesUtil.getClassDeclarationTextRange(aClass)) - .descriptionAndTooltip(JavaErrorLocalize.interfaceCannotBeLocal()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.interfaceCannotBeLocal()); } return null; } @Nullable @RequiredReadAction - public static HighlightInfo checkCyclicInheritance(PsiClass aClass) { + public static HighlightInfo.Builder checkCyclicInheritance(PsiClass aClass) { PsiClass circularClass = getCircularClass(aClass, new HashSet<>()); if (circularClass != null) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(HighlightNamesUtil.getClassDeclarationTextRange(aClass)) - .descriptionAndTooltip(JavaErrorLocalize.cyclicInheritance(HighlightUtil.formatClass(circularClass))) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.cyclicInheritance(HighlightUtil.formatClass(circularClass))); } return null; } @@ -771,19 +753,13 @@ public static HighlightInfo checkClassAlreadyImported(PsiClass aClass, PsiElemen @Nullable @RequiredReadAction - public static HighlightInfo checkClassExtendsOnlyOneClass(PsiReferenceList list) { + public static HighlightInfo.Builder checkClassExtendsOnlyOneClass(PsiReferenceList list) { PsiClassType[] referencedTypes = list.getReferencedTypes(); - PsiElement parent = list.getParent(); - if (!(parent instanceof PsiClass)) { - return null; - } - - PsiClass aClass = (PsiClass)parent; - if (!aClass.isInterface() && referencedTypes.length > 1 && aClass.getExtendsList() == list) { + if (list.getParent() instanceof PsiClass aClass && !aClass.isInterface() + && referencedTypes.length > 1 && aClass.getExtendsList() == list) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(list) - .descriptionAndTooltip(JavaErrorLocalize.classCannotExtendMultipleClasses()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.classCannotExtendMultipleClasses()); } return null; diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java index 6e109eda6c..e1e1190d2c 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightMethodUtil.java @@ -167,8 +167,9 @@ private static HighlightInfo.Builder isWeaker( return null; } + @Nullable @RequiredReadAction - public static HighlightInfo checkMethodIncompatibleReturnType( + public static HighlightInfo.Builder checkMethodIncompatibleReturnType( @Nonnull MethodSignatureBackedByPsiMethod methodSignature, @Nonnull List superMethodSignatures, boolean includeRealPositionInfo @@ -176,8 +177,9 @@ public static HighlightInfo checkMethodIncompatibleReturnType( return checkMethodIncompatibleReturnType(methodSignature, superMethodSignatures, includeRealPositionInfo, null); } + @Nullable @RequiredReadAction - public static HighlightInfo checkMethodIncompatibleReturnType( + public static HighlightInfo.Builder checkMethodIncompatibleReturnType( @Nonnull MethodSignatureBackedByPsiMethod methodSignature, @Nonnull List superMethodSignatures, boolean includeRealPositionInfo, @@ -205,7 +207,7 @@ public static HighlightInfo checkMethodIncompatibleReturnType( } TextRange toHighlight = textRange != null ? textRange : includeRealPositionInfo ? method.getReturnTypeElement().getTextRange() : TextRange.EMPTY_RANGE; - HighlightInfo highlightInfo = checkSuperMethodSignature( + HighlightInfo.Builder hlBuilder = checkSuperMethodSignature( superMethod, superMethodSignature, superReturnType, @@ -216,15 +218,16 @@ public static HighlightInfo checkMethodIncompatibleReturnType( toHighlight, PsiUtil.getLanguageLevel(aClass) ); - if (highlightInfo != null) { - return highlightInfo; + if (hlBuilder != null) { + return hlBuilder; } } return null; } - private static HighlightInfo checkSuperMethodSignature( + @Nullable + private static HighlightInfo.Builder checkSuperMethodSignature( @Nonnull PsiMethod superMethod, @Nonnull MethodSignatureBackedByPsiMethod superMethodSignature, PsiType superReturnType, @@ -270,7 +273,8 @@ private static HighlightInfo checkSuperMethodSignature( return createIncompatibleReturnTypeMessage(method, superMethod, substitutedSuperReturnType, returnType, detailMessage, range); } - private static HighlightInfo createIncompatibleReturnTypeMessage( + @Nonnull + private static HighlightInfo.Builder createIncompatibleReturnTypeMessage( @Nonnull PsiMethod method, @Nonnull PsiMethod superMethod, @Nonnull PsiType substitutedSuperReturnType, @@ -290,7 +294,7 @@ private static HighlightInfo createIncompatibleReturnTypeMessage( hlBuilder.registerFix(factory.createChangeParameterClassFix(returnClass, classType)); } - return hlBuilder.create(); + return hlBuilder; } @Nullable @@ -1348,7 +1352,7 @@ private static boolean showShortType(int i, PsiParameter[] parameters, PsiExpres } @RequiredReadAction - public static HighlightInfo checkMethodMustHaveBody(PsiMethod method, PsiClass aClass) { + public static HighlightInfo.Builder checkMethodMustHaveBody(PsiMethod method, PsiClass aClass) { if (method.getBody() == null && !method.isAbstract() && !method.hasModifierProperty(PsiModifier.NATIVE) && aClass != null && !aClass.isInterface() && !PsiUtilCore.hasErrorElementChild(method)) { @@ -1356,14 +1360,13 @@ public static HighlightInfo checkMethodMustHaveBody(PsiMethod method, PsiClass a int end = method.getTextRange().getEndOffset(); QuickFixFactory factory = QuickFixFactory.getInstance(); - HighlightInfo.Builder errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + HighlightInfo.Builder hlBuilder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(start, end) .descriptionAndTooltip(JavaErrorLocalize.missingMethodBody()); if (HighlightUtil.getIncompatibleModifier(PsiModifier.ABSTRACT, method.getModifierList()) == null) { - errorResult.registerFix(factory.createModifierFixBuilder(method).add(PsiModifier.ABSTRACT).create()); + hlBuilder.registerFix(factory.createModifierFixBuilder(method).add(PsiModifier.ABSTRACT).create()); } - return errorResult.registerFix(factory.createAddMethodBodyFix(method)) - .create(); + return hlBuilder.registerFix(factory.createAddMethodBodyFix(method)); } return null; } @@ -1409,7 +1412,7 @@ public static HighlightInfo checkConstructorName(PsiMethod method) { @Nullable @RequiredReadAction - public static HighlightInfo checkDuplicateMethod( + public static HighlightInfo.Builder checkDuplicateMethod( PsiClass aClass, @Nonnull PsiMethod method, @Nonnull MostlySingularMultiMap duplicateMethods @@ -1434,15 +1437,14 @@ public static HighlightInfo checkDuplicateMethod( .descriptionAndTooltip(JavaErrorLocalize.duplicateMethod( JavaHighlightUtil.formatMethod(method), HighlightUtil.formatClass(aClass) - )) - .create(); + )); } return null; } @Nullable @RequiredReadAction - public static HighlightInfo checkMethodCanHaveBody(@Nonnull PsiMethod method, @Nonnull LanguageLevel languageLevel) { + public static HighlightInfo.Builder checkMethodCanHaveBody(@Nonnull PsiMethod method, @Nonnull LanguageLevel languageLevel) { PsiClass aClass = method.getContainingClass(); boolean hasNoBody = method.getBody() == null; boolean isInterface = aClass != null && aClass.isInterface(); @@ -1460,10 +1462,10 @@ public static HighlightInfo checkMethodCanHaveBody(@Nonnull PsiMethod method, @N } else if (isInterface) { if (isStatic && languageLevel.isAtLeast(LanguageLevel.JDK_1_8)) { - description = LocalizeValue.localizeTODO("Static methods in interfaces should have a body"); + description = JavaErrorLocalize.methodStaticInInterfaceShouldHaveBody(); } else if (isPrivate && languageLevel.isAtLeast(LanguageLevel.JDK_1_9)) { - description = LocalizeValue.localizeTODO("Private methods in interfaces should have a body"); + description = JavaErrorLocalize.methodPrivateInInterfaceShouldHaveBody(); } else { return null; @@ -1498,24 +1500,24 @@ else if (method.hasModifierProperty(PsiModifier.NATIVE)) { return null; } - HighlightInfo.Builder info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + HighlightInfo.Builder hlBuilder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(HighlightNamesUtil.getMethodDeclarationTextRange(method)) .descriptionAndTooltip(description); if (!hasNoBody) { - info.registerFix(factory.createDeleteMethodBodyFix(method)); + hlBuilder.registerFix(factory.createDeleteMethodBodyFix(method)); } if (method.isAbstract() && !isInterface) { - info.registerFix(factory.createModifierFixBuilder(method).remove(PsiModifier.ABSTRACT).create()); + hlBuilder.registerFix(factory.createModifierFixBuilder(method).remove(PsiModifier.ABSTRACT).create()); } for (IntentionAction intentionAction : additionalFixes) { - info.registerFix(intentionAction); + hlBuilder.registerFix(intentionAction); } - return info.create(); + return hlBuilder; } @Nullable @RequiredReadAction - public static HighlightInfo checkConstructorCallMustBeFirstStatement(@Nonnull PsiMethodCallExpression methodCall) { + public static HighlightInfo.Builder checkConstructorCallMustBeFirstStatement(@Nonnull PsiMethodCallExpression methodCall) { if (!RefactoringChangeUtil.isSuperOrThisMethodCall(methodCall)) { return null; } @@ -1535,23 +1537,21 @@ public static HighlightInfo checkConstructorCallMustBeFirstStatement(@Nonnull Ps PsiReferenceExpression expression = methodCall.getMethodExpression(); return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(methodCall) - .descriptionAndTooltip(JavaErrorLocalize.constructorCallMustBeFirstStatement(expression.getText() + "()")) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.constructorCallMustBeFirstStatement(expression.getText() + "()")); } - + @Nullable @RequiredReadAction - public static HighlightInfo checkSuperAbstractMethodDirectCall(@Nonnull PsiMethodCallExpression methodCallExpression) { - PsiReferenceExpression expression = methodCallExpression.getMethodExpression(); + public static HighlightInfo.Builder checkSuperAbstractMethodDirectCall(@Nonnull PsiMethodCallExpression methodCall) { + PsiReferenceExpression expression = methodCall.getMethodExpression(); if (!(expression.getQualifierExpression() instanceof PsiSuperExpression)) { return null; } - PsiMethod method = methodCallExpression.resolveMethod(); + PsiMethod method = methodCall.resolveMethod(); if (method != null && method.isAbstract()) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) - .range(methodCallExpression) - .descriptionAndTooltip(JavaErrorLocalize.directAbstractMethodAccess(JavaHighlightUtil.formatMethod(method))) - .create(); + .range(methodCall) + .descriptionAndTooltip(JavaErrorLocalize.directAbstractMethodAccess(JavaHighlightUtil.formatMethod(method))); } return null; } @@ -1656,8 +1656,8 @@ private static HighlightInfo.Builder checkStaticMethodOverride( if (superModifierList.hasModifierProperty(PsiModifier.PRIVATE)) { return null; } - if (superModifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && !JavaPsiFacade.getInstance(manager.getProject()) - .arePackagesTheSame(aClass, superClass)) { + if (superModifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) + && !JavaPsiFacade.getInstance(manager.getProject()).arePackagesTheSame(aClass, superClass)) { return null; } boolean isSuperMethodStatic = superModifierList.hasModifierProperty(PsiModifier.STATIC); @@ -1712,7 +1712,8 @@ private static HighlightInfo.Builder checkStaticMethodOverride( return null; } - private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes( + @Nullable + private static HighlightInfo.Builder checkInterfaceInheritedMethodsReturnTypes( @Nonnull List superMethodSignatures, @Nonnull LanguageLevel languageLevel ) { @@ -1740,9 +1741,10 @@ private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes( } PsiType otherReturnType = otherSuperReturnType; PsiType curType = currentType; - HighlightInfo info = - LambdaUtil.performWithSubstitutedParameterBounds(otherSuperMethod.getTypeParameters(), otherSubstitutor, () -> - { + HighlightInfo.Builder hlBuilder = LambdaUtil.performWithSubstitutedParameterBounds( + otherSuperMethod.getTypeParameters(), + otherSubstitutor, + () -> { if (languageLevel.isAtLeast(LanguageLevel.JDK_1_5)) { //http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8 Example 8.1.5-3 if (!(otherReturnType instanceof PsiPrimitiveType || curType instanceof PsiPrimitiveType)) { @@ -1754,7 +1756,8 @@ private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes( return null; } } - if (otherSuperMethod.getTypeParameters().length > 0 && JavaGenericsUtil.isRawToGeneric(otherReturnType, curType)) { + if (otherSuperMethod.getTypeParameters().length > 0 + && JavaGenericsUtil.isRawToGeneric(otherReturnType, curType)) { return null; } } @@ -1766,16 +1769,18 @@ private static HighlightInfo checkInterfaceInheritedMethodsReturnTypes( JavaErrorLocalize.unrelatedOverridingMethodsReturnTypes(), TextRange.EMPTY_RANGE ); - }); - if (info != null) { - return info; + } + ); + if (hlBuilder != null) { + return hlBuilder; } } return null; } + @Nullable @RequiredReadAction - public static HighlightInfo checkOverrideEquivalentInheritedMethods( + public static HighlightInfo.Builder checkOverrideEquivalentInheritedMethods( PsiClass aClass, PsiFile containingFile, @Nonnull LanguageLevel languageLevel @@ -1805,10 +1810,12 @@ public static HighlightInfo checkOverrideEquivalentInheritedMethods( if (allAbstracts) { superSignatures = new ArrayList<>(superSignatures); superSignatures.add(0, signature); - highlightInfo = checkInterfaceInheritedMethodsReturnTypes(superSignatures, languageLevel); + HighlightInfo.Builder hlBuilder = checkInterfaceInheritedMethodsReturnTypes(superSignatures, languageLevel); + highlightInfo = hlBuilder != null ? hlBuilder.create() : null; } else { - highlightInfo = checkMethodIncompatibleReturnType(signature, superSignatures, false); + HighlightInfo.Builder hlBuilder = checkMethodIncompatibleReturnType(signature, superSignatures, false); + highlightInfo = hlBuilder != null ? hlBuilder.create() : null; } if (highlightInfo != null) { description = highlightInfo.getDescription(); @@ -1853,15 +1860,13 @@ public static HighlightInfo checkOverrideEquivalentInheritedMethods( if (description != null) { // show error info at the class level - TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass); - HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) - .range(textRange) - .descriptionAndTooltip(description) - .create(); + HighlightInfo.Builder hlBuilder = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) + .range(HighlightNamesUtil.getClassDeclarationTextRange(aClass)) + .descriptionAndTooltip(description); if (appendImplementMethodFix) { - QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createImplementMethodsFix(aClass)); + hlBuilder.registerFix(QuickFixFactory.getInstance().createImplementMethodsFix(aClass)); } - return highlightInfo; + return hlBuilder; } return null; } diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightUtil.java index 0ff50f685d..a41031dc46 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -390,7 +390,7 @@ private static PsiClass getPackageLocalClassInTheMiddle(@Nonnull PsiElement plac @Nullable @RequiredReadAction - public static HighlightInfo checkInstanceOfApplicable(@Nonnull PsiInstanceOfExpression expression) { + public static HighlightInfo.Builder checkInstanceOfApplicable(@Nonnull PsiInstanceOfExpression expression) { PsiExpression operand = expression.getOperand(); PsiTypeElement typeElement = expression.getCheckType(); if (typeElement == null) { @@ -409,13 +409,11 @@ public static HighlightInfo checkInstanceOfApplicable(@Nonnull PsiInstanceOfExpr .descriptionAndTooltip(JavaErrorLocalize.inconvertibleTypeCast( JavaHighlightUtil.formatType(operandType), JavaHighlightUtil.formatType(checkType) - )) - .create(); + )); } return null; } - /** * 15.16 Cast Expressions * ( ReferenceType {AdditionalBound} ) expression, where AdditionalBound: & InterfaceType then all must be true @@ -2682,14 +2680,13 @@ public static HighlightInfo checkIllegalType(@Nullable PsiTypeElement typeElemen @Nullable @RequiredReadAction - public static HighlightInfo checkIllegalVoidType(@Nonnull PsiKeyword type) { + public static HighlightInfo.Builder checkIllegalVoidType(@Nonnull PsiKeyword type) { if (!PsiKeyword.VOID.equals(type.getText())) { return null; } - PsiElement parent = type.getParent(); - if (parent instanceof PsiTypeElement) { - PsiElement typeOwner = parent.getParent(); + if (type.getParent() instanceof PsiTypeElement typeElem) { + PsiElement typeOwner = typeElem.getParent(); if (typeOwner != null) { // do not highlight incomplete declarations if (PsiUtilCore.hasErrorElementChild(typeOwner)) { @@ -2698,7 +2695,7 @@ public static HighlightInfo checkIllegalVoidType(@Nonnull PsiKeyword type) { } if (typeOwner instanceof PsiMethod method) { - if (method.getReturnTypeElement() == parent && PsiType.VOID.equals(method.getReturnType())) { + if (method.getReturnTypeElement() == typeElem && PsiType.VOID.equals(method.getReturnType())) { return null; } } @@ -2716,8 +2713,7 @@ else if (typeOwner instanceof JavaCodeFragment) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(type) - .descriptionAndTooltip(JavaErrorLocalize.illegalTypeVoid()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.illegalTypeVoid()); } @Nullable @@ -3025,19 +3021,18 @@ else if (qualifierExpression instanceof PsiSuperExpression superExpr) { @Nullable @RequiredReadAction - public static HighlightInfo checkLabelWithoutStatement(@Nonnull PsiLabeledStatement statement) { + public static HighlightInfo.Builder checkLabelWithoutStatement(@Nonnull PsiLabeledStatement statement) { if (statement.getStatement() == null) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(statement) - .descriptionAndTooltip(JavaErrorLocalize.labelWithoutStatement()) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.labelWithoutStatement()); } return null; } @Nullable @RequiredReadAction - public static HighlightInfo checkLabelAlreadyInUse(@Nonnull PsiLabeledStatement statement) { + public static HighlightInfo.Builder checkLabelAlreadyInUse(@Nonnull PsiLabeledStatement statement) { PsiIdentifier identifier = statement.getLabelIdentifier(); String text = identifier.getText(); PsiElement element = statement; @@ -3049,8 +3044,7 @@ public static HighlightInfo checkLabelAlreadyInUse(@Nonnull PsiLabeledStatement && Objects.equals(labeledStmt.getLabelIdentifier().getText(), text)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(identifier) - .descriptionAndTooltip(JavaErrorLocalize.duplicateLabel(text)) - .create(); + .descriptionAndTooltip(JavaErrorLocalize.duplicateLabel(text)); } element = element.getParent(); } diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index 8044888bee..2c45aedec6 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -334,7 +334,7 @@ public void visitAnnotationMethod(PsiAnnotationMethod method) { add(AnnotationsHighlightUtil.checkClashesWithSuperMethods(method)); if (!myHolder.hasErrorResults() && aClass != null) { - myHolder.add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass))); + add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass))); } } @@ -796,11 +796,11 @@ else if (parent instanceof PsiClass aClass) { myHolder.add(HighlightNamesUtil.highlightClassName(aClass, identifier, colorsScheme)); } if (!myHolder.hasErrorResults() && myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) { - myHolder.add(GenericsHighlightUtil.checkUnrelatedDefaultMethods(aClass, identifier)); + add(GenericsHighlightUtil.checkUnrelatedDefaultMethods(aClass, identifier)); } if (!myHolder.hasErrorResults()) { - myHolder.add(GenericsHighlightUtil.checkUnrelatedConcrete(aClass, identifier)); + add(GenericsHighlightUtil.checkUnrelatedConcrete(aClass, identifier)); } } else if (parent instanceof PsiMethod method) { @@ -925,10 +925,10 @@ else if (resolved instanceof PsiMethod method) { public void visitInstanceOfExpression(@Nonnull PsiInstanceOfExpression expression) { super.visitInstanceOfExpression(expression); if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightUtil.checkInstanceOfApplicable(expression)); + add(HighlightUtil.checkInstanceOfApplicable(expression)); } if (!myHolder.hasErrorResults()) { - myHolder.add(GenericsHighlightUtil.checkInstanceOfGenericType(expression)); + add(GenericsHighlightUtil.checkInstanceOfGenericType(expression)); } } @@ -953,14 +953,14 @@ public void visitKeyword(@Nonnull PsiKeyword keyword) { } else if (PsiKeyword.INTERFACE.equals(text) && parent instanceof PsiClass psiClass) { if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkInterfaceCannotBeLocal(psiClass)); + add(HighlightClassUtil.checkInterfaceCannotBeLocal(psiClass)); } } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkStaticDeclarationInInnerClass(keyword, myLanguageLevel)); + add(HighlightClassUtil.checkStaticDeclarationInInnerClass(keyword, myLanguageLevel)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightUtil.checkIllegalVoidType(keyword)); + add(HighlightUtil.checkIllegalVoidType(keyword)); } } @@ -969,10 +969,10 @@ else if (PsiKeyword.INTERFACE.equals(text) && parent instanceof PsiClass psiClas public void visitLabeledStatement(@Nonnull PsiLabeledStatement statement) { super.visitLabeledStatement(statement); if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightUtil.checkLabelWithoutStatement(statement)); + add(HighlightUtil.checkLabelWithoutStatement(statement)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightUtil.checkLabelAlreadyInUse(statement)); + add(HighlightUtil.checkLabelAlreadyInUse(statement)); } } @@ -1025,7 +1025,7 @@ public void visitMethod(@Nonnull PsiMethod method) { add(checkFeature(method, JavaFeature.EXTENSION_METHODS)); } if (!myHolder.hasErrorResults() && aClass != null) { - myHolder.add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass))); + add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass))); } // method params are highlighted in visitMethod since we should make sure the method body was visited before @@ -1112,10 +1112,10 @@ public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expressio } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkConstructorCallMustBeFirstStatement(expression)); + add(HighlightMethodUtil.checkConstructorCallMustBeFirstStatement(expression)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkSuperAbstractMethodDirectCall(expression)); + add(HighlightMethodUtil.checkSuperAbstractMethodDirectCall(expression)); } if (!myHolder.hasErrorResults()) { @@ -1130,7 +1130,7 @@ public void visitModifierList(@Nonnull PsiModifierList list) { PsiElement parent = list.getParent(); if (parent instanceof PsiMethod method) { if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkMethodCanHaveBody(method, myLanguageLevel)); + add(HighlightMethodUtil.checkMethodCanHaveBody(method, myLanguageLevel)); } MethodSignatureBackedByPsiMethod methodSignature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY); if (!method.isConstructor()) { @@ -1138,7 +1138,7 @@ public void visitModifierList(@Nonnull PsiModifierList list) { List superMethodSignatures = method.getHierarchicalMethodSignature().getSuperSignatures(); if (!superMethodSignatures.isEmpty()) { if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkMethodIncompatibleReturnType( + add(HighlightMethodUtil.checkMethodIncompatibleReturnType( methodSignature, superMethodSignatures, true @@ -1172,7 +1172,7 @@ public void visitModifierList(@Nonnull PsiModifierList list) { } PsiClass aClass = method.getContainingClass(); if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkMethodMustHaveBody(method, aClass)); + add(HighlightMethodUtil.checkMethodMustHaveBody(method, aClass)); } if (!myHolder.hasErrorResults()) { add(HighlightMethodUtil.checkConstructorCallsBaseClassConstructor(method, myRefCountHolder, myResolveHelper)); @@ -1187,7 +1187,7 @@ public void visitModifierList(@Nonnull PsiModifierList list) { else if (parent instanceof PsiClass aClass) { try { if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkDuplicateNestedClass(aClass)); + add(HighlightClassUtil.checkDuplicateNestedClass(aClass)); } if (!myHolder.hasErrorResults()) { TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass); @@ -1201,13 +1201,13 @@ else if (parent instanceof PsiClass aClass) { )); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightMethodUtil.checkOverrideEquivalentInheritedMethods(aClass, myFile, myLanguageLevel)); + add(HighlightMethodUtil.checkOverrideEquivalentInheritedMethods(aClass, myFile, myLanguageLevel)); } if (!myHolder.hasErrorResults() && myOverrideEquivalentMethodsVisitedClasses.add(aClass)) { myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkCyclicInheritance(aClass)); + add(HighlightClassUtil.checkCyclicInheritance(aClass)); } } catch (IndexNotReadyException ignored) { @@ -1890,13 +1890,13 @@ public void visitReferenceList(PsiReferenceList list) { if (!(parent instanceof PsiTypeParameter)) { myHolder.add(AnnotationsHighlightUtil.checkAnnotationDeclaration(parent, list)); if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkExtendsAllowed(list)); + add(HighlightClassUtil.checkExtendsAllowed(list)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkImplementsAllowed(list)); + add(HighlightClassUtil.checkImplementsAllowed(list)); } if (!myHolder.hasErrorResults()) { - myHolder.add(HighlightClassUtil.checkClassExtendsOnlyOneClass(list)); + add(HighlightClassUtil.checkClassExtendsOnlyOneClass(list)); } if (!myHolder.hasErrorResults()) { add(GenericsHighlightUtil.checkGenericCannotExtendException(list)); @@ -2111,7 +2111,7 @@ public void visitTypeParameterList(PsiTypeParameterList list) { if (typeParameters.length > 0) { add(checkFeature(list, JavaFeature.GENERICS)); if (!myHolder.hasErrorResults()) { - myHolder.add(GenericsHighlightUtil.checkTypeParametersList(list, typeParameters, myLanguageLevel)); + add(GenericsHighlightUtil.checkTypeParametersList(list, typeParameters, myLanguageLevel)); } } } diff --git a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/psi/util/JavaMatchers.java b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/psi/util/JavaMatchers.java index 6c4166feac..b251f8b209 100644 --- a/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/psi/util/JavaMatchers.java +++ b/java-analysis-impl/src/main/java/com/intellij/java/analysis/impl/psi/util/JavaMatchers.java @@ -22,24 +22,19 @@ import consulo.language.psi.util.PsiMatcherExpression; public class JavaMatchers { - public static PsiMatcherExpression isConstructor(final boolean shouldBe) { - return new PsiMatcherExpression() { - @Override - public Boolean match(PsiElement element) { - return element instanceof PsiMethod && ((PsiMethod) element).isConstructor() == shouldBe; - } - }; - } + public static PsiMatcherExpression isConstructor(boolean shouldBe) { + return element -> element instanceof PsiMethod method && method.isConstructor() == shouldBe; + } - public static PsiMatcherExpression hasModifier(@PsiModifier.ModifierConstant final String modifier, final boolean shouldHave) { - return new PsiMatcherExpression() { - @Override - public Boolean match(PsiElement element) { - PsiModifierListOwner owner = element instanceof PsiModifierListOwner ? (PsiModifierListOwner) element : null; + public static PsiMatcherExpression hasModifier(@PsiModifier.ModifierConstant String modifier) { + return element -> element instanceof PsiModifierListOwner owner && owner.hasModifierProperty(modifier); + } - if (owner != null && owner.hasModifierProperty(modifier) == shouldHave) return Boolean.TRUE; - return Boolean.FALSE; - } - }; - } + public static PsiMatcherExpression hasNoModifier(@PsiModifier.ModifierConstant String modifier) { + return element -> element instanceof PsiModifierListOwner owner && !owner.hasModifierProperty(modifier); + } + + public static PsiMatcherExpression hasModifier(@PsiModifier.ModifierConstant String modifier, boolean shouldHave) { + return element -> element instanceof PsiModifierListOwner owner && owner.hasModifierProperty(modifier) == shouldHave; + } } diff --git a/java-language-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.language.impl.JavaErrorLocalize.yaml b/java-language-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.language.impl.JavaErrorLocalize.yaml index 82f1a2ffe9..abbaffd1d1 100644 --- a/java-language-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.language.impl.JavaErrorLocalize.yaml +++ b/java-language-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.language.impl.JavaErrorLocalize.yaml @@ -817,4 +817,10 @@ receiver.wrong.position: receiver.type.mismatch: text: The receiver type does not match the enclosing class type receiver.name.mismatch: - text: The receiver name does not match the enclosing class type \ No newline at end of file + text: The receiver name does not match the enclosing class type +method.static.in.interface.should.have.body: + text: Static methods in interfaces should have a body +method.private.in.interface.should.have.body: + text: Private methods in interfaces should have a body +class.inheritance.method.clash: + text: Methods {0} from {1} and {2} from {3} are inherited with the same signature \ No newline at end of file diff --git a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/InsertConstructorCallFix.java b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/InsertConstructorCallFix.java index c88aa65208..86c2605503 100644 --- a/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/InsertConstructorCallFix.java +++ b/plugin/src/main/java/com/intellij/java/impl/codeInsight/daemon/impl/quickfix/InsertConstructorCallFix.java @@ -26,51 +26,58 @@ import consulo.language.psi.PsiFile; import consulo.language.psi.util.PsiMatcherImpl; import consulo.project.Project; - import jakarta.annotation.Nonnull; public class InsertConstructorCallFix implements SyntheticIntentionAction, HighPriorityAction { - protected final PsiMethod myConstructor; - private final String myCall; - - public InsertConstructorCallFix(@Nonnull PsiMethod constructor, String call) { - myConstructor = constructor; - myCall = call; - } + protected final PsiMethod myConstructor; + private final String myCall; - @Override - @Nonnull - public String getText() { - return JavaQuickFixBundle.message("insert.super.constructor.call.text", myCall); - } + public InsertConstructorCallFix(@Nonnull PsiMethod constructor, String call) { + myConstructor = constructor; + myCall = call; + } - @Override - public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) { - return myConstructor.isValid() && myConstructor.getBody() != null && myConstructor.getBody().getLBrace() != null && myConstructor.getManager().isInProject(myConstructor); - } + @Override + @Nonnull + public String getText() { + return JavaQuickFixBundle.message("insert.super.constructor.call.text", myCall); + } - @Nonnull - @Override - public PsiElement getElementToMakeWritable(@Nonnull PsiFile file) { - return myConstructor; - } + @Override + public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) { + return myConstructor.isValid() + && myConstructor.getBody() != null + && myConstructor.getBody().getLBrace() != null + && myConstructor.getManager().isInProject(myConstructor); + } - @Override - public void invoke(@Nonnull Project project, Editor editor, PsiFile file) { - PsiStatement superCall = JavaPsiFacade.getInstance(myConstructor.getProject()).getElementFactory().createStatementFromText(myCall, null); + @Nonnull + @Override + public PsiElement getElementToMakeWritable(@Nonnull PsiFile file) { + return myConstructor; + } - PsiCodeBlock body = myConstructor.getBody(); - PsiJavaToken lBrace = body.getLBrace(); - body.addAfter(superCall, lBrace); - lBrace = (PsiJavaToken) new PsiMatcherImpl(body).firstChild(PsiMatchers.hasClass(PsiExpressionStatement.class)).firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class)).firstChild - (PsiMatchers.hasClass(PsiExpressionList.class)).firstChild(PsiMatchers.hasClass(PsiJavaToken.class)).dot(PsiMatchers.hasText("(")).getElement(); - editor.getCaretModel().moveToOffset(lBrace.getTextOffset() + 1); - LanguageUndoUtil.markPsiFileForUndo(file); - } + @Override + public void invoke(@Nonnull Project project, Editor editor, PsiFile file) { + PsiStatement superCall = + JavaPsiFacade.getInstance(myConstructor.getProject()).getElementFactory().createStatementFromText(myCall, null); - @Override - public boolean startInWriteAction() { - return true; - } + PsiCodeBlock body = myConstructor.getBody(); + PsiJavaToken lBrace = body.getLBrace(); + body.addAfter(superCall, lBrace); + lBrace = (PsiJavaToken)new PsiMatcherImpl(body) + .firstChild(PsiMatchers.hasClass(PsiExpressionStatement.class)) + .firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class)) + .firstChild(PsiMatchers.hasClass(PsiExpressionList.class)) + .firstChild(PsiMatchers.hasClass(PsiJavaToken.class)) + .dot(PsiMatchers.hasText("(")) + .getElement(); + editor.getCaretModel().moveToOffset(lBrace.getTextOffset() + 1); + LanguageUndoUtil.markPsiFileForUndo(file); + } + @Override + public boolean startInWriteAction() { + return true; + } }