diff options
author | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-02-19 02:08:08 +0100 |
---|---|---|
committer | Reinier Zwitserloot <r.zwitserloot@projectlombok.org> | 2019-02-19 02:08:14 +0100 |
commit | 3ac415254f64d27003a3c5b7a232a33fd785b1f9 (patch) | |
tree | cb6c6be91972b5d6d30e0454eedacd66420284d9 | |
parent | 8479edb4262a7eb8cb76baba138f0ea5aae841c5 (diff) | |
download | lombok-3ac415254f64d27003a3c5b7a232a33fd785b1f9.tar.gz lombok-3ac415254f64d27003a3c5b7a232a33fd785b1f9.tar.bz2 lombok-3ac415254f64d27003a3c5b7a232a33fd785b1f9.zip |
[fixes #1997] try-with-resources where the thing you’re guarding is just a variable ref instead of a full decl (legal since JDK9) would break delombok
-rw-r--r-- | src/delombok/lombok/delombok/PrettyPrinter.java | 19 | ||||
-rw-r--r-- | test/pretty/resource/after/tryWithResourcesVarRef.java | 9 | ||||
-rw-r--r-- | test/pretty/resource/before/TryWithResourcesVarRef.java | 10 |
3 files changed, 33 insertions, 5 deletions
diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index 84c342f0..353bd753 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1268,9 +1268,14 @@ public class PrettyPrinter extends JCTree.Visitor { break; case 1: print("("); - JCVariableDecl decl = (JCVariableDecl) resources.get(0); - flagMod = -1L & ~FINAL; - printVarDefInline(decl); + JCTree resource = (JCTree) resources.get(0); + if (resource instanceof JCVariableDecl) { + JCVariableDecl decl = (JCVariableDecl) resource; + flagMod = -1L & ~FINAL; + printVarDefInline(decl); + } else { + print(resource); + } print(") "); break; default: @@ -1279,8 +1284,12 @@ public class PrettyPrinter extends JCTree.Visitor { int c = 0; for (Object i : resources) { align(); - flagMod = -1L & ~FINAL; - printVarDefInline((JCVariableDecl) i); + if (i instanceof JCVariableDecl) { + flagMod = -1L & ~FINAL; + printVarDefInline((JCVariableDecl) i); + } else { + print((JCTree) i); + } if (++c == len) { print(") "); } else { diff --git a/test/pretty/resource/after/tryWithResourcesVarRef.java b/test/pretty/resource/after/tryWithResourcesVarRef.java new file mode 100644 index 00000000..5117f706 --- /dev/null +++ b/test/pretty/resource/after/tryWithResourcesVarRef.java @@ -0,0 +1,9 @@ +import java.io.PrintWriter; +public class TryWithResourcesVarRef { + { + PrintWriter pw = new PrintWriter(System.out); + try (pw) { + pw.println(); + } + } +} diff --git a/test/pretty/resource/before/TryWithResourcesVarRef.java b/test/pretty/resource/before/TryWithResourcesVarRef.java new file mode 100644 index 00000000..6c039eb7 --- /dev/null +++ b/test/pretty/resource/before/TryWithResourcesVarRef.java @@ -0,0 +1,10 @@ +//version 9: +import java.io.PrintWriter; +public class TryWithResourcesVarRef { + { + PrintWriter pw = new PrintWriter(System.out); + try (pw) { + pw.println(); + } + } +} |