From 76a10e63e46351778902f89780755ced47db7689 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 16 Jun 2025 15:40:53 +0200 Subject: [PATCH] Java: enable Flexibile Constructor Bodies in non-preview language level 25 (IDEA-372971) GitOrigin-RevId: c0b12174a7dc60cfdf662d7d6d3bc576e734e4d4 --- .../src/com/intellij/pom/java/JavaFeature.kt | 6 +++- ...FlexibleConstructorBodiesNotAvailable.java | 32 +++++++++++++++++++ .../PointlessBooleanExpression.java | 8 ++--- .../daemon/LightAdvHighlightingTest.java | 11 +++++-- .../refactoring/inline/InlineMethodTest.java | 10 +++--- ...ntlessBooleanExpressionInspectionTest.java | 8 ++++- 6 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/FlexibleConstructorBodiesNotAvailable.java diff --git a/java/java-syntax/src/com/intellij/pom/java/JavaFeature.kt b/java/java-syntax/src/com/intellij/pom/java/JavaFeature.kt index e5d76b861367..752dfc4d3dee 100644 --- a/java/java-syntax/src/com/intellij/pom/java/JavaFeature.kt +++ b/java/java-syntax/src/com/intellij/pom/java/JavaFeature.kt @@ -104,7 +104,11 @@ enum class JavaFeature { IMPLICIT_CLASS_NAME_OUT_OF_SCOPE(LanguageLevel.JDK_22_PREVIEW, "feature.implicit.class.name.out.of.scope"), CLASSFILE_API(LanguageLevel.JDK_22_PREVIEW, "feature.classfile.api"), STREAM_GATHERERS(LanguageLevel.JDK_22_PREVIEW, "feature.stream.gatherers"), - STATEMENTS_BEFORE_SUPER(LanguageLevel.JDK_22_PREVIEW, "feature.statements.before.super"), + STATEMENTS_BEFORE_SUPER(LanguageLevel.JDK_22_PREVIEW, "feature.statements.before.super") { + override fun isSufficient(useSiteLevel: LanguageLevel): Boolean { + return super.isSufficient(useSiteLevel) || useSiteLevel.isAtLeast(LanguageLevel.JDK_25) + } + }, /** * Was a preview feature in Java 20 Preview. * Keep the implementation, as it could reappear in the future. diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/FlexibleConstructorBodiesNotAvailable.java b/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/FlexibleConstructorBodiesNotAvailable.java new file mode 100644 index 000000000000..7f6ed2b8e2db --- /dev/null +++ b/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/FlexibleConstructorBodiesNotAvailable.java @@ -0,0 +1,32 @@ +class FlexibleConstructorBodiesNotAvailable { + boolean sideEffect() { + System.out.println("hello"); + return Math.random() > 0.5; + } + + class X { + X(boolean b) {} + } + + class Y extends X { + Y(int i) { + // side-effect cannot be extracted from super call + super(sideEffect() && false); + } + + Y(long l) { + // side-effect cannot be extracted from super call + super(false & sideEffect()); + } + + Y(double d) { + // no side-effect extraction necessary + super(sideEffect() && true); + } + + Y(float f) { + // no side-effect extraction necessary + super(false && sideEffect()); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java b/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java index 6e60fd09c32d..a1e7fb9cd25a 100644 --- a/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java +++ b/java/java-tests/testData/ig/com/siyeh/igtest/controlflow/pointless_boolean_expression/PointlessBooleanExpression.java @@ -40,13 +40,13 @@ class PointlessBooleanExpression { class Y extends X { Y(int i) { - // side-effect cannot be extracted from super call - super(sideEffect() && false); + // side-effect can be extracted from super call with Flexible Constructor Bodies + super(sideEffect() && false); } Y(long l) { - // side-effect cannot be extracted from super call - super(false & sideEffect()); + // side-effect can be extracted from super call with Flexible Constructor Bodies + super(false & sideEffect()); } Y(double d) { diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingTest.java index ac0e1b70af0d..3e29bc63be9c 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/LightAdvHighlightingTest.java @@ -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.java.codeInsight.daemon; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings; @@ -243,7 +243,12 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase { public void testIgnoreImplicitThisReferenceBeforeSuperSinceJdk7() { doTest(false); } public void testStatementsBeforeSuper() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_22_PREVIEW, () -> doTest(false)); } - public void testFlexibleConstructorBodies() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_23_PREVIEW, () -> doTest(false)); } + + public void testFlexibleConstructorBodies() { + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_23_PREVIEW, () -> doTest(false)); + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_25, () -> doTest(false)); + } + public void testCastFromVoid() { doTest(false); } public void testCatchUnknownMethod() { doTest(false); } public void testIDEADEV8822() { doTest(false); } @@ -289,7 +294,7 @@ public class LightAdvHighlightingTest extends LightDaemonAnalyzerTestCase { @Override public boolean isEntryPoint(@NotNull PsiElement psiElement) { - return psiElement instanceof PsiMethod && ((PsiMethod)psiElement).getName().equals("myTestMethod"); + return psiElement instanceof PsiMethod m && m.getName().equals("myTestMethod"); } @Override diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java index 18c19ddf9174..270417f1ceb6 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/inline/InlineMethodTest.java @@ -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.java.refactoring.inline; import com.intellij.JavaTestUtil; @@ -267,8 +267,9 @@ public class InlineMethodTest extends LightRefactoringTestCase { } public void testInSuperCall() { - doTestConflict("Inline cannot be applied to multiline method in constructor call"); - IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_22_PREVIEW, () -> doTest()); + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, () -> doTestConflict("Inline cannot be applied to multiline method in constructor call")); + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_22_PREVIEW, this::doTest); + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_25, this::doTest); } public void testMethodReferenceInsideMethodCall() { @@ -394,7 +395,8 @@ public class InlineMethodTest extends LightRefactoringTestCase { } public void testUnableToInlineCodeBlockToSuper() { - doTestConflict("Inline cannot be applied to multiline method in constructor call"); + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, + () -> doTestConflict("Inline cannot be applied to multiline method in constructor call")); } public void testRedundantCastOnMethodReferenceToLambda() { diff --git a/java/java-tests/testSrc/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspectionTest.java b/java/java-tests/testSrc/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspectionTest.java index 6336fa84ac65..1f49b0ba42bc 100644 --- a/java/java-tests/testSrc/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspectionTest.java +++ b/java/java-tests/testSrc/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspectionTest.java @@ -1,7 +1,9 @@ -// 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.siyeh.ig.controlflow; import com.intellij.codeInspection.InspectionProfileEntry; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.testFramework.IdeaTestUtil; import com.siyeh.ig.LightJavaInspectionTestCase; import org.jetbrains.annotations.Nullable; @@ -55,6 +57,10 @@ public class PointlessBooleanExpressionInspectionTest extends LightJavaInspectio doTest(); } + public void testFlexibleConstructorBodiesNotAvailable() { + IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, this::doTest); + } + public void testRegression() { doTest(); }