-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Java 21 unnamed patterns
TODO: Activate after eclipse-jdt/eclipse.jdt.core#893 is done. Signed-off-by: Alexander Kriegisch <[email protected]>
- Loading branch information
Showing
4 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
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,85 @@ | ||
import java.awt.*; | ||
import java.util.List; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Examples taken from <a href="https://openjdk.org/jeps/443">JEP 443</a> | ||
*/ | ||
public class UnnamedPatternsPreview1 { | ||
public static void main(String[] args) { | ||
// An enhanced for loop with side effects | ||
int acc = 0; | ||
final int LIMIT = 2; | ||
for (Order _ : List.of(new Order(), new Order(), new Order())) { | ||
if (acc < LIMIT) | ||
acc++; | ||
} | ||
System.out.println(acc); | ||
|
||
// The initialisation of a basic for loop can declare unnamed local variables | ||
for (int i = 0, _ = sideEffect(); i < 2; i++) { | ||
System.out.println(i); | ||
} | ||
|
||
// An assignment statement, where the result of the expression on the right hand side is not needed | ||
Queue<Integer> q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6)); | ||
while (q.size() >= 3) { | ||
var x = q.remove(); | ||
var y = q.remove(); | ||
var _ = q.remove(); | ||
System.out.println(new Point(x, y)); | ||
} | ||
|
||
// The same unnamed variable name '_' can be used in multiple assignment statements | ||
q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6)); | ||
while (q.size() >= 3) { | ||
var x = q.remove(); | ||
var _ = q.remove(); | ||
var _ = q.remove(); | ||
System.out.println(new Point(x, 0)); | ||
} | ||
|
||
// Unnamed variables can be used in one or multiple catch blocks | ||
String s = "123xy"; | ||
try { | ||
int i = Integer.parseInt(s); | ||
System.out.println(i); | ||
} catch (NumberFormatException _) { | ||
System.out.println("Bad number: " + s); | ||
} catch (Exception _) { | ||
System.out.println("Unexpected error"); | ||
} | ||
|
||
// Try with resources | ||
try (var _ = ScopedContext.acquire()) { | ||
System.out.println("Doing something within scoped context"); | ||
} | ||
|
||
// A lambda whose parameter is irrelevant | ||
System.out.println( | ||
Stream.of("one", "two", "three") | ||
.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA")) | ||
); | ||
} | ||
|
||
static int sideEffect() { | ||
System.out.println("side effect"); | ||
return 42; | ||
} | ||
|
||
static class Order {} | ||
|
||
static class ScopedContext implements AutoCloseable { | ||
public static ScopedContext acquire() { | ||
return new ScopedContext(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
System.out.println("Closing scoped context"); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
tests/features1921/java21/UnnamedPatternsPreview1Aspect.aj
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,89 @@ | ||
import java.awt.*; | ||
import java.util.List; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Examples taken from <a href="https://openjdk.org/jeps/443">JEP 443</a> | ||
*/ | ||
public class UnnamedPatternsPreview1Aspect { | ||
public static void main(String[] args) {} | ||
|
||
before() : execution(* main(String[])) { | ||
System.out.println(thisJoinPoint); | ||
|
||
// An enhanced for loop with side effects | ||
int acc = 0; | ||
final int LIMIT = 2; | ||
for (Order _ : List.of(new Order(), new Order(), new Order())) { | ||
if (acc < LIMIT) | ||
acc++; | ||
} | ||
System.out.println(acc); | ||
|
||
// The initialisation of a basic for loop can declare unnamed local variables | ||
for (int i = 0, _ = sideEffect(); i < 2; i++) { | ||
System.out.println(i); | ||
} | ||
|
||
// An assignment statement, where the result of the expression on the right hand side is not needed | ||
Queue<Integer> q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6)); | ||
while (q.size() >= 3) { | ||
var x = q.remove(); | ||
var y = q.remove(); | ||
var _ = q.remove(); | ||
System.out.println(new Point(x, y)); | ||
} | ||
|
||
// The same unnamed variable name '_' can be used in multiple assignment statements | ||
q = new PriorityQueue<>(List.of(1, 2, 3, 4, 5, 6)); | ||
while (q.size() >= 3) { | ||
var x = q.remove(); | ||
var _ = q.remove(); | ||
var _ = q.remove(); | ||
System.out.println(new Point(x, 0)); | ||
} | ||
|
||
// Unnamed variables can be used in one or multiple catch blocks | ||
String s = "123xy"; | ||
try { | ||
int i = Integer.parseInt(s); | ||
System.out.println(i); | ||
} catch (NumberFormatException _) { | ||
System.out.println("Bad number: " + s); | ||
} catch (Exception _) { | ||
System.out.println("Unexpected error"); | ||
} | ||
|
||
// Try with resources | ||
try (var _ = ScopedContext.acquire()) { | ||
System.out.println("Doing something within scoped context"); | ||
} | ||
|
||
// A lambda whose parameter is irrelevant | ||
System.out.println( | ||
Stream.of("one", "two", "three") | ||
.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA")) | ||
); | ||
} | ||
|
||
static int sideEffect() { | ||
System.out.println("side effect"); | ||
return 42; | ||
} | ||
|
||
static class Order {} | ||
|
||
static class ScopedContext implements AutoCloseable { | ||
public static ScopedContext acquire() { | ||
return new ScopedContext(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
System.out.println("Closing scoped context"); | ||
} | ||
} | ||
} |
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