8172910: Use default methods as appropriate for language model visitors
authordarcy
Wed, 18 Jan 2017 19:39:06 -0800
changeset 43260 f5b52e571607
parent 43259 1a8c1621084d
child 43262 61eeb6ee2289
8172910: Use default methods as appropriate for language model visitors Reviewed-by: jjg
langtools/src/java.compiler/share/classes/javax/lang/model/element/AnnotationValueVisitor.java
langtools/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java
langtools/src/java.compiler/share/classes/javax/lang/model/type/TypeVisitor.java
langtools/src/java.compiler/share/classes/javax/lang/model/util/AbstractElementVisitor6.java
langtools/test/tools/javac/processing/model/TestVisitorDefaults.java
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/element/AnnotationValueVisitor.java	Wed Jan 18 21:09:19 2017 +0300
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/element/AnnotationValueVisitor.java	Wed Jan 18 19:39:06 2017 -0800
@@ -68,8 +68,8 @@
  * javax.lang.model.*} packages bundled in Java SE 8 were required to
  * also be runnable on Java SE 7.  Therefore, default methods
  * were <em>not</em> used when extending {@code javax.lang.model.*}
- * to cover Java SE 8 language features.  However, default methods may
- * be used in subsequent revisions of the {@code javax.lang.model.*}
+ * to cover Java SE 8 language features.  However, default methods
+ * are used in subsequent revisions of the {@code javax.lang.model.*}
  * packages that are only required to run on Java SE 8 and higher
  * platform versions.
  *
@@ -90,11 +90,16 @@
     R visit(AnnotationValue av, P p);
 
     /**
-     * A convenience method equivalent to {@code v.visit(av, null)}.
+     * A convenience method equivalent to {@code visit(av, null)}.
+     *
+     * @implSpec The default implementation is {@code visit(av, null)}.
+     *
      * @param av the value to visit
      * @return  a visitor-specified result
      */
-    R visit(AnnotationValue av);
+    default R visit(AnnotationValue av) {
+        return visit(av, null);
+    }
 
     /**
      * Visits a {@code boolean} value in an annotation.
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java	Wed Jan 18 21:09:19 2017 +0300
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java	Wed Jan 18 19:39:06 2017 -0800
@@ -59,8 +59,8 @@
  * javax.lang.model.*} packages bundled in Java SE 8 were required to
  * also be runnable on Java SE 7.  Therefore, default methods
  * were <em>not</em> used when extending {@code javax.lang.model.*}
- * to cover Java SE 8 language features.  However, default methods may
- * be used in subsequent revisions of the {@code javax.lang.model.*}
+ * to cover Java SE 8 language features.  However, default methods
+ * are used in subsequent revisions of the {@code javax.lang.model.*}
  * packages that are only required to run on Java SE 8 and higher
  * platform versions.
  *
@@ -85,11 +85,16 @@
     R visit(Element e, P p);
 
     /**
-     * A convenience method equivalent to {@code v.visit(e, null)}.
+     * A convenience method equivalent to {@code visit(e, null)}.
+     *
+     * @implSpec The default implementation is {@code visit(e, null)}.
+     *
      * @param e  the element to visit
      * @return a visitor-specified result
      */
-    R visit(Element e);
+    default R visit(Element e) {
+        return visit(e, null);
+    }
 
     /**
      * Visits a package element.
@@ -146,10 +151,16 @@
 
     /**
      * Visits a module element.
+     *
+     * @implSpec Visits a {@code ModuleElement} by calling {@code
+     * visitUnknown(e, p)}.
+     *
      * @param e  the element to visit
      * @param p  a visitor-specified parameter
      * @return a visitor-specified result
      * @since 9
      */
-    R visitModule(ModuleElement e, P p);
+    default R visitModule(ModuleElement e, P p) {
+        return visitUnknown(e, p);
+    }
 }
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/type/TypeVisitor.java	Wed Jan 18 21:09:19 2017 +0300
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/type/TypeVisitor.java	Wed Jan 18 19:39:06 2017 -0800
@@ -59,8 +59,8 @@
  * javax.lang.model.*} packages bundled in Java SE 8 were required to
  * also be runnable on Java SE 7.  Therefore, default methods
  * were <em>not</em> used when extending {@code javax.lang.model.*}
- * to cover Java SE 8 language features.  However, default methods may
- * be used in subsequent revisions of the {@code javax.lang.model.*}
+ * to cover Java SE 8 language features.  However, default methods
+ * are used in subsequent revisions of the {@code javax.lang.model.*}
  * packages that are only required to run on Java SE 8 and higher
  * platform versions.
  *
@@ -85,11 +85,16 @@
     R visit(TypeMirror t, P p);
 
     /**
-     * A convenience method equivalent to {@code v.visit(t, null)}.
+     * A convenience method equivalent to {@code visit(t, null)}.
+     *
+     * @implSpec The default implementation is {@code visit(t, null)}.
+     *
      * @param t the element to visit
      * @return  a visitor-specified result
      */
-    R visit(TypeMirror t);
+    default R visit(TypeMirror t) {
+        return visit(t, null);
+    }
 
     /**
      * Visits a primitive type.
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/util/AbstractElementVisitor6.java	Wed Jan 18 21:09:19 2017 +0300
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/util/AbstractElementVisitor6.java	Wed Jan 18 19:39:06 2017 -0800
@@ -139,6 +139,7 @@
      */
     @Override
     public R visitModule(ModuleElement e, P p) {
-        return visitUnknown(e, p);
+        // Use implementation from interface default method
+        return ElementVisitor.super.visitModule(e, p);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/processing/model/TestVisitorDefaults.java	Wed Jan 18 19:39:06 2017 -0800
@@ -0,0 +1,226 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8172910
+ * @summary Test behavior of default methods on visitors.
+ * @modules java.compiler
+ */
+
+import java.util.List;
+import javax.lang.model.SourceVersion;
+import javax.lang.model.element.*;
+import javax.lang.model.type.*;
+import javax.lang.model.util.*;
+
+/**
+ * Verify expected behavior of default methods on visitors.
+ */
+public class TestVisitorDefaults {
+    public static void main(String... args) {
+        DirectElementVisitorChild dvc = new DirectElementVisitorChild();
+        if (!"visitUnknown".equals(dvc.visitModule(null, null))) {
+            throw new RuntimeException("Problem with DirectElementVisitorChild");
+        }
+        if (!"visit".equals(dvc.visit(null))) {
+            throw new RuntimeException("Problem with DirectElementVisitorChild");
+        }
+
+        IndirectElementVisitorChild ivc = new IndirectElementVisitorChild();
+        if (!"visitUnknown".equals(ivc.visitModule(null, null))) {
+            throw new RuntimeException("Problem with IndirectElementVisitorChild");
+        }
+
+        DirectTypeVisitorChild dtvc = new DirectTypeVisitorChild();
+        if (!"visit".equals(dtvc.visit(null))) {
+            throw new RuntimeException("Problem with DirectTypeVisitorChild");
+        }
+
+        DirectAnnotationVisitorChild davc = new DirectAnnotationVisitorChild();
+        if (!"visit".equals(davc.visit(null))) {
+            throw new RuntimeException("Problem with DirectAnnotationVisitorChild");
+        }
+    }
+
+    private static class DirectElementVisitorChild
+        implements ElementVisitor<String, Object> {
+
+        public DirectElementVisitorChild() {
+            super();
+        }
+
+        @Override
+        public String visitModule(ModuleElement e, Object o) {
+            return ElementVisitor.super.visitModule(e, null);
+        }
+
+        @Override
+        public String visitUnknown(Element e, Object o) {
+            return "visitUnknown";
+        }
+
+        @Override
+        public String visit(Element e) {
+            return ElementVisitor.super.visit(e);
+        }
+
+        @Override
+        public String visit(Element e, Object o) {
+            return "visit";
+        }
+
+        @Override
+        public String visitExecutable(ExecutableElement e, Object o)       { return throwUOE(); }
+        @Override
+        public String visitPackage(PackageElement e, Object o)             { return throwUOE(); }
+        @Override
+        public String visitType(TypeElement e, Object o)                   { return throwUOE(); }
+        @Override
+        public String visitTypeParameter(TypeParameterElement e, Object o) { return throwUOE(); }
+        @Override
+        public String visitVariable(VariableElement e, Object o)           { return throwUOE(); }
+    }
+
+    private static class IndirectElementVisitorChild
+        extends AbstractElementVisitor6<String, Object> {
+
+        public IndirectElementVisitorChild() {
+            super();
+        }
+
+        @Override
+        public String visitModule(ModuleElement e, Object o) {
+            return super.visitModule(e, o);
+        }
+
+
+        @Override
+        public String visitUnknown(Element e, Object o) {
+            return "visitUnknown";
+        }
+
+        @Override
+        public String visitExecutable(ExecutableElement e, Object o)       { return throwUOE(); }
+        @Override
+        public String visitPackage(PackageElement e, Object o)             { return throwUOE(); }
+        @Override
+        public String visitType(TypeElement e, Object o)                   { return throwUOE(); }
+        @Override
+        public String visitTypeParameter(TypeParameterElement e, Object o) { return throwUOE(); }
+        @Override
+        public String visitVariable(VariableElement e, Object o)           { return throwUOE(); }
+    }
+
+
+    private static class DirectTypeVisitorChild
+        implements TypeVisitor<String, Object> {
+
+        public DirectTypeVisitorChild() {
+            super();
+        }
+
+        @Override
+        public String visit(TypeMirror t) {
+            return TypeVisitor.super.visit(t);
+        }
+
+        @Override
+        public String visit(TypeMirror t, Object o) {
+            return "visit";
+        }
+
+        @Override
+        public String visitUnknown(TypeMirror t, Object o)            { return throwUOE(); }
+        @Override
+        public String visitArray(ArrayType t, Object o)               { return throwUOE(); }
+        @Override
+        public String visitDeclared(DeclaredType t, Object o)         { return throwUOE(); }
+        @Override
+        public String visitError(ErrorType t, Object o)               { return throwUOE(); }
+        @Override
+        public String visitExecutable(ExecutableType t, Object o)     { return throwUOE(); }
+        @Override
+        public String visitIntersection(IntersectionType t, Object o) { return throwUOE(); }
+        @Override
+        public String visitNoType(NoType t, Object o)                 { return throwUOE(); }
+        @Override
+        public String visitNull(NullType t, Object o)                 { return throwUOE(); }
+        @Override
+        public String visitPrimitive(PrimitiveType t, Object o)       { return throwUOE(); }
+        @Override
+        public String visitTypeVariable(TypeVariable t, Object o)     { return throwUOE(); }
+        @Override
+        public String visitUnion(UnionType t, Object o)               { return throwUOE(); }
+        @Override
+        public String visitWildcard(WildcardType t, Object o)         { return throwUOE(); }
+    }
+
+    private static class DirectAnnotationVisitorChild
+        implements AnnotationValueVisitor<String, Object> {
+
+        @Override
+        public String visit(AnnotationValue av) {
+            return AnnotationValueVisitor.super.visit(av);
+        }
+
+        @Override
+        public String visit(AnnotationValue av, Object o) {
+            return "visit";
+        }
+
+        @Override
+        public String visitAnnotation(AnnotationMirror a, Object o)    { return throwUOE(); }
+        @Override
+        public String visitArray(List<? extends AnnotationValue> vals,
+                                   Object o)                           { return throwUOE(); }
+        @Override
+        public String visitBoolean(boolean b, Object o)                { return throwUOE(); }
+        @Override
+        public String visitByte(byte b, Object o)                      { return throwUOE(); }
+        @Override
+        public String visitChar(char c, Object o)                      { return throwUOE(); }
+        @Override
+        public String visitDouble(double d, Object o)                  { return throwUOE(); }
+        @Override
+        public String visitEnumConstant(VariableElement c, Object o)   { return throwUOE(); }
+        @Override
+        public String visitFloat(float f, Object o)                    { return throwUOE(); }
+        @Override
+        public String visitInt(int i, Object o)                        { return throwUOE(); }
+        @Override
+        public String visitLong(long i, Object o)                      { return throwUOE(); }
+        @Override
+        public String visitShort(short s, Object o)                    { return throwUOE(); }
+        @Override
+        public String visitString(String s, Object o)                  { return throwUOE(); }
+        @Override
+        public String visitType(TypeMirror t, Object o)                { return throwUOE(); }
+        @Override
+        public String visitUnknown(AnnotationValue av, Object o)       { return throwUOE(); }
+    }
+
+    private static String throwUOE() {
+        throw new UnsupportedOperationException();
+    }
+}