test/tools/javac/processing/model/type/NoTypes.java

Wed, 02 Jun 2010 19:08:47 -0700

author
darcy
date
Wed, 02 Jun 2010 19:08:47 -0700
changeset 575
9a7c998bf2fc
parent 554
9d9f26857129
child 699
d2aaaec153e8
permissions
-rw-r--r--

6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2006, 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  */
    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  */
    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.*;
    40 import static javax.lang.model.type.TypeKind.*;
    42 @SupportedAnnotationTypes("*")
    43 public class NoTypes extends AbstractProcessor {
    45     Elements elements;
    46     Types types;
    48     public void init(ProcessingEnvironment penv) {
    49         super.init(penv);
    50         elements = penv.getElementUtils();
    51         types =  penv.getTypeUtils();
    52     }
    54     public boolean process(Set<? extends TypeElement> annoTypes,
    55                            RoundEnvironment round) {
    56         if (!round.processingOver())
    57             doit(annoTypes, round);
    58         return true;
    59     }
    61     @Override
    62     public SourceVersion getSupportedSourceVersion() {
    63         return SourceVersion.latest();
    64     }
    66     private void doit(Set<? extends TypeElement> annoTypes,
    67                       RoundEnvironment round) {
    69         // The superclass of Object is NONE.
    70         TypeElement object = elements.getTypeElement("java.lang.Object");
    71         verifyKind(NONE, object.getSuperclass());
    73         // The enclosing type of a top-level class is NONE
    74         verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType());
    76         // The superclass of an interface is NONE.
    77         TypeElement i = elements.getTypeElement("NoTypes.I");
    78         verifyKind(NONE, i.getSuperclass());
    80         // The type of a package is PACKAGE.
    81         Element pkg = i.getEnclosingElement().getEnclosingElement();
    82         verifyKind(PACKAGE, pkg.asType());
    84         // A package isn't enclosed.  Not yet, anyway.
    85         if (pkg.getEnclosingElement() != null)
    86             throw new AssertionError();
    88         verifyKind(VOID, types.getNoType(VOID));
    89         verifyKind(NONE, types.getNoType(NONE));
    91         // The return type of a constructor or void method is VOID.
    92         class Scanner extends ElementScanner7<Void, Void> {
    93             @Override
    94             public Void visitExecutable(ExecutableElement e, Void p) {
    95                 verifyKind(VOID, e.getReturnType());
    96                 ExecutableType t = (ExecutableType) e.asType();
    97                 verifyKind(VOID, t.getReturnType());
    98                 return null;
    99             }
   100         }
   101         TypeElement c = elements.getTypeElement("NoTypes.C");
   102         new Scanner().scan(c);
   103     }
   105     /**
   106      * Verify that a NoType instance is of a particular kind,
   107      * and that TypeKindVisitor7 properly dispatches on it.
   108      */
   109     private void verifyKind(TypeKind kind, TypeMirror type) {
   110         class Vis extends TypeKindVisitor7<TypeKind, Void> {
   111             @Override
   112             public TypeKind visitNoTypeAsVoid(NoType t, Void p) {
   113                 return VOID;
   114             }
   115             @Override
   116             public TypeKind visitNoTypeAsPackage(NoType t, Void p) {
   117                 return PACKAGE;
   118             }
   119             @Override
   120             public TypeKind visitNoTypeAsNone(NoType t, Void p) {
   121                 return NONE;
   122             }
   123         }
   124         if (kind != type.getKind() || kind != new Vis().visit(type))
   125             throw new AssertionError();
   126     }
   129     // Fodder for the tests
   131     interface I {
   132     }
   134     class C {
   135         C() {}
   136         void m() {}
   137     }
   138 }

mercurial