mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java] Use more Java features
GitOrigin-RevId: d0690e949f311dc53bdfe5ff63140e2ef15e4d55
This commit is contained in:
committed by
intellij-monorepo-bot
parent
746cb5401f
commit
d18c733fe1
+2
-2
@@ -1178,9 +1178,9 @@ public final class HighlightUtil {
|
||||
isAllowed = false;
|
||||
}
|
||||
|
||||
boolean isInterface = modifierOwnerParent instanceof PsiClass && ((PsiClass)modifierOwnerParent).isInterface();
|
||||
boolean isInterface = modifierOwnerParent instanceof PsiClass psiClass && psiClass.isInterface();
|
||||
if (PsiModifier.PRIVATE.equals(modifier) && modifierOwnerParent instanceof PsiClass) {
|
||||
isAllowed &= !isInterface || PsiUtil.isLanguageLevel9OrHigher(modifierOwner) && !((PsiClass)modifierOwnerParent).isAnnotationType();
|
||||
isAllowed &= !isInterface || PsiUtil.isAvailable(JavaFeature.PRIVATE_INTERFACE_METHODS, modifierOwner) && !((PsiClass)modifierOwnerParent).isAnnotationType();
|
||||
}
|
||||
else if (PsiModifier.STRICTFP.equals(modifier)) {
|
||||
isAllowed &= !isInterface || PsiUtil.isAvailable(JavaFeature.EXTENSION_METHODS, modifierOwner);
|
||||
|
||||
+1
-1
@@ -116,7 +116,7 @@ public final class RawUseOfParameterizedTypeInspection extends BaseInspection {
|
||||
return new CastQuickFix(typeElement.getText() + StreamEx.constant("?", count).joining(",", "<", ">"));
|
||||
}
|
||||
else if (parent instanceof PsiNewExpression newExpression) {
|
||||
if (!PsiUtil.isLanguageLevel7OrHigher(parent)) return null;
|
||||
if (!PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, parent)) return null;
|
||||
if (newExpression.isArrayCreation() || newExpression.getAnonymousClass() != null) return null;
|
||||
PsiType expectedType = ExpectedTypeUtils.findExpectedType(newExpression, false);
|
||||
if (expectedType == null || expectedType.equals(PsiTypes.nullType()) ||
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.text.LiteralFormatUtil;
|
||||
@@ -20,7 +21,7 @@ public final class InsertLiteralUnderscoresInspection extends LocalInspectionToo
|
||||
|
||||
@Override
|
||||
public @NotNull PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
|
||||
if (!PsiUtil.isLanguageLevel7OrHigher(holder.getFile())) {
|
||||
if (!PsiUtil.isAvailable(JavaFeature.UNDERSCORES, holder.getFile())) {
|
||||
return PsiElementVisitor.EMPTY_VISITOR;
|
||||
}
|
||||
return new JavaElementVisitor() {
|
||||
|
||||
+2
-1
@@ -5,6 +5,7 @@ import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.text.LiteralFormatUtil;
|
||||
@@ -19,7 +20,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
public final class RemoveLiteralUnderscoresInspection extends LocalInspectionTool {
|
||||
@Override
|
||||
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
if (!PsiUtil.isLanguageLevel7OrHigher(holder.getFile())) {
|
||||
if (!PsiUtil.isAvailable(JavaFeature.UNDERSCORES, holder.getFile())) {
|
||||
return PsiElementVisitor.EMPTY_VISITOR;
|
||||
}
|
||||
return new JavaElementVisitor() {
|
||||
|
||||
@@ -139,7 +139,7 @@ public final class ModifierChooser {
|
||||
if (PsiUtil.isAvailable(JavaFeature.SEALED_CLASSES, list)) {
|
||||
return INTERFACE_MEMBER_MODIFIERS_WITH_SEALED;
|
||||
}
|
||||
if (PsiUtil.isLanguageLevel9OrHigher(list)) {
|
||||
if (PsiUtil.isAvailable(JavaFeature.PRIVATE_INTERFACE_METHODS, list)) {
|
||||
return INTERFACE_MEMBER_MODIFIERS_WITH_PRIVATE;
|
||||
}
|
||||
if (PsiUtil.isAvailable(JavaFeature.STATIC_INTERFACE_CALLS, list)) {
|
||||
|
||||
+5
-4
@@ -6,6 +6,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.util.IntentionName;
|
||||
import com.intellij.modcommand.*;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
@@ -40,7 +41,7 @@ public final class AddExceptionToExistingCatchFix extends PsiBasedModCommandActi
|
||||
return ModCommand.chooseAction(QuickFixBundle.message("add.exception.to.existing.catch.chooser.title"), actions);
|
||||
}
|
||||
|
||||
private static List<PsiCatchSection> findSuitableSections(List<? extends PsiCatchSection> sections, @NotNull List<? extends PsiClassType> exceptionTypes, boolean isJava7OrHigher) {
|
||||
private static List<PsiCatchSection> findSuitableSections(List<? extends PsiCatchSection> sections, @NotNull List<? extends PsiClassType> exceptionTypes, boolean multiCatchAvailable) {
|
||||
List<PsiCatchSection> finalSections = new ArrayList<>();
|
||||
for (PsiCatchSection section : ContainerUtil.reverse(sections)) {
|
||||
finalSections.add(section);
|
||||
@@ -54,7 +55,7 @@ public final class AddExceptionToExistingCatchFix extends PsiBasedModCommandActi
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isJava7OrHigher) {
|
||||
if (!multiCatchAvailable) {
|
||||
// if we get to this point, this means, that we can't generify any catch clause, so we can't suggest a fix
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -114,13 +115,13 @@ public final class AddExceptionToExistingCatchFix extends PsiBasedModCommandActi
|
||||
@Nullable
|
||||
static Context from(@NotNull PsiElement element) {
|
||||
if (!element.isValid() || element instanceof PsiMethodReferenceExpression) return null;
|
||||
boolean isJava7OrHigher = PsiUtil.isLanguageLevel7OrHigher(element);
|
||||
boolean multiCatchAvailable = PsiUtil.isAvailable(JavaFeature.MULTI_CATCH, element);
|
||||
List<PsiClassType> unhandledExceptions = new ArrayList<>(ExceptionUtil.getOwnUnhandledExceptions(element));
|
||||
if (unhandledExceptions.isEmpty()) return null;
|
||||
List<PsiTryStatement> tryStatements = getTryStatements(element);
|
||||
List<PsiCatchSection> sections =
|
||||
tryStatements.stream()
|
||||
.flatMap(stmt -> findSuitableSections(Arrays.asList(stmt.getCatchSections()), unhandledExceptions, isJava7OrHigher).stream())
|
||||
.flatMap(stmt -> findSuitableSections(Arrays.asList(stmt.getCatchSections()), unhandledExceptions, multiCatchAvailable).stream())
|
||||
.filter(catchSection -> {
|
||||
PsiParameter parameter = catchSection.getParameter();
|
||||
if (parameter == null) return false;
|
||||
|
||||
+2
-2
@@ -5,10 +5,10 @@ import com.intellij.codeInsight.lookup.ExpressionLookupItem;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.template.PsiElementResult;
|
||||
import com.intellij.codeInsight.template.impl.ConstantNode;
|
||||
import com.intellij.codeInspection.util.OptionalUtil;
|
||||
import com.intellij.java.JavaBundle;
|
||||
import com.intellij.modcommand.*;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
@@ -70,7 +70,7 @@ public class AddVariableInitializerFix extends PsiUpdateModCommandAction<PsiVari
|
||||
if (aClass != null) {
|
||||
if (!aClass.hasModifierProperty(PsiModifier.ABSTRACT) && PsiUtil.hasDefaultConstructor(aClass)) {
|
||||
String typeText = type.getCanonicalText(false);
|
||||
if (aClass.getTypeParameters().length > 0 && PsiUtil.isLanguageLevel7OrHigher(variable)) {
|
||||
if (aClass.getTypeParameters().length > 0 && PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, variable)) {
|
||||
if (!PsiDiamondTypeImpl.haveConstructorsGenericsParameters(aClass)) {
|
||||
typeText = TypeConversionUtil.erasure(type).getCanonicalText(false) + "<>";
|
||||
}
|
||||
|
||||
+1
-1
@@ -264,7 +264,7 @@ public final class SurroundAutoCloseableAction extends PsiUpdateModCommandAction
|
||||
|
||||
@Override
|
||||
public PsiElement @NotNull [] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
|
||||
if (!PsiUtil.isLanguageLevel7OrHigher(file)) return PsiElement.EMPTY_ARRAY;
|
||||
if (!PsiUtil.isAvailable(JavaFeature.TRY_WITH_RESOURCES, file)) return PsiElement.EMPTY_ARRAY;
|
||||
PsiElement element = file.findElementAt(endOffset);
|
||||
PsiElement target = findExpression(element);
|
||||
if (target == null) {
|
||||
|
||||
+2
-1
@@ -14,6 +14,7 @@ import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.DumbAware;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.ProjectScope;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
@@ -30,7 +31,7 @@ public class TryWithResourcesPostfixTemplate extends PostfixTemplate implements
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(@NotNull PsiElement element, @NotNull Document copyDocument, int newOffset) {
|
||||
if (!PsiUtil.isLanguageLevel7OrHigher(element)) return false;
|
||||
if (!PsiUtil.isAvailable(JavaFeature.TRY_WITH_RESOURCES, element)) return false;
|
||||
|
||||
PsiExpression initializer = JavaPostfixTemplatesUtils.getTopmostExpression(element);
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Conditions;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.filters.TrueFilter;
|
||||
import com.intellij.psi.impl.java.stubs.index.JavaStaticMemberTypeIndex;
|
||||
@@ -140,7 +141,7 @@ public abstract class MembersGetter {
|
||||
}
|
||||
// For parameterized class constructors, we add a diamond. Do not suggest constructors for parameterized classes
|
||||
// in Java 6 or older when diamond was not supported
|
||||
if (aClass.getTypeParameters().length > 0 && !PsiUtil.isLanguageLevel7OrHigher(myPlace)) continue;
|
||||
if (aClass.getTypeParameters().length > 0 && !PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, myPlace)) continue;
|
||||
// Constructor type parameters aren't supported yet
|
||||
if (method.getTypeParameters().length > 0) continue;
|
||||
}
|
||||
|
||||
@@ -51,13 +51,13 @@ public final class ForwardCompatibilityInspection extends AbstractBaseJavaLocalI
|
||||
}
|
||||
switch (name) {
|
||||
case PsiKeyword.ASSERT -> {
|
||||
if (languageLevel.isLessThan(LanguageLevel.JDK_1_4) &&
|
||||
if (!JavaFeature.ASSERTIONS.isSufficient(languageLevel) &&
|
||||
(parent instanceof PsiClass || parent instanceof PsiMethod || parent instanceof PsiVariable)) {
|
||||
return JavaErrorBundle.message("assert.identifier.warn");
|
||||
}
|
||||
}
|
||||
case PsiKeyword.ENUM -> {
|
||||
if (languageLevel.isLessThan(LanguageLevel.JDK_1_5) &&
|
||||
if (!JavaFeature.ENUMS.isSufficient(languageLevel) &&
|
||||
(parent instanceof PsiClass || parent instanceof PsiMethod || parent instanceof PsiVariable)) {
|
||||
return JavaErrorBundle.message("enum.identifier.warn");
|
||||
}
|
||||
|
||||
+4
-2
@@ -1,10 +1,12 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
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.*;
|
||||
import com.intellij.psi.impl.PsiDiamondTypeUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -50,7 +52,7 @@ public final class DiamondCanBeReplacedWithExplicitTypeArgumentsInspection exten
|
||||
if (newExpression != null) {
|
||||
final List<PsiType> types = PsiDiamondTypeImpl.resolveInferredTypesNoCheck(newExpression, newExpression).getInferredTypes();
|
||||
if (!types.isEmpty()) {
|
||||
boolean pullToErrors = !PsiUtil.isLanguageLevel7OrHigher(referenceParameterList) ||
|
||||
boolean pullToErrors = !PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, referenceParameterList) ||
|
||||
PsiDiamondTypeImpl.resolveInferredTypes(newExpression, newExpression).getErrorMessage() != null;
|
||||
registerError(referenceParameterList,
|
||||
pullToErrors ? ProblemHighlightType.ERROR : ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.intellij.core.JavaPsiBundle;
|
||||
import com.intellij.openapi.util.NlsContexts;
|
||||
import com.intellij.openapi.util.RecursionGuard;
|
||||
import com.intellij.openapi.util.RecursionManager;
|
||||
import com.intellij.pom.java.JavaFeature;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -161,7 +162,7 @@ public abstract class PsiDiamondType extends PsiType {
|
||||
}
|
||||
|
||||
public static PsiDiamondType getDiamondType(PsiNewExpression expression) {
|
||||
if (PsiUtil.isLanguageLevel7OrHigher(expression)) {
|
||||
if (PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, expression)) {
|
||||
final PsiJavaCodeReferenceElement classReference = expression.getClassOrAnonymousClassReference();
|
||||
if (classReference != null) {
|
||||
final PsiReferenceParameterList parameterList = classReference.getParameterList();
|
||||
|
||||
@@ -134,7 +134,7 @@ public final class PsiDiamondTypeUtil {
|
||||
|
||||
public static String getCollapsedType(PsiType type, PsiElement context) {
|
||||
String typeText = type.getCanonicalText();
|
||||
if (PsiUtil.isLanguageLevel7OrHigher(context)) {
|
||||
if (PsiUtil.isAvailable(JavaFeature.DIAMOND_TYPES, context)) {
|
||||
final int idx = typeText.indexOf('<');
|
||||
if (idx >= 0) {
|
||||
return typeText.substring(0, idx) + "<>";
|
||||
|
||||
Reference in New Issue
Block a user