-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4119 Handle new switch patterns in CFG
- Loading branch information
Showing
5 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...est-sources/src/main/files/non-compiling/symbolicexecution/engine/SwitchWithPatterns.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.foo; | ||
|
||
/** | ||
* Requires Java 17 (JEP-406) preview feature | ||
*/ | ||
public class SwitchWithPatterns { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(String.format("%d,%d,%d", | ||
switch_array_null_pattern(null), | ||
switch_array_null_pattern(new A()), | ||
switch_array_null_pattern(new A[10]))); | ||
|
||
System.out.println(String.format("%d,%d,%d", | ||
switch_array_default_null_pattern(null), | ||
switch_array_default_null_pattern(new A()), | ||
switch_array_default_null_pattern(new A[10]))); | ||
} | ||
|
||
static Object foo(Object o) { | ||
return switch (o) { | ||
case default -> o; | ||
}; | ||
} | ||
|
||
static int switch_array_null_pattern(Object o) { | ||
return switch (o) { | ||
case Object[] arr -> arr.length; | ||
default -> -1; | ||
case null -> 42; | ||
}; | ||
} | ||
|
||
static int switch_array_default_null_pattern(Object o) { | ||
return switch (o) { | ||
case Object[] arr -> arr.length; | ||
case default, null -> 42; | ||
}; | ||
} | ||
|
||
static String switch_sealed_class_minimum(Shape shape) { | ||
return switch (shape) { | ||
case Triangle t -> "triangle"; | ||
case Rectangle r -> "rectangle"; | ||
}; | ||
} | ||
|
||
static String switch_sealed_class_null_default_sub_classes(Shape shape) { | ||
return switch (shape) { | ||
case null -> "null case"; | ||
case Triangle t -> String.format("triangle (%d,%d,%d)", t.a(), t.b(), t.c()); | ||
case Rectangle r && r.volume() > 42 -> String.format("big rectangle of volume %d!", r.volume()); | ||
case Square s -> "Square!"; | ||
case Rectangle r -> String.format("Rectangle (%d,%d)", r.base, r.height); | ||
default -> "default case"; | ||
}; | ||
} | ||
|
||
public sealed interface Shape permits Rectangle,Triangle { | ||
default int volume() { return 0; } | ||
} | ||
|
||
public static non-sealed class Rectangle implements Shape { | ||
private int base, height; | ||
Rectangle(int base, int height) { this.base = base; this.height = height; } | ||
} | ||
|
||
public static final class Square extends Rectangle { | ||
Square(int side) { super(side, side); } | ||
} | ||
|
||
public static record Triangle(int a, int b, int c) implements Shape {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters