From cccb22d78e96e5dcf07fc2eeb4ed83104b6bd4b9 Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 2 Dec 2024 11:30:30 +0000 Subject: [PATCH] [java-inspections] IDEA-360429 Support guards in switch UAST GitOrigin-RevId: 6679acb5d8380073f75ef7be8a8261c59e6b392b --- .../unusedGuardStatement/expected.xml | 11 +++ .../unusedGuardStatement/src/Main.java | 17 ++++ .../UnusedDeclarationInspectionTest.java | 6 ++ .../controlStructures/USwitchExpression.kt | 11 +++ .../JavaUSwitchExpression.kt | 12 +++ .../NewStyleSwitchExpressionWithGuard.java | 13 +++ .../NewStyleSwitchExpressionWithGuard.log.txt | 83 +++++++++++++++++ ...wStyleSwitchExpressionWithGuard.render.txt | 31 +++++++ .../NewStyleSwitchStatementWithGuard.java | 20 ++++ .../NewStyleSwitchStatementWithGuard.log.txt | 93 +++++++++++++++++++ ...ewStyleSwitchStatementWithGuard.render.txt | 32 +++++++ .../OldStyleSwitchExpressionWithGuard.java | 19 ++++ .../OldStyleSwitchExpressionWithGuard.log.txt | 83 +++++++++++++++++ ...dStyleSwitchExpressionWithGuard.render.txt | 31 +++++++ .../OldStyleSwitchStatementWithGuard.java | 26 ++++++ .../OldStyleSwitchStatementWithGuard.log.txt | 93 +++++++++++++++++++ ...ldStyleSwitchStatementWithGuard.render.txt | 38 ++++++++ .../uast/test/java/SimpleJavaRenderLogTest.kt | 14 ++- 18 files changed, 632 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/inspection/deadCode/unusedGuardStatement/expected.xml create mode 100644 java/java-tests/testData/inspection/deadCode/unusedGuardStatement/src/Main.java create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.java create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.log.txt create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.render.txt create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.java create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.log.txt create mode 100644 uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.render.txt create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.java create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.log.txt create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.render.txt create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.java create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.log.txt create mode 100644 uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.render.txt diff --git a/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/expected.xml b/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/expected.xml new file mode 100644 index 000000000000..19bde539d9dd --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/expected.xml @@ -0,0 +1,11 @@ + + + + Main.java + 3 + 6 + 4 + Test + Class is not instantiated. + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/src/Main.java b/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/src/Main.java new file mode 100644 index 000000000000..d857651a9216 --- /dev/null +++ b/java/java-tests/testData/inspection/deadCode/unusedGuardStatement/src/Main.java @@ -0,0 +1,17 @@ +import java.io.Serializable; + +class Test { + + @SuppressWarnings("unused") + private Object toConflictDetail(String controlClass, T conflict) { + return switch (conflict) { + case String ruleConflict when hasSameControlClass(controlClass, ruleConflict) -> conflict; + case Number nestingConflict when hasSameControlClass(controlClass, nestingConflict) -> null; + default -> null; + }; + } + + private static boolean hasSameControlClass(String controlClass, Object ruleConflict) { + return controlClass.equals(ruleConflict); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInspectionTest.java index d7ff8bee310a..5de2d8e90e87 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/UnusedDeclarationInspectionTest.java @@ -288,6 +288,12 @@ public class UnusedDeclarationInspectionTest extends AbstractUnusedDeclarationTe }); } + public void testUnusedGuardStatement() { + IdeaTestUtil.withLevel(getModule(), JavaFeature.PATTERN_GUARDS_AND_RECORD_PATTERNS.getMinimumLevel(), () -> { + doTest(); + }); + } + public void testBrokenClassToImplicitClass() { IdeaTestUtil.withLevel(getModule(), JavaFeature.IMPLICIT_CLASSES.getMinimumLevel(), () -> { doTest(); diff --git a/uast/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt b/uast/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt index e3dccc76bb9c..bd700c7369d2 100644 --- a/uast/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt +++ b/uast/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt @@ -1,6 +1,7 @@ // Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.uast +import org.jetbrains.annotations.ApiStatus.Experimental import org.jetbrains.uast.internal.acceptList import org.jetbrains.uast.internal.log import org.jetbrains.uast.visitor.UastTypedVisitor @@ -67,10 +68,19 @@ interface USwitchClauseExpression : UExpression { */ val caseValues: List + /** + * Represents the guard expressions for this switch clause or null if there is no guard. + * (for example, expression after `when` in java `switch` statement). + */ + val guard: UExpression? + @Experimental + get() = null + override fun accept(visitor: UastVisitor) { if (visitor.visitSwitchClauseExpression(this)) return uAnnotations.acceptList(visitor) caseValues.acceptList(visitor) + guard?.accept(visitor) visitor.afterVisitSwitchClauseExpression(this) } @@ -98,6 +108,7 @@ interface USwitchClauseExpressionWithBody : USwitchClauseExpression { if (visitor.visitSwitchClauseExpression(this)) return uAnnotations.acceptList(visitor) caseValues.acceptList(visitor) + guard?.accept(visitor) body.accept(visitor) visitor.afterVisitSwitchClauseExpression(this) } diff --git a/uast/uast-java/src/org/jetbrains/uast/java/controlStructures/JavaUSwitchExpression.kt b/uast/uast-java/src/org/jetbrains/uast/java/controlStructures/JavaUSwitchExpression.kt index 772cb2f8b955..a045674f8dc4 100644 --- a/uast/uast-java/src/org/jetbrains/uast/java/controlStructures/JavaUSwitchExpression.kt +++ b/uast/uast-java/src/org/jetbrains/uast/java/controlStructures/JavaUSwitchExpression.kt @@ -118,6 +118,7 @@ class JavaUSwitchEntry( private val caseValuesPart = UastLazyPart>() private val bodyPart = UastLazyPart() + private val guardPart = UastLazyPart() override val sourcePsi: PsiSwitchLabelStatementBase get() = labels.first() @@ -136,6 +137,13 @@ class JavaUSwitchEntry( } } + override val guard: UExpression? + get() = guardPart.getOrBuild { + val expression = labels.singleOrNull() ?: return@getOrBuild null + val guard = expression.guardExpression ?: return@getOrBuild null + JavaConverter.convertPsiElement(guard, this, UExpression::class.java) as? UExpression ?: UnknownJavaExpression(guard, this) + } + override val body: UExpressionList get() = bodyPart.getOrBuild { object : JavaUExpressionList(sourcePsi, JavaSpecialExpressionKinds.SWITCH_ENTRY, this) { @@ -163,6 +171,10 @@ class JavaUSwitchEntry( } } } + + override fun asRenderString(): String = caseValues.joinToString { it.asRenderString() } + + (guard?.let { " when " + it.asRenderString() } ?: "") + + " -> " + body.asRenderString() } internal class DummyYieldExpression( diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.java b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.java new file mode 100644 index 000000000000..445993fbacac --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.java @@ -0,0 +1,13 @@ +public class TypePattern { + static String formatter(Object o) { + String formatted = switch (o) { + case Integer i when i < 0 -> String.format("int %d", i); + case Integer i -> String.format("int %d", i); + case Long l when l < 0 -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> formatted = o.toString(); + }; + return formatted; + } +} \ No newline at end of file diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.log.txt b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.log.txt new file mode 100644 index 000000000000..f2ad8e0062f9 --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.log.txt @@ -0,0 +1,83 @@ +UFile (package = ) + UClass (name = TypePattern) + UMethod (name = formatter) + UParameter (name = o) + UBlockExpression + UDeclarationsExpression + ULocalVariable (name = formatted) + USwitchExpression + USimpleNameReferenceExpression (identifier = o) + UExpressionList (switch) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = i) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = l) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = l) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "long %d") + USimpleNameReferenceExpression (identifier = l) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = d) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "double %f") + USimpleNameReferenceExpression (identifier = d) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = s) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "String %s") + USimpleNameReferenceExpression (identifier = s) + USwitchClauseExpressionWithBody + UDefaultCaseExpression + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = o) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) + UIdentifier (Identifier (toString)) + UReturnExpression + USimpleNameReferenceExpression (identifier = formatted) diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.render.txt b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.render.txt new file mode 100644 index 000000000000..1c8303d9a9c4 --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchExpressionWithGuard.render.txt @@ -0,0 +1,31 @@ +public class TypePattern { + static fun formatter(o: java.lang.Object) : java.lang.String { + var formatted: java.lang.String = switch (o) + java.lang.Integer i when i < 0 -> { + yield String.format("int %d", i) + } + + java.lang.Integer i -> { + yield String.format("int %d", i) + } + + java.lang.Long l when l < 0 -> { + yield String.format("long %d", l) + } + + java.lang.Double d -> { + yield String.format("double %f", d) + } + + java.lang.String s -> { + yield String.format("String %s", s) + } + + else -> { + yield formatted = o.toString() + } + + + return formatted + } +} diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.java b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.java new file mode 100644 index 000000000000..e24ec50950b8 --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.java @@ -0,0 +1,20 @@ +public class TypePattern { + static String formatter(Object o) { + String formatted; + switch (o) { + case Integer i when i < 0 -> + formatted = String.format("int %d", i); + case Integer i -> + formatted = String.format("int %d", i); + case Long l when l < 0 -> + formatted = String.format("long %d", l); + case Double d -> + formatted = String.format("double %f", d); + case String s -> + formatted = String.format("String %s", s); + default -> + formatted = o.toString(); + } + return formatted; + } +} \ No newline at end of file diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.log.txt b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.log.txt new file mode 100644 index 000000000000..9280d08d7294 --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.log.txt @@ -0,0 +1,93 @@ +UFile (package = ) + UClass (name = TypePattern) + UMethod (name = formatter) + UParameter (name = o) + UBlockExpression + UDeclarationsExpression + ULocalVariable (name = formatted) + USwitchExpression + USimpleNameReferenceExpression (identifier = o) + UExpressionList (switch) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = i) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = l) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = l) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "long %d") + USimpleNameReferenceExpression (identifier = l) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = d) + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "double %f") + USimpleNameReferenceExpression (identifier = d) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = s) + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "String %s") + USimpleNameReferenceExpression (identifier = s) + USwitchClauseExpressionWithBody + UDefaultCaseExpression + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = o) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) + UIdentifier (Identifier (toString)) + UReturnExpression + USimpleNameReferenceExpression (identifier = formatted) diff --git a/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.render.txt b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.render.txt new file mode 100644 index 000000000000..97054e9d1edd --- /dev/null +++ b/uast/uast-tests/java/Simple/NewStyleSwitchStatementWithGuard.render.txt @@ -0,0 +1,32 @@ +public class TypePattern { + static fun formatter(o: java.lang.Object) : java.lang.String { + var formatted: java.lang.String + switch (o) + java.lang.Integer i when i < 0 -> { + yield formatted = String.format("int %d", i) + } + + java.lang.Integer i -> { + yield formatted = String.format("int %d", i) + } + + java.lang.Long l when l < 0 -> { + yield formatted = String.format("long %d", l) + } + + java.lang.Double d -> { + yield formatted = String.format("double %f", d) + } + + java.lang.String s -> { + yield formatted = String.format("String %s", s) + } + + else -> { + yield formatted = o.toString() + } + + + return formatted + } +} diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.java b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.java new file mode 100644 index 000000000000..82eae0601486 --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.java @@ -0,0 +1,19 @@ +public class TypePattern { + static String formatter(Object o) { + String formatted = switch (o) { + case Integer i when i < 0: + yield String.format("int %d", i); + case Integer i: + yield String.format("int %d", i); + case Long l when l < 0: + yield String.format("long %d", l); + case Double d: + yield String.format("double %f", d); + case String s: + yield String.format("String %s", s); + default: + yield formatted = o.toString(); + }; + return formatted; + } +} \ No newline at end of file diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.log.txt b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.log.txt new file mode 100644 index 000000000000..f2ad8e0062f9 --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.log.txt @@ -0,0 +1,83 @@ +UFile (package = ) + UClass (name = TypePattern) + UMethod (name = formatter) + UParameter (name = o) + UBlockExpression + UDeclarationsExpression + ULocalVariable (name = formatted) + USwitchExpression + USimpleNameReferenceExpression (identifier = o) + UExpressionList (switch) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = i) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = l) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = l) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "long %d") + USimpleNameReferenceExpression (identifier = l) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = d) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "double %f") + USimpleNameReferenceExpression (identifier = d) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = s) + UExpressionList (switch_entry) + UYieldExpression + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "String %s") + USimpleNameReferenceExpression (identifier = s) + USwitchClauseExpressionWithBody + UDefaultCaseExpression + UExpressionList (switch_entry) + UYieldExpression + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = o) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) + UIdentifier (Identifier (toString)) + UReturnExpression + USimpleNameReferenceExpression (identifier = formatted) diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.render.txt b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.render.txt new file mode 100644 index 000000000000..1c8303d9a9c4 --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchExpressionWithGuard.render.txt @@ -0,0 +1,31 @@ +public class TypePattern { + static fun formatter(o: java.lang.Object) : java.lang.String { + var formatted: java.lang.String = switch (o) + java.lang.Integer i when i < 0 -> { + yield String.format("int %d", i) + } + + java.lang.Integer i -> { + yield String.format("int %d", i) + } + + java.lang.Long l when l < 0 -> { + yield String.format("long %d", l) + } + + java.lang.Double d -> { + yield String.format("double %f", d) + } + + java.lang.String s -> { + yield String.format("String %s", s) + } + + else -> { + yield formatted = o.toString() + } + + + return formatted + } +} diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.java b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.java new file mode 100644 index 000000000000..2719aceb0024 --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.java @@ -0,0 +1,26 @@ +public class TypePattern { + static String formatter(Object o) { + String formatted; + switch (o) { + case Integer i when i < 0: + formatted = String.format("int %d", i); + break; + case Integer i: + formatted = String.format("int %d", i); + break; + case Long l when l < 0: + formatted = String.format("long %d", l); + break; + case Double d: + formatted = String.format("double %f", d); + break; + case String s: + formatted = String.format("String %s", s); + break; + default: + formatted = o.toString(); + break; + } + return formatted; + } +} \ No newline at end of file diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.log.txt b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.log.txt new file mode 100644 index 000000000000..9c26d5e0c2ee --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.log.txt @@ -0,0 +1,93 @@ +UFile (package = ) + UClass (name = TypePattern) + UMethod (name = formatter) + UParameter (name = o) + UBlockExpression + UDeclarationsExpression + ULocalVariable (name = formatted) + USwitchExpression + USimpleNameReferenceExpression (identifier = o) + UExpressionList (switch) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = i) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + UBreakExpression (label = null) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = i) + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "int %d") + USimpleNameReferenceExpression (identifier = i) + UBreakExpression (label = null) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = l) + UBinaryExpression (operator = <) + USimpleNameReferenceExpression (identifier = l) + ULiteralExpression (value = 0) + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "long %d") + USimpleNameReferenceExpression (identifier = l) + UBreakExpression (label = null) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = d) + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "double %f") + USimpleNameReferenceExpression (identifier = d) + UBreakExpression (label = null) + USwitchClauseExpressionWithBody + UPatternExpression + UParameter (name = s) + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = String) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 2)) + UIdentifier (Identifier (format)) + ULiteralExpression (value = "String %s") + USimpleNameReferenceExpression (identifier = s) + UBreakExpression (label = null) + USwitchClauseExpressionWithBody + UDefaultCaseExpression + UExpressionList (switch_entry) + UBinaryExpression (operator = =) + USimpleNameReferenceExpression (identifier = formatted) + UQualifiedReferenceExpression + USimpleNameReferenceExpression (identifier = o) + UCallExpression (kind = UastCallKind(name='method_call'), argCount = 0)) + UIdentifier (Identifier (toString)) + UBreakExpression (label = null) + UReturnExpression + USimpleNameReferenceExpression (identifier = formatted) diff --git a/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.render.txt b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.render.txt new file mode 100644 index 000000000000..0646b06a8258 --- /dev/null +++ b/uast/uast-tests/java/Simple/OldStyleSwitchStatementWithGuard.render.txt @@ -0,0 +1,38 @@ +public class TypePattern { + static fun formatter(o: java.lang.Object) : java.lang.String { + var formatted: java.lang.String + switch (o) + java.lang.Integer i when i < 0 -> { + formatted = String.format("int %d", i) + break + } + + java.lang.Integer i -> { + formatted = String.format("int %d", i) + break + } + + java.lang.Long l when l < 0 -> { + formatted = String.format("long %d", l) + break + } + + java.lang.Double d -> { + formatted = String.format("double %f", d) + break + } + + java.lang.String s -> { + formatted = String.format("String %s", s) + break + } + + else -> { + formatted = o.toString() + break + } + + + return formatted + } +} diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt index 162882a385d1..46b5ea5b598e 100644 --- a/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt @@ -1,8 +1,8 @@ // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.uast.test.java -import org.jetbrains.uast.UFile import com.intellij.platform.uast.testFramework.common.RenderLogTestBase +import org.jetbrains.uast.UFile import org.junit.Test class SimpleJavaRenderLogTest : AbstractJavaRenderLogTest(), RenderLogTestBase { @@ -94,4 +94,16 @@ class SimpleJavaRenderLogTest : AbstractJavaRenderLogTest(), RenderLogTestBase { @Test fun testSwitchCaseRecordPattern() = doTest("Simple/SwitchCaseRecordPattern.java") + + @Test + fun testOldStyleSwitchStatementWithGuard() = doTest("Simple/OldStyleSwitchStatementWithGuard.java") + + @Test + fun testNewStyleSwitchStatementWithGuard() = doTest("Simple/NewStyleSwitchStatementWithGuard.java") + + @Test + fun testOldStyleSwitchExpressionWithGuard() = doTest("Simple/OldStyleSwitchExpressionWithGuard.java") + + @Test + fun testNewStyleSwitchExpressionWithGuard() = doTest("Simple/NewStyleSwitchExpressionWithGuard.java") } \ No newline at end of file