Java optimization: introduce PsiArrayInitializerExpression.getInitializerCount() and use it where possible (IDEA-375485)

GitOrigin-RevId: 1d54ca3773469a22db195a1667ce21cdc679a324
This commit is contained in:
Bas Leijdekkers
2025-09-19 15:09:50 +02:00
committed by intellij-monorepo-bot
parent 78d349d257
commit a1c7ca404c
11 changed files with 33 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// 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.codeInspection.dataFlow.jvm;
import com.intellij.codeInsight.Nullability;
@@ -54,12 +54,12 @@ public enum SpecialField implements DerivedVariableDescriptor {
@NotNull
DfType fromInitializer(PsiExpression initializer) {
if (initializer instanceof PsiArrayInitializerExpression) {
return DfTypes.intValue(((PsiArrayInitializerExpression)initializer).getInitializers().length);
return DfTypes.intValue(((PsiArrayInitializerExpression)initializer).getInitializerCount());
}
if (initializer instanceof PsiNewExpression) {
PsiArrayInitializerExpression arrayInitializer = ((PsiNewExpression)initializer).getArrayInitializer();
if (arrayInitializer != null) {
return DfTypes.intValue(arrayInitializer.getInitializers().length);
return DfTypes.intValue(arrayInitializer.getInitializerCount());
}
PsiExpression[] dimensions = ((PsiNewExpression)initializer).getArrayDimensions();
if (dimensions.length > 0) {

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// 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.codeInspection.dataFlow.jvm.descriptors;
import com.intellij.codeInsight.Nullability;
@@ -175,7 +175,7 @@ public final class ArrayElementDescriptor extends JvmVariableDescriptor {
}
PsiType type = expression.getType();
if (expression instanceof PsiArrayInitializerExpression) {
int length = ((PsiArrayInitializerExpression)expression).getInitializers().length;
int length = ((PsiArrayInitializerExpression)expression).getInitializerCount();
return factory.fromDfType(SpecialField.ARRAY_LENGTH.asDfType(DfTypes.intValue(length))
.meet(DfTypes.typedObject(type, Nullability.NOT_NULL)));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2003-2018 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2025 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -177,8 +177,7 @@ public final class MismatchedArrayReadWriteInspection extends BaseInspection {
return arrayInitializer == null || isZeroSizeArrayExpression(arrayInitializer);
}
if (initializer instanceof PsiArrayInitializerExpression arrayInitializerExpression) {
final PsiExpression[] initializers = arrayInitializerExpression.getInitializers();
return initializers.length == 0;
return arrayInitializerExpression.getInitializerCount() == 0;
}
return false;
}

View File

@@ -223,7 +223,7 @@ public final class CollectionUtils {
public static boolean isEmptyArray(PsiVariable variable) {
final PsiExpression initializer = variable.getInitializer();
return initializer instanceof PsiArrayInitializerExpression arrayInitializerExpression
? arrayInitializerExpression.getInitializers().length == 0
? arrayInitializerExpression.getInitializerCount() == 0
: ConstructionUtils.isEmptyArrayInitializer(initializer);
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.siyeh.ig.psiutils;
import com.intellij.openapi.util.text.StringUtil;
@@ -201,13 +201,10 @@ public final class ConstructionUtils {
final PsiExpression[] dimensions = newExpression.getArrayDimensions();
if (dimensions.length == 0) {
final PsiArrayInitializerExpression arrayInitializer = newExpression.getArrayInitializer();
if (arrayInitializer == null) return false;
final PsiExpression[] initializers = arrayInitializer.getInitializers();
return initializers.length == 0;
return arrayInitializer != null && arrayInitializer.getInitializerCount() == 0;
}
for (PsiExpression dimension : dimensions) {
final String dimensionText = dimension.getText();
if (!"0".equals(dimensionText)) return false;
if (!ExpressionUtils.isZero(dimension)) return false;
}
return true;
}

View File

@@ -72,8 +72,7 @@ public final class ZeroLengthArrayInitializationInspection extends BaseInspectio
@Override
public void visitArrayInitializerExpression(@NotNull PsiArrayInitializerExpression expression) {
super.visitArrayInitializerExpression(expression);
final PsiExpression[] initializers = expression.getInitializers();
if (initializers.length > 0) {
if (expression.getInitializerCount() > 0) {
return;
}
if (expression.getParent() instanceof PsiNewExpression) {

View File

@@ -1115,7 +1115,7 @@ public final class RedundantStringOperationInspection extends AbstractBaseJavaLo
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
if (!(element instanceof PsiNewExpression expression)) return;
final PsiArrayInitializerExpression initializer = expression.getArrayInitializer();
if (initializer == null || initializer.getInitializers().length != 1) return;
if (initializer == null || initializer.getInitializerCount() != 1) return;
PsiReplacementUtil.replaceExpression(expression, initializer.getInitializers()[0].getText(), new CommentTracker());
}
}

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// 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.psi;
import org.jetbrains.annotations.NotNull;
@@ -24,9 +10,16 @@ import org.jetbrains.annotations.NotNull;
*/
public interface PsiArrayInitializerExpression extends PsiExpression {
/**
* Returns the list of expressions initializing members of the array.
* Returns the expressions initializing the elements of the array.
*
* @return the array of member initializer expressions.
* @return an array of initializer expressions.
*/
PsiExpression @NotNull [] getInitializers();
/**
* @return the number of initializer expressions in this array initializer expression
*/
default int getInitializerCount() {
return getInitializers().length;
}
}

View File

@@ -1,18 +1,4 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// 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.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
@@ -36,6 +22,11 @@ public class PsiArrayInitializerExpressionImpl extends ExpressionPsiElement impl
return getChildrenAsPsiElements(ElementType.EXPRESSION_BIT_SET, PsiExpression.ARRAY_FACTORY);
}
@Override
public int getInitializerCount() {
return countChildren(ElementType.EXPRESSION_BIT_SET);
}
@Override
public PsiType getType(){
if (getTreeParent() instanceof PsiNewExpression){

View File

@@ -1057,7 +1057,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
if (!myMatchingVisitor.setResult(myMatchingVisitor.matchSons(initializer1, initializer2))) return;
}
else if (initializer2 != null) {
myMatchingVisitor.setResult(areZeroLiterals(patternExpression.getArrayDimensions()) && initializer2.getInitializers().length == 0);
myMatchingVisitor.setResult(areZeroLiterals(patternExpression.getArrayDimensions()) && initializer2.getInitializerCount() == 0);
return;
}
@@ -1645,7 +1645,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
myMatchingVisitor.setResult(myMatchingVisitor.matchSons(initializer, other));
}
else {
myMatchingVisitor.setResult(((PsiArrayInitializerExpression)other).getInitializers().length == 0);
myMatchingVisitor.setResult(((PsiArrayInitializerExpression)other).getInitializerCount() == 0);
}
}
return;

View File

@@ -175,7 +175,7 @@ class JavaConstructorUCallExpression(
get() {
val initializer = sourcePsi.arrayInitializer
return when {
initializer != null -> initializer.initializers.size
initializer != null -> initializer.initializerCount
sourcePsi.arrayDimensions.isNotEmpty() -> sourcePsi.arrayDimensions.size
else -> sourcePsi.argumentList?.expressionCount ?: 0
}
@@ -244,7 +244,7 @@ class JavaArrayInitializerUCallExpression(
override val valueArgumentCount: Int
get() {
if (valueArgumentCountLazy == Int.MIN_VALUE) {
valueArgumentCountLazy = sourcePsi.initializers.size
valueArgumentCountLazy = sourcePsi.initializerCount
}
return valueArgumentCountLazy