8191234: TypeKindVisitor needs to handle modules
authordarcy
Mon, 27 Nov 2017 18:00:56 -0800
changeset 47944 ead84f53a32c
parent 47943 cece8b7adf10
child 47946 e5df7ccc4b73
8191234: TypeKindVisitor needs to handle modules Reviewed-by: jjg
src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor6.java
src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor9.java
test/langtools/tools/javac/processing/model/util/TestTypeKindVisitors.java
--- a/src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor6.java	Mon Nov 27 22:22:03 2017 +0100
+++ b/src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor6.java	Mon Nov 27 18:00:56 2017 -0800
@@ -257,7 +257,7 @@
      *
      * @implSpec This implementation dispatches to the visit method for
      * the specific {@linkplain TypeKind kind} of pseudo-type:
-     * {@code VOID}, {@code PACKAGE}, or {@code NONE}.
+     * {@code VOID}, {@code PACKAGE}, {@code MODULE}, or {@code NONE}.
      *
      * @param t {@inheritDoc}
      * @param p {@inheritDoc}
@@ -273,6 +273,9 @@
         case PACKAGE:
             return visitNoTypeAsPackage(t, p);
 
+        case MODULE:
+            return visitNoTypeAsModule(t, p);
+
         case NONE:
             return visitNoTypeAsNone(t, p);
 
@@ -308,6 +311,21 @@
     }
 
     /**
+     * Visits a {@link TypeKind#MODULE MODULE} pseudo-type.
+     *
+     * @implSpec This implementation calls {@code visitUnknown}.
+     *
+     * @param t the type to visit
+     * @param p a visitor-specified parameter
+     * @return  the result of {@code visitUnknown}
+     *
+     * @since 10
+     */
+    public R visitNoTypeAsModule(NoType t, P p) {
+        return visitUnknown(t, p);
+    }
+
+    /**
      * Visits a {@link TypeKind#NONE NONE} pseudo-type.
      *
      * @implSpec This implementation calls {@code defaultAction}.
--- a/src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor9.java	Mon Nov 27 22:22:03 2017 +0100
+++ b/src/java.compiler/share/classes/javax/lang/model/util/TypeKindVisitor9.java	Mon Nov 27 18:00:56 2017 -0800
@@ -93,4 +93,20 @@
     protected TypeKindVisitor9(R defaultValue) {
         super(defaultValue);
     }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @implSpec This implementation calls {@code defaultAction}.
+     *
+     * @param t {@inheritDoc}
+     * @param p {@inheritDoc}
+     * @return  the result of {@code defaultAction}
+     *
+     * @since 10
+     */
+    @Override
+    public R visitNoTypeAsModule(NoType t, P p) {
+        return defaultAction(t, p);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/processing/model/util/TestTypeKindVisitors.java	Mon Nov 27 18:00:56 2017 -0800
@@ -0,0 +1,196 @@
+/*
+ * Copyright (c) 2013, 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     8191234
+ * @summary Test TypeKind visitors on pseudo types.
+ * @library /tools/javac/lib
+ * @modules java.compiler
+ * @build   JavacTestingAbstractProcessor TestTypeKindVisitors
+ * @compile -processor TestTypeKindVisitors -proc:only TestTypeKindVisitors.java
+ */
+
+import java.lang.annotation.Annotation;
+import java.util.*;
+import javax.annotation.processing.*;
+import javax.lang.model.element.*;
+import javax.lang.model.type.*;
+import javax.lang.model.util.*;
+import static javax.lang.model.SourceVersion.*;
+
+public class TestTypeKindVisitors extends JavacTestingAbstractProcessor {
+    @Override
+    public boolean process(Set<? extends TypeElement> tes,
+                           RoundEnvironment round) {
+        if (round.processingOver())
+            return true;
+
+        List<NoType> tradNoTypes = List.of(types.getNoType(TypeKind.NONE),
+                                           types.getNoType(TypeKind.VOID),
+                                           getPackageNoType());
+        NoType moduleNoType = getModuleNoType();
+
+        // For KindVisitors based on 6, 7, and 8
+        for (TypeVisitor<TypeKind, String> visitor : getVisitors()) {
+            System.out.println(visitor.getClass().getSuperclass().getName());
+
+            for (NoType noType : tradNoTypes) {
+                System.out.println("\t" + noType.toString());
+                checkTypeKind(noType.getKind(), visitor.visit(noType));
+            }
+
+            if (RELEASE_9.compareTo(visitor.getClass().getSuperclass().
+                                    getAnnotation(SupportedSourceVersion.class).
+                                    value()) > 0) {
+                try {
+                    System.out.println("\t" + moduleNoType.toString());
+                    visitor.visit(moduleNoType);
+                } catch (UnknownTypeException ute) {
+                    ; // Expected
+                }
+            } else {
+                checkTypeKind(moduleNoType.getKind(), visitor.visit(moduleNoType));
+            }
+        }
+
+        return true;
+    }
+
+    private NoType getPackageNoType() {
+        TypeMirror type = elements.getPackageElement("java.lang").asType();
+        checkTypeKind(TypeKind.PACKAGE, type.getKind());
+        return (NoType) type;
+    }
+
+    private NoType getModuleNoType() {
+        TypeMirror type = elements.getModuleElement("java.base").asType();
+        checkTypeKind(TypeKind.MODULE, type.getKind());
+        return (NoType) type;
+    }
+
+    private void checkTypeKind(TypeKind expected, TypeKind retreived) {
+        if (retreived != expected)
+            throw new AssertionError("Unexpected type kind " + retreived);
+    }
+
+    List<TypeVisitor<TypeKind, String>> getVisitors() {
+        return List.of(new TypeKindVisitor6<>(null) {
+                           @Override
+                           protected TypeKind defaultAction(TypeMirror e, String p) {
+                               throw new AssertionError("Should not reach");
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsVoid(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsNone(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsPackage(NoType t, String p) {
+                               return t.getKind();
+                           }
+                           // Leave default behavior for a NoType module
+                       },
+
+                       new TypeKindVisitor7<>(null){
+                           @Override
+                           protected TypeKind defaultAction(TypeMirror e, String p) {
+                               throw new AssertionError("Should not reach");
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsVoid(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsNone(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsPackage(NoType t, String p) {
+                               return t.getKind();
+                           }
+                           // Leave default behavior for a NoType module
+
+                       },
+
+                       new TypeKindVisitor8<>(null){
+                           @Override
+                           protected TypeKind defaultAction(TypeMirror e, String p) {
+                               throw new AssertionError("Should not reach");
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsVoid(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsNone(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsPackage(NoType t, String p) {
+                               return t.getKind();
+                           }
+                           // Leave default behavior for a NoType module
+
+                       },
+
+                       new TypeKindVisitor9<>(null){
+                           @Override
+                           protected TypeKind defaultAction(TypeMirror e, String p) {
+                               throw new AssertionError("Should not reach");
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsVoid(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsNone(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsPackage(NoType t, String p) {
+                               return t.getKind();
+                           }
+
+                           @Override
+                           public TypeKind visitNoTypeAsModule(NoType t, String p) {
+                               return t.getKind();
+                           }
+                       });
+    }
+}