[java] IDEA-345355 Refactor HighlightingFeature (in progress)

Remove JavaFeature.isFeatureSupported() in favor of isAvailable()
Rename getLevel() into getMinimumLevel()
Avoid use of getMinimumLevel() where possible
Javadocs

GitOrigin-RevId: 4354821d8cfcf88cc1c3830b0e3cb3f221a5c122
This commit is contained in:
Tagir Valeev
2024-02-08 10:07:33 +00:00
committed by intellij-monorepo-bot
parent 7625b3c7a2
commit 87c2a93f10
17 changed files with 46 additions and 37 deletions
@@ -3789,19 +3789,20 @@ public final class HighlightUtil {
@NotNull
private static LanguageLevel getApplicableLevel(@NotNull PsiFile file, @NotNull JavaFeature feature) {
LanguageLevel standardLevel = feature.getStandardLevel();
if (feature.getLevel().isPreview()) {
LanguageLevel featureLevel = feature.getMinimumLevel();
if (featureLevel.isPreview()) {
JavaSdkVersion sdkVersion = JavaSdkVersionUtil.getJavaSdkVersion(file);
if (sdkVersion != null) {
if (standardLevel != null && sdkVersion.isAtLeast(JavaSdkVersion.fromLanguageLevel(standardLevel))) {
return standardLevel;
}
LanguageLevel previewLevel = sdkVersion.getMaxLanguageLevel().getPreviewLevel();
if (previewLevel != null && previewLevel.isAtLeast(feature.getLevel())) {
if (previewLevel != null && previewLevel.isAtLeast(featureLevel)) {
return previewLevel;
}
}
}
return feature.getLevel();
return featureLevel;
}
@Nullable
@@ -3872,7 +3873,7 @@ public final class HighlightUtil {
Module module = ModuleUtilCore.findModuleForPsiElement(file);
if (module != null) {
LanguageLevel moduleLanguageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(module);
if (moduleLanguageLevel.isAtLeast(feature.getLevel()) && !feature.isLimited()) {
if (moduleLanguageLevel.isAtLeast(feature.getMinimumLevel()) && !feature.isLimited()) {
for (FilePropertyPusher<?> pusher : FilePropertyPusher.EP_NAME.getExtensionList()) {
if (pusher instanceof JavaLanguageLevelPusher) {
String newMessage = ((JavaLanguageLevelPusher)pusher).getInconsistencyLanguageLevelMessage(message, level, file);
@@ -249,7 +249,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
myLanguageLevel = PsiUtil.getLanguageLevel(file);
myJavaSdkVersion = ObjectUtils
.notNull(JavaVersionService.getInstance().getJavaSdkVersion(file), JavaSdkVersion.fromLanguageLevel(myLanguageLevel));
myJavaModule = myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel()) ? JavaModuleGraphUtil.findDescriptorByElement(file) : null;
myJavaModule = JavaFeature.MODULES.isSufficient(myLanguageLevel) ? JavaModuleGraphUtil.findDescriptorByElement(file) : null;
myPreviewFeatureVisitor = myLanguageLevel.isPreview() ? null : new PreviewFeatureUtil.PreviewFeatureVisitor(myLanguageLevel, myErrorSink);
}
@@ -1134,7 +1134,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitPackageStatement(@NotNull PsiPackageStatement statement) {
super.visitPackageStatement(statement);
add(AnnotationsHighlightUtil.checkPackageAnnotationContainingFile(statement, myFile));
if (myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel())) {
if (JavaFeature.MODULES.isSufficient(myLanguageLevel)) {
if (!hasErrorResults()) add(ModuleHighlightUtil.checkPackageStatement(statement, myFile, myJavaModule));
}
if (!hasErrorResults()) add(HighlightImplicitClassUtil.checkPackageNotAllowedInImplicitClass(statement, myFile));
@@ -1958,7 +1958,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitRequiresStatement(@NotNull PsiRequiresStatement statement) {
super.visitRequiresStatement(statement);
if (myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel())) {
if (JavaFeature.MODULES.isSufficient(myLanguageLevel)) {
if (!hasErrorResults()) add(ModuleHighlightUtil.checkModuleReference(statement));
if (!hasErrorResults() && myLanguageLevel.isAtLeast(LanguageLevel.JDK_10)) {
ModuleHighlightUtil.checkModifiers(statement, myErrorSink);
@@ -1969,7 +1969,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitPackageAccessibilityStatement(@NotNull PsiPackageAccessibilityStatement statement) {
super.visitPackageAccessibilityStatement(statement);
if (myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel())) {
if (JavaFeature.MODULES.isSufficient(myLanguageLevel)) {
if (!hasErrorResults()) add(ModuleHighlightUtil.checkHostModuleStrength(statement));
if (!hasErrorResults()) add(ModuleHighlightUtil.checkPackageReference(statement, myFile));
if (!hasErrorResults()) ModuleHighlightUtil.checkPackageAccessTargets(statement, myErrorSink);
@@ -1979,7 +1979,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitUsesStatement(@NotNull PsiUsesStatement statement) {
super.visitUsesStatement(statement);
if (myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel())) {
if (JavaFeature.MODULES.isSufficient(myLanguageLevel)) {
if (!hasErrorResults()) add(ModuleHighlightUtil.checkServiceReference(statement.getClassReference()));
}
}
@@ -1987,7 +1987,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitProvidesStatement(@NotNull PsiProvidesStatement statement) {
super.visitProvidesStatement(statement);
if (myLanguageLevel.isAtLeast(JavaFeature.MODULES.getLevel())) {
if (JavaFeature.MODULES.isSufficient(myLanguageLevel)) {
if (!hasErrorResults()) ModuleHighlightUtil.checkServiceImplementations(statement, myFile, myErrorSink);
}
}
@@ -61,7 +61,7 @@ public enum HighlightingFeature {
}
public LanguageLevel getLevel() {
return myFeature.getLevel();
return myFeature.getMinimumLevel();
}
/**
@@ -48,7 +48,8 @@ public final class AnonymousHasLambdaAlternativeInspection extends AbstractBaseJ
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.THREAD_LOCAL_WITH_INITIAL.isFeatureSupported(holder.getFile())) {
@NotNull PsiFile context = holder.getFile();
if (!JavaFeature.THREAD_LOCAL_WITH_INITIAL.isAvailable(context)) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -4,8 +4,8 @@ package com.intellij.codeInspection.java18api;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.JavaFeature;
import com.intellij.psi.*;
@@ -19,7 +19,7 @@ public class Java8ListSortInspection extends AbstractBaseJavaLocalInspectionTool
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -62,7 +62,7 @@ public final class TryWithIdenticalCatchesInspection extends BaseInspection {
@Override
public boolean shouldInspect(@NotNull PsiFile file) {
return JavaFeature.MULTI_CATCH.isFeatureSupported(file);
return JavaFeature.MULTI_CATCH.isAvailable(file);
}
@Override
@@ -76,7 +76,7 @@ public final class ExplicitArrayFillingInspection extends AbstractBaseJavaLocalI
registerProblem(statement, false);
return;
}
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) return;
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) return;
if (!StreamApiUtil.isSupportedStreamElement(container.getElementType())) return;
if (!LambdaGenerationUtil.canBeUncheckedLambda(rValue, Predicate.isEqual(loop.getCounter()))) return;
registerProblem(statement, true);
@@ -5,12 +5,12 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.LambdaCanBeMethodReferenceInspection;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.codeInspection.util.ForEachCollectionTraversal;
import com.intellij.codeInspection.util.IterableTraversal;
import com.intellij.codeInspection.util.IteratorDeclaration;
import com.intellij.codeInspection.util.LambdaGenerationUtil;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.JavaFeature;
import com.intellij.psi.*;
@@ -34,7 +34,7 @@ public class Java8CollectionRemoveIfInspection extends AbstractBaseJavaLocalInsp
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -7,12 +7,12 @@ import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.LambdaCanBeMethodReferenceInspection;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.codeInspection.util.IteratorDeclaration;
import com.intellij.codeInspection.util.LambdaGenerationUtil;
import com.intellij.java.JavaBundle;
import com.intellij.modcommand.ModPsiUpdater;
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.JavaFeature;
import com.intellij.psi.*;
@@ -52,7 +52,7 @@ public final class Java8ListReplaceAllInspection extends AbstractBaseJavaLocalIn
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -79,7 +79,7 @@ public final class Java8MapApiInspection extends AbstractBaseJavaLocalInspection
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -1,7 +1,10 @@
// 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.codeInspection.java18api;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.options.OptPane;
import com.intellij.codeInspection.util.LambdaGenerationUtil;
import com.intellij.java.JavaBundle;
@@ -51,7 +54,7 @@ public final class Java8MapForEachInspection extends AbstractBaseJavaLocalInspec
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.ADVANCED_COLLECTIONS_API.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -84,7 +84,7 @@ public final class StreamApiMigrationInspection extends AbstractBaseJavaLocalIns
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
PsiFile file = holder.getFile();
VirtualFile virtualFile = file.getVirtualFile();
if (!JavaFeature.STREAMS.isFeatureSupported(file) || virtualFile == null ||
if (!JavaFeature.STREAMS.isAvailable(file) || virtualFile == null ||
!FileIndexFacade.getInstance(holder.getProject()).isInSourceContent(virtualFile)) {
return PsiElementVisitor.EMPTY_VISITOR;
}
@@ -326,7 +326,7 @@ public class ConvertRecordToClassFix extends PsiUpdateModCommandAction<PsiElemen
:
"if(obj == this) return true;\n" +
"if(obj == null || obj.getClass() != this.getClass()) return false;\n" +
(myLanguageLevel.isAtLeast(JavaFeature.LVTI.getLevel()) ? PsiKeyword.VAR : psiClass.getName()) +
(JavaFeature.LVTI.isSufficient(myLanguageLevel) ? PsiKeyword.VAR : psiClass.getName()) +
" that = (" + psiClass.getName() + ")obj;\n" +
"return " + equalsExpression + ";\n";
return "@" + CommonClassNames.JAVA_LANG_OVERRIDE + "\n" +
@@ -34,7 +34,7 @@ public final class StaticPseudoFunctionalStyleMethodInspection extends AbstractB
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.STREAMS.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.STREAMS.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -128,10 +128,10 @@ public enum JavaFeature {
STATEMENTS_BEFORE_SUPER(LanguageLevel.JDK_22_PREVIEW, "feature.statements.before.super"),
;
private final LanguageLevel myLevel;
private final @NotNull LanguageLevel myLevel;
@PropertyKey(resourceBundle = JavaPsiBundle.BUNDLE)
private final String myKey;
private final @NotNull String myKey;
private final boolean myCanBeCustomized;
JavaFeature(@NotNull LanguageLevel level, @NotNull @PropertyKey(resourceBundle = JavaPsiBundle.BUNDLE) String key) {
@@ -144,19 +144,23 @@ public enum JavaFeature {
myKey = key;
myCanBeCustomized = canBeCustomized;
}
public @Nls String getFeatureName() {
/**
* @return Human-readable feature name
*/
public @NotNull @Nls String getFeatureName() {
return JavaPsiBundle.message(myKey);
}
public LanguageLevel getLevel() {
/**
* @return minimal language level where feature is available.
* Note that this doesn't mean that the feature is available on every language level which is higher.
* In most of the cases, {@link #isAvailable(PsiElement)} or {@link #isSufficient(LanguageLevel)} should be used instead.
*/
public @NotNull LanguageLevel getMinimumLevel() {
return myLevel;
}
public boolean isFeatureSupported(@NotNull PsiFile context) {
return isAvailable(context);
}
/**
* @param element a valid PsiElement to check (it's better to supply PsiFile if already known; any element is accepted for convenience)
* @return true if this feature is available in the PsiFile the supplied element belongs to
@@ -56,7 +56,7 @@ public final class GuavaInspection extends AbstractBaseJavaLocalInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
if (!JavaFeature.STREAMS.isFeatureSupported(holder.getFile())) {
if (!JavaFeature.STREAMS.isAvailable(holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@@ -103,7 +103,7 @@ class JavaApiUsageGenerator : LightJavaCodeInsightFixtureTestCase() {
val annotation = PreviewFeatureUtil.getPreviewFeatureAnnotation(e)
?: return null
val feature = PreviewFeatureUtil.fromPreviewFeatureAnnotation(annotation)
return feature?.level
return feature?.minimumLevel
}
}
if (LANGUAGE_LEVEL.isPreview) {