1 /*
   2  * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     8191234
  27  * @summary Test TypeKind visitors on pseudo types.
  28  * @library /tools/javac/lib
  29  * @modules java.compiler
  30  * @build   JavacTestingAbstractProcessor TestTypeKindVisitors
  31  * @compile -processor TestTypeKindVisitors -proc:only TestTypeKindVisitors.java
  32  */
  33 
  34 import java.lang.annotation.Annotation;
  35 import java.util.*;
  36 import javax.annotation.processing.*;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.type.*;
  39 import javax.lang.model.util.*;
  40 import static javax.lang.model.SourceVersion.*;
  41 
  42 public class TestTypeKindVisitors extends JavacTestingAbstractProcessor {
  43     @Override
  44     public boolean process(Set<? extends TypeElement> tes,
  45                            RoundEnvironment round) {
  46         if (round.processingOver())
  47             return true;
  48 
  49         List<NoType> tradNoTypes = List.of(types.getNoType(TypeKind.NONE),
  50                                            types.getNoType(TypeKind.VOID),
  51                                            getPackageNoType());
  52         NoType moduleNoType = getModuleNoType();
  53 
  54         // For KindVisitors based on 6, 7, and 8
  55         for (TypeVisitor<TypeKind, String> visitor : getVisitors()) {
  56             System.out.println(visitor.getClass().getSuperclass().getName());
  57 
  58             for (NoType noType : tradNoTypes) {
  59                 System.out.println("\t" + noType.toString());
  60                 checkTypeKind(noType.getKind(), visitor.visit(noType));
  61             }
  62 
  63             if (RELEASE_9.compareTo(visitor.getClass().getSuperclass().
  64                                     getAnnotation(SupportedSourceVersion.class).
  65                                     value()) > 0) {
  66                 try {
  67                     System.out.println("\t" + moduleNoType.toString());
  68                     visitor.visit(moduleNoType);
  69                 } catch (UnknownTypeException ute) {
  70                     ; // Expected
  71                 }
  72             } else {
  73                 checkTypeKind(moduleNoType.getKind(), visitor.visit(moduleNoType));
  74             }
  75         }
  76 
  77         return true;
  78     }
  79 
  80     private NoType getPackageNoType() {
  81         TypeMirror type = elements.getPackageElement("java.lang").asType();
  82         checkTypeKind(TypeKind.PACKAGE, type.getKind());
  83         return (NoType) type;
  84     }
  85 
  86     private NoType getModuleNoType() {
  87         TypeMirror type = elements.getModuleElement("java.base").asType();
  88         checkTypeKind(TypeKind.MODULE, type.getKind());
  89         return (NoType) type;
  90     }
  91 
  92     private void checkTypeKind(TypeKind expected, TypeKind retreived) {
  93         if (retreived != expected)
  94             throw new AssertionError("Unexpected type kind " + retreived);
  95     }
  96 
  97     List<TypeVisitor<TypeKind, String>> getVisitors() {
  98         return List.of(new TypeKindVisitor6<>(null) {
  99                            @Override
 100                            protected TypeKind defaultAction(TypeMirror e, String p) {
 101                                throw new AssertionError("Should not reach");
 102                            }
 103 
 104                            @Override
 105                            public TypeKind visitNoTypeAsVoid(NoType t, String p) {
 106                                return t.getKind();
 107                            }
 108 
 109                            @Override
 110                            public TypeKind visitNoTypeAsNone(NoType t, String p) {
 111                                return t.getKind();
 112                            }
 113 
 114                            @Override
 115                            public TypeKind visitNoTypeAsPackage(NoType t, String p) {
 116                                return t.getKind();
 117                            }
 118                            // Leave default behavior for a NoType module
 119                        },
 120 
 121                        new TypeKindVisitor7<>(null){
 122                            @Override
 123                            protected TypeKind defaultAction(TypeMirror e, String p) {
 124                                throw new AssertionError("Should not reach");
 125                            }
 126 
 127                            @Override
 128                            public TypeKind visitNoTypeAsVoid(NoType t, String p) {
 129                                return t.getKind();
 130                            }
 131 
 132                            @Override
 133                            public TypeKind visitNoTypeAsNone(NoType t, String p) {
 134                                return t.getKind();
 135                            }
 136 
 137                            @Override
 138                            public TypeKind visitNoTypeAsPackage(NoType t, String p) {
 139                                return t.getKind();
 140                            }
 141                            // Leave default behavior for a NoType module
 142                         
 143                        },
 144 
 145                        new TypeKindVisitor8<>(null){
 146                            @Override
 147                            protected TypeKind defaultAction(TypeMirror e, String p) {
 148                                throw new AssertionError("Should not reach");
 149                            }
 150 
 151                            @Override
 152                            public TypeKind visitNoTypeAsVoid(NoType t, String p) {
 153                                return t.getKind();
 154                            }
 155 
 156                            @Override
 157                            public TypeKind visitNoTypeAsNone(NoType t, String p) {
 158                                return t.getKind();
 159                            }
 160 
 161                            @Override
 162                            public TypeKind visitNoTypeAsPackage(NoType t, String p) {
 163                                return t.getKind();
 164                            }
 165                            // Leave default behavior for a NoType module
 166 
 167                        },
 168 
 169                        new TypeKindVisitor9<>(null){
 170                            @Override
 171                            protected TypeKind defaultAction(TypeMirror e, String p) {
 172                                throw new AssertionError("Should not reach");
 173                            }
 174 
 175                            @Override
 176                            public TypeKind visitNoTypeAsVoid(NoType t, String p) {
 177                                return t.getKind();
 178                            }
 179 
 180                            @Override
 181                            public TypeKind visitNoTypeAsNone(NoType t, String p) {
 182                                return t.getKind();
 183                            }
 184 
 185                            @Override
 186                            public TypeKind visitNoTypeAsPackage(NoType t, String p) {
 187                                return t.getKind();
 188                            }
 189 
 190                            @Override
 191                            public TypeKind visitNoTypeAsModule(NoType t, String p) {
 192                                return t.getKind();
 193                            }
 194                        });
 195     }
 196 }