[java-highlighting] Unify null-handling in switch-related code

Now, isNullLiteral is used everywhere. There are differences between isNullLiteral and isNullType, but they affect only

GitOrigin-RevId: b778f35bcc321b925689a46a8725df5fcad75c93
This commit is contained in:
Tagir Valeev
2025-03-04 17:38:53 +00:00
committed by intellij-monorepo-bot
parent bca1a631f8
commit cd443481a1
4 changed files with 18 additions and 39 deletions
@@ -1,22 +0,0 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.java.codeserver.core;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiLiteralExpression;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
/**
* Utilities related to Java expressions
*/
public final class JavaPsiExpressionUtil {
/**
* @param expression expression to check
* @return true if the expression is a null literal (possibly parenthesized or cast)
*/
@Contract("null -> false")
public static boolean isNullLiteral(@Nullable PsiExpression expression) {
return PsiUtil.deparenthesizeExpression(expression) instanceof PsiLiteralExpression literal && literal.getValue() == null;
}
}
@@ -174,7 +174,7 @@ public final class JavaPsiSwitchUtil {
@NotNull PsiType selectorType) {
boolean isOverWhomUnconditionalForSelector = JavaPsiPatternUtil.isUnconditionalForType(overWhom, selectorType);
if (!isOverWhomUnconditionalForSelector &&
((!(overWhom instanceof PsiExpression expression) || JavaPsiExpressionUtil.isNullLiteral(expression)) &&
((!(overWhom instanceof PsiExpression expression) || ExpressionUtil.isNullLiteral(expression)) &&
who instanceof PsiKeyword &&
PsiKeyword.DEFAULT.equals(who.getText()) || isInCaseNullDefaultLabel(who))) {
// JEP 440-441
@@ -205,8 +205,7 @@ public final class JavaPsiSwitchUtil {
PsiCaseLabelElementList list = ObjectUtils.tryCast(element.getParent(), PsiCaseLabelElementList.class);
if (list == null || list.getElementCount() != 2) return false;
PsiCaseLabelElement[] elements = list.getElements();
return elements[0] instanceof PsiExpression expr &&
JavaPsiExpressionUtil.isNullLiteral(expr) &&
return elements[0] instanceof PsiExpression expr && ExpressionUtil.isNullLiteral(expr) &&
elements[1] instanceof PsiDefaultCaseLabelElement;
}
@@ -264,7 +263,7 @@ public final class JavaPsiSwitchUtil {
private static boolean shouldConsiderForDominance(@NotNull PsiCaseLabelElement labelElement) {
if (labelElement instanceof PsiPattern) return true;
if (labelElement instanceof PsiExpression) {
boolean isNullType = ExpressionUtil.isNullType(labelElement);
boolean isNullType = ExpressionUtil.isNullLiteral(labelElement);
if (isNullType && isInCaseNullDefaultLabel(labelElement)) {
// JEP 432
// A 'case null, default' label dominates all other switch labels.