1 /*
   2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     6418666 6423973 6453386
  27  * @summary Test the NoTypes: VOID, PACKAGE, NONE
  28  * @author  Scott Seligman
  29  * @compile -g NoTypes.java
  30  * @compile -processor NoTypes -proc:only NoTypes.java
  31  */
  32 
  33 import java.util.Set;
  34 import javax.annotation.processing.*;
  35 import javax.lang.model.SourceVersion;
  36 import javax.lang.model.element.*;
  37 import javax.lang.model.type.*;
  38 import javax.lang.model.util.*;
  39 
  40 import static javax.lang.model.type.TypeKind.*;
  41 
  42 
  43 @SupportedSourceVersion(SourceVersion.RELEASE_6)
  44 @SupportedAnnotationTypes("*")
  45 public class NoTypes extends AbstractProcessor {
  46 
  47     Elements elements;
  48     Types types;
  49 
  50     public void init(ProcessingEnvironment penv) {
  51         super.init(penv);
  52         elements = penv.getElementUtils();
  53         types =  penv.getTypeUtils();
  54     }
  55 
  56     public boolean process(Set<? extends TypeElement> annoTypes,
  57                            RoundEnvironment round) {
  58         if (!round.processingOver())
  59             doit(annoTypes, round);
  60         return true;
  61     }
  62 
  63     private void doit(Set<? extends TypeElement> annoTypes,
  64                       RoundEnvironment round) {
  65 
  66         // The superclass of Object is NONE.
  67         TypeElement object = elements.getTypeElement("java.lang.Object");
  68         verifyKind(NONE, object.getSuperclass());
  69 
  70         // The enclosing type of a top-level class is NONE
  71         verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType());
  72 
  73         // The superclass of an interface is NONE.
  74         TypeElement i = elements.getTypeElement("NoTypes.I");
  75         verifyKind(NONE, i.getSuperclass());
  76 
  77         // The type of a package is PACKAGE.
  78         Element pkg = i.getEnclosingElement().getEnclosingElement();
  79         verifyKind(PACKAGE, pkg.asType());
  80 
  81         // A package isn't enclosed.  Not yet, anyway.
  82         if (pkg.getEnclosingElement() != null)
  83             throw new AssertionError();
  84 
  85         verifyKind(VOID, types.getNoType(VOID));
  86         verifyKind(NONE, types.getNoType(NONE));
  87 
  88         // The return type of a constructor or void method is VOID.
  89         class Scanner extends ElementScanner6<Void, Void> {
  90             @Override
  91             public Void visitExecutable(ExecutableElement e, Void p) {
  92                 verifyKind(VOID, e.getReturnType());
  93                 ExecutableType t = (ExecutableType) e.asType();
  94                 verifyKind(VOID, t.getReturnType());
  95                 return null;
  96             }
  97         }
  98         TypeElement c = elements.getTypeElement("NoTypes.C");
  99         new Scanner().scan(c);
 100     }
 101 
 102     /**
 103      * Verify that a NoType instance is of a particular kind,
 104      * and that TypeKindVisitor6 properly dispatches on it.
 105      */
 106     private void verifyKind(TypeKind kind, TypeMirror type) {
 107         class Vis extends TypeKindVisitor6<TypeKind, Void> {
 108             @Override
 109             public TypeKind visitNoTypeAsVoid(NoType t, Void p) {
 110                 return VOID;
 111             }
 112             @Override
 113             public TypeKind visitNoTypeAsPackage(NoType t, Void p) {
 114                 return PACKAGE;
 115             }
 116             @Override
 117             public TypeKind visitNoTypeAsNone(NoType t, Void p) {
 118                 return NONE;
 119             }
 120         }
 121         if (kind != type.getKind() || kind != new Vis().visit(type))
 122             throw new AssertionError();
 123     }
 124 
 125 
 126     // Fodder for the tests
 127 
 128     interface I {
 129     }
 130 
 131     class C {
 132         C() {}
 133         void m() {}
 134     }
 135 }