1 /*
   2  * Copyright (c) 2006, 2011, 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     6418666 6423973 6453386 7025809
  27  * @summary Test the NoTypes: VOID, PACKAGE, NONE
  28  * @author  Scott Seligman
  29  * @library ../../../lib
  30  * @build JavacTestingAbstractProcessor
  31  * @compile -g NoTypes.java
  32  * @compile -processor NoTypes -proc:only NoTypes.java
  33  */
  34 
  35 import java.util.Set;
  36 import javax.annotation.processing.*;
  37 import javax.lang.model.SourceVersion;
  38 import javax.lang.model.element.*;
  39 import javax.lang.model.type.*;
  40 import javax.lang.model.util.*;
  41 
  42 import static javax.lang.model.type.TypeKind.*;
  43 import static JavacTestingAbstractProcessor.*;
  44 
  45 public class NoTypes extends JavacTestingAbstractProcessor {
  46     public boolean process(Set<? extends TypeElement> annoTypes,
  47                            RoundEnvironment round) {
  48         if (!round.processingOver())
  49             doit(annoTypes, round);
  50         return true;
  51     }
  52 
  53     private void doit(Set<? extends TypeElement> annoTypes,
  54                       RoundEnvironment round) {
  55 
  56         // The superclass of Object is NONE.
  57         TypeElement object = elements.getTypeElement("java.lang.Object");
  58         verifyKind(NONE, object.getSuperclass());
  59 
  60         // The enclosing type of a top-level class is NONE
  61         verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType());
  62 
  63         // The superclass of an interface is NONE.
  64         TypeElement i = elements.getTypeElement("NoTypes.I");
  65         verifyKind(NONE, i.getSuperclass());
  66 
  67         // The type of a package is PACKAGE.
  68         Element pkg = i.getEnclosingElement().getEnclosingElement();
  69         verifyKind(PACKAGE, pkg.asType());
  70 
  71         // A package isn't enclosed.  Not yet, anyway.
  72         if (pkg.getEnclosingElement() != null)
  73             throw new AssertionError();
  74 
  75         verifyKind(VOID, types.getNoType(VOID));
  76         verifyKind(NONE, types.getNoType(NONE));
  77 
  78         // The return type of a constructor or void method is VOID.
  79         class Scanner extends ElementScanner<Void, Void> {
  80             @Override
  81             public Void visitExecutable(ExecutableElement e, Void p) {
  82                 verifyKind(VOID, e.getReturnType());
  83                 ExecutableType t = (ExecutableType) e.asType();
  84                 verifyKind(VOID, t.getReturnType());
  85                 return null;
  86             }
  87         }
  88         TypeElement c = elements.getTypeElement("NoTypes.C");
  89         new Scanner().scan(c);
  90     }
  91 
  92     /**
  93      * Verify that a NoType instance is of a particular kind, and that
  94      * the latest TypeKindVisitor properly dispatches on it.
  95      */
  96     private void verifyKind(TypeKind kind, TypeMirror type) {
  97         class Vis extends TypeKindVisitor<TypeKind, Void> {
  98             @Override
  99             public TypeKind visitNoTypeAsVoid(NoType t, Void p) {
 100                 return VOID;
 101             }
 102             @Override
 103             public TypeKind visitNoTypeAsPackage(NoType t, Void p) {
 104                 return PACKAGE;
 105             }
 106             @Override
 107             public TypeKind visitNoTypeAsNone(NoType t, Void p) {
 108                 return NONE;
 109             }
 110         }
 111         if (kind != type.getKind() || kind != new Vis().visit(type))
 112             throw new AssertionError();
 113     }
 114 
 115     // Fodder for the tests
 116     interface I {
 117     }
 118 
 119     class C {
 120         C() {}
 121         void m() {}
 122     }
 123 }