changeset 52943:15bcdae1ec0d concise-method-declarations

Improving the AST model for concise method bodies.
author jlahoda
date Tue, 30 Oct 2018 20:46:30 +0100
parents dd5c0716ba41
children 51aa9496a1dd 8f2715f44ede
files src/jdk.compiler/share/classes/com/sun/source/tree/MethodTree.java src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java
diffstat 3 files changed, 64 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/MethodTree.java	Mon Oct 29 22:17:30 2018 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/MethodTree.java	Tue Oct 30 20:46:30 2018 +0100
@@ -94,7 +94,8 @@
     List<? extends ExpressionTree> getThrows();
 
     /**
-     * Returns the method body, or {@code null} if this is an abstract or native method.
+     * Returns the method body, or {@code null} if this is an abstract or native method, or if the
+     * {@link #BodyKind} is not {@linkplain BLOCK}.
      * @return the method body
      */
     BlockTree getBody();
@@ -106,4 +107,37 @@
      * @return the default value
      */
     Tree getDefaultValue(); // for annotation types
+
+    /**
+     * Returns the method body, or {@code null} if this is an abstract or native method.
+     * @return the method body
+     */
+    public default Tree getBodyTree() {
+        return getBody();
+    }
+
+    /**
+     * The type of the body of this method.
+     * @return the type of the body of this method
+     */
+    public default BodyKind getBodyKind() {
+        return BodyKind.BLOCK;
+    }
+    /**
+     * Defines types of bodies of methods.
+     */
+    public enum BodyKind {
+        /**
+         * The body is a block.
+         */
+        BLOCK,
+        /**
+         * The body is an expression.
+         */
+        EXPRESSION,
+        /**
+         * The body is a method reference.
+         */
+        REFERENCE
+    }
 }
--- a/src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java	Mon Oct 29 22:17:30 2018 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java	Tue Oct 30 20:46:30 2018 +0100
@@ -204,7 +204,7 @@
         r = scanAndReduce(node.getParameters(), p, r);
         r = scanAndReduce(node.getReceiverParameter(), p, r);
         r = scanAndReduce(node.getThrows(), p, r);
-        r = scanAndReduce(node.getBody(), p, r);
+        r = scanAndReduce(node.getBodyTree(), p, r);
         r = scanAndReduce(node.getDefaultValue(), p, r);
         return r;
     }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Mon Oct 29 22:17:30 2018 -0400
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Tue Oct 30 20:46:30 2018 +0100
@@ -915,6 +915,34 @@
         public Tag getTag() {
             return METHODDEF;
         }
+
+        @Override
+        public BodyKind getBodyKind() {
+            if ((mods.flags & Flags.CONCISE_ARROW) != 0)
+                return BodyKind.EXPRESSION;
+            if ((mods.flags & Flags.CONCISE_EQUAL) != 0)
+                return BodyKind.REFERENCE;
+            return BodyKind.BLOCK;
+        }
+
+        @Override
+        public Tree getBodyTree() {
+            if ((mods.flags & Flags.CONCISE_ARROW) != 0) {
+                JCStatement first = body.stats.head;
+                if (first.hasTag(RETURN)) {
+                    return ((JCReturn) first).expr;
+                } else if (first.hasTag(EXEC)) {
+                    return ((JCExpressionStatement) first).expr;
+                } else {
+                    Assert.error("Unknown kind: " + first.getTag());
+                    return null;
+                }
+            }
+            if ((mods.flags & Flags.CONCISE_EQUAL) != 0)
+                return conciseMethodRef;
+            return body;
+        }
+
   }
 
     /**