From 23307eb8d137d7e4a3f645552135ab7354ca1e6e Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Thu, 14 Mar 2024 20:28:26 +0100 Subject: [PATCH] [jdk22] Adds support for unnamed variables (JEP 456) --- .../lombok/delombok/PrettyPrinter.java | 12 +++- .../resource/after/UnnamedVariables.java | 70 +++++++++++++++++++ .../resource/before/UnnamedVariables.java | 70 +++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 test/pretty/resource/after/UnnamedVariables.java create mode 100644 test/pretty/resource/before/UnnamedVariables.java diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index d74344544..85598d320 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2021 The Project Lombok Authors. + * Copyright (C) 2016-2024 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -263,6 +263,14 @@ private boolean suppress(JCTree tree) { return false; } + private void print(Name name) { + if (name.isEmpty()) { + print("_"); + } else { + print(name.toString()); + } + } + private void print(CharSequence s) { boolean align = needsAlign; if (needsNewLine && !onNewLine) println(); @@ -1722,6 +1730,8 @@ public void visitTypeBoundKind(TypeBoundKind tree) { printPatternCaseLabel(tree); } else if (className.endsWith("$JCRecordPattern")) { // Introduced in JDK19 printRecordPattern(tree); + } else if (className.endsWith("$JCAnyPattern")) { // Introduced in JDK22 + print("_"); } else { throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree); } diff --git a/test/pretty/resource/after/UnnamedVariables.java b/test/pretty/resource/after/UnnamedVariables.java new file mode 100644 index 000000000..f95b29cae --- /dev/null +++ b/test/pretty/resource/after/UnnamedVariables.java @@ -0,0 +1,70 @@ +// version 22: +import java.util.Arrays; +import java.util.Scanner; +import java.util.stream.Stream; + +record Box(T content) { +} + +record Pair(T first, U second) { +} + +public class UnnamedVariables { + void enhancedLoop() { + for (String _ : Arrays.asList("")) { + } + } + + void initializationInForLoop() { + for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) { + } + } + + void assignment() { + var _ = 1; + } + + void catchBlock() { + try { + } catch (Exception _) { + } + } + + void tryWithResources() { + try (var _ = new Scanner("")) { + } + } + + void lambda() { + Stream.of(1).forEach(_ -> { + }); + } + + Object switchStatement(Object o) { + return switch (o) { + case String _, Integer _ -> o; + case Long _ -> o; + default -> o; + }; + } + + void recordPattern(Object o) { + if (o instanceof Pair(Box _, Box(Integer _))) { + } + if (o instanceof Box(String _)) { + } + if (o instanceof Box(var _)) { + } + if (o instanceof Box(_)) { + } + } + + Object recordPatternSwitch(Object o) { + return switch (o) { + case Box(String _), Box(Integer _) -> o; + case Box(Long _) -> o; + case Box(_) -> o; + default -> o; + }; + } +} diff --git a/test/pretty/resource/before/UnnamedVariables.java b/test/pretty/resource/before/UnnamedVariables.java new file mode 100644 index 000000000..f95b29cae --- /dev/null +++ b/test/pretty/resource/before/UnnamedVariables.java @@ -0,0 +1,70 @@ +// version 22: +import java.util.Arrays; +import java.util.Scanner; +import java.util.stream.Stream; + +record Box(T content) { +} + +record Pair(T first, U second) { +} + +public class UnnamedVariables { + void enhancedLoop() { + for (String _ : Arrays.asList("")) { + } + } + + void initializationInForLoop() { + for (int i = 0, _ = Integer.MAX_VALUE; i < 10; i++) { + } + } + + void assignment() { + var _ = 1; + } + + void catchBlock() { + try { + } catch (Exception _) { + } + } + + void tryWithResources() { + try (var _ = new Scanner("")) { + } + } + + void lambda() { + Stream.of(1).forEach(_ -> { + }); + } + + Object switchStatement(Object o) { + return switch (o) { + case String _, Integer _ -> o; + case Long _ -> o; + default -> o; + }; + } + + void recordPattern(Object o) { + if (o instanceof Pair(Box _, Box(Integer _))) { + } + if (o instanceof Box(String _)) { + } + if (o instanceof Box(var _)) { + } + if (o instanceof Box(_)) { + } + } + + Object recordPatternSwitch(Object o) { + return switch (o) { + case Box(String _), Box(Integer _) -> o; + case Box(Long _) -> o; + case Box(_) -> o; + default -> o; + }; + } +}