changeset 52944:51aa9496a1dd amber-demo

Automatic merge with concise-method-declarations
author mcimadamore
date Tue, 30 Oct 2018 21:16:21 +0100
parents 75b290883f15 15bcdae1ec0d
children 42acc7e03b24
files 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	Tue Oct 30 18:09:17 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/MethodTree.java	Tue Oct 30 21:16:21 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	Tue Oct 30 18:09:17 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/source/util/TreeScanner.java	Tue Oct 30 21:16:21 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	Tue Oct 30 18:09:17 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java	Tue Oct 30 21:16:21 2018 +0100
@@ -930,6 +930,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;
+        }
+
   }
 
     /**