-
CSR
-
Resolution: Approved
-
P3
-
source, behavioral
-
low
-
Using default methods as intended.
-
Java API
-
SE
Summary
Upgrade existing one-arg visit
methods on the visitor interfaces to be default methods.
Problem
When visitModule as added to ElementVisitor (JDK-8142968), it was added as a normal abstract interface method. This approach was necessary in analogous cases back in Java SE 8 since the package in question is built under the previously JDK during the bootstrap process. For 9, better source compatibility would come from using a default method here.
Likewise, several convenience methods in the visitor interfaces can be upgraded to default methods as a small convenience for those directly implementing the interfaces.
Solution
Use default methods where appropriate.
Specification
--- old/src/java.compiler/share/classes/javax/lang/model/element/AnnotationValueVisitor.java 2017-01-18 14:27:09.971342501 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/element/AnnotationValueVisitor.java 2017-01-18 14:27:09.875342503 -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.
--- old/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java 2017-01-18 14:27:10.371342494 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java 2017-01-18 14:27:10.235342496 -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);
+ }
}
--- old/src/java.compiler/share/classes/javax/lang/model/type/TypeVisitor.java 2017-01-18 14:27:10.719342487 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/type/TypeVisitor.java 2017-01-18 14:27:10.603342489 -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.
--- old/src/java.compiler/share/classes/javax/lang/model/util/AbstractElementVisitor6.java 2017-01-18 14:27:11.059342480 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/util/AbstractElementVisitor6.java 2017-01-18 14:27:10.963342482 -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);
}
}
- csr for
-
JDK-8172910 Use default methods as appropriate for language model visitors
-
- Closed
-