From 229cde2bf084e243913030ddad4bce13f6c38732 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 29 Jan 2020 03:07:21 +0100 Subject: [Fixes #2349] Support for JDK 14 Added the ability to parse and pretty-print the new 'x instanceof String y' pattern concept. Added a test to the pretty printer to confirm that it works. --- doc/changelog.markdown | 1 + src/delombok/lombok/delombok/PrettyPrinter.java | 16 ++++++++++++++-- test/pretty/resource/after/PatternInstanceOf.java | 7 +++++++ test/pretty/resource/before/PatternInstanceOf.java | 8 ++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/pretty/resource/after/PatternInstanceOf.java create mode 100644 test/pretty/resource/before/PatternInstanceOf.java diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 8c1b463d..0407f1e3 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -3,6 +3,7 @@ Lombok Changelog ### v1.18.11 "Edgy Guinea Pig" * PLATFORM: Support for JDK13 (including `yield` in switch expressions, as well as delombok having a nicer style for arrow-style switch blocks, and text blocks). +* PLATFORM: Support for JDK14 (including `pattern match` instanceof expressions). * FEATURE: In [`lombok.config`](https://projectlombok.org/features/configuration) it is possible to import other config files, even from a `.zip` or `.jar`. * FEATURE: You can now configure a builder's 'setter' prefixes via `@Builder(setterPrefix = "set")` for example. We discourage doing this, but if some library you use requires them, have at it. [Pull Request #2174](https://github.com/rzwitserloot/lombok/pull/2174], [Issue #1805](https://github.com/rzwitserloot/lombok/issues/1805). * FEATURE: If you use `@Builder`'s `@Singular`, a plural form is also generated, which has the effect of adding all elements in the passed collection. If you pass a null reference, this would result in a message-less `NullPointerException`. Now, it results in that exception but with a useful message attached, and you can choose other behaviors as well via a parameter on the `@Singular` annotation and via `lombok.config`; you can even choose treat them as empty collections; this can be useful when deserializing (e.g. Jackson JSON) and JPA/Hibernate code. [Issue #2221](https://github.com/rzwitserloot/lombok/issues/2221]. [singular documentation](https://projectlombok.org/features/Builder). diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index fc5eaec2..2db70f7f 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2019 The Project Lombok Authors. + * Copyright (C) 2016-2020 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 @@ -1071,7 +1071,11 @@ public class PrettyPrinter extends JCTree.Visitor { @Override public void visitTypeTest(JCInstanceOf tree) { print(tree.expr); print(" instanceof "); - print(tree.clazz); + + /** With java14, instead of a type (field 'clazz'), there's now a pattern (field 'pattern', of type JCTree, which is either a JCPattern for new-style instanceof, or if not it's the same as what 'clazz' held) */ + JCTree c = readObject(tree, "clazz", null); // JDK-13 + if (c == null) c = readObject(tree, "pattern", null); // JDK14+ + print(c); } @Override public void visitTypeCast(JCTypeCast tree) { @@ -1379,6 +1383,12 @@ public class PrettyPrinter extends JCTree.Visitor { println(";", tree); } + void printBindingPattern(JCTree tree) { + print((JCExpression) readObject(tree, "vartype", null)); + print(" "); + print((Name) readObject(tree, "name", null)); + } + @Override public void visitTry(JCTry tree) { aPrint("try "); List resources = readObject(tree, "resources", List.nil()); @@ -1601,6 +1611,8 @@ public class PrettyPrinter extends JCTree.Visitor { printSwitchExpression(tree); } else if ("JCYield".equals(simpleName)) { // Introduced as preview feature in JDK13, part of switch expressions. printYieldExpression(tree); + } else if ("JCBindingPattern".equals(simpleName)) { // Introduced as preview in JDK14 + printBindingPattern(tree); } else { throw new AssertionError("Unhandled tree type: " + tree.getClass() + ": " + tree); } diff --git a/test/pretty/resource/after/PatternInstanceOf.java b/test/pretty/resource/after/PatternInstanceOf.java new file mode 100644 index 00000000..3fcba749 --- /dev/null +++ b/test/pretty/resource/after/PatternInstanceOf.java @@ -0,0 +1,7 @@ +public class PatternInstanceOf { + public void foo(Object o) { + if (o instanceof String y) { + System.out.println(y); + } + } +} diff --git a/test/pretty/resource/before/PatternInstanceOf.java b/test/pretty/resource/before/PatternInstanceOf.java new file mode 100644 index 00000000..c957adeb --- /dev/null +++ b/test/pretty/resource/before/PatternInstanceOf.java @@ -0,0 +1,8 @@ +// version 14: +public class PatternInstanceOf { + public void foo(Object o) { + if (o instanceof String y) { + System.out.println(y); + } + } +} -- cgit