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

Tue, 14 May 2013 15:04:06 -0700

author
jjg
date
Tue, 14 May 2013 15:04:06 -0700
changeset 1755
ddb4a2bfcd82
parent 1466
b52a38d4536c
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8013852: update reference impl for type-annotations
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com, steve.sides@oracle.com, joel.franck@oracle.com, alex.buckley@oracle.com

duke@1 1 /*
darcy@1054 2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
duke@1 22 */
duke@1 23
duke@1 24 /*
duke@1 25 * @test
darcy@1054 26 * @bug 6418666 6423973 6453386 7025809
duke@1 27 * @summary Test the NoTypes: VOID, PACKAGE, NONE
duke@1 28 * @author Scott Seligman
darcy@1466 29 * @library /tools/javac/lib
darcy@699 30 * @build JavacTestingAbstractProcessor
duke@1 31 * @compile -g NoTypes.java
duke@1 32 * @compile -processor NoTypes -proc:only NoTypes.java
duke@1 33 */
duke@1 34
duke@1 35 import java.util.Set;
duke@1 36 import javax.annotation.processing.*;
duke@1 37 import javax.lang.model.SourceVersion;
duke@1 38 import javax.lang.model.element.*;
duke@1 39 import javax.lang.model.type.*;
duke@1 40 import javax.lang.model.util.*;
duke@1 41
duke@1 42 import static javax.lang.model.type.TypeKind.*;
duke@1 43
darcy@699 44 public class NoTypes extends JavacTestingAbstractProcessor {
duke@1 45 public boolean process(Set<? extends TypeElement> annoTypes,
duke@1 46 RoundEnvironment round) {
duke@1 47 if (!round.processingOver())
duke@1 48 doit(annoTypes, round);
duke@1 49 return true;
duke@1 50 }
duke@1 51
duke@1 52 private void doit(Set<? extends TypeElement> annoTypes,
duke@1 53 RoundEnvironment round) {
duke@1 54
duke@1 55 // The superclass of Object is NONE.
duke@1 56 TypeElement object = elements.getTypeElement("java.lang.Object");
duke@1 57 verifyKind(NONE, object.getSuperclass());
duke@1 58
duke@1 59 // The enclosing type of a top-level class is NONE
duke@1 60 verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType());
duke@1 61
duke@1 62 // The superclass of an interface is NONE.
duke@1 63 TypeElement i = elements.getTypeElement("NoTypes.I");
duke@1 64 verifyKind(NONE, i.getSuperclass());
duke@1 65
duke@1 66 // The type of a package is PACKAGE.
duke@1 67 Element pkg = i.getEnclosingElement().getEnclosingElement();
duke@1 68 verifyKind(PACKAGE, pkg.asType());
duke@1 69
duke@1 70 // A package isn't enclosed. Not yet, anyway.
duke@1 71 if (pkg.getEnclosingElement() != null)
duke@1 72 throw new AssertionError();
duke@1 73
duke@1 74 verifyKind(VOID, types.getNoType(VOID));
duke@1 75 verifyKind(NONE, types.getNoType(NONE));
duke@1 76
duke@1 77 // The return type of a constructor or void method is VOID.
darcy@1054 78 class Scanner extends ElementScanner<Void, Void> {
duke@1 79 @Override
duke@1 80 public Void visitExecutable(ExecutableElement e, Void p) {
duke@1 81 verifyKind(VOID, e.getReturnType());
duke@1 82 ExecutableType t = (ExecutableType) e.asType();
duke@1 83 verifyKind(VOID, t.getReturnType());
duke@1 84 return null;
duke@1 85 }
duke@1 86 }
duke@1 87 TypeElement c = elements.getTypeElement("NoTypes.C");
duke@1 88 new Scanner().scan(c);
duke@1 89 }
duke@1 90
duke@1 91 /**
darcy@1054 92 * Verify that a NoType instance is of a particular kind, and that
darcy@1054 93 * the latest TypeKindVisitor properly dispatches on it.
duke@1 94 */
duke@1 95 private void verifyKind(TypeKind kind, TypeMirror type) {
darcy@1054 96 class Vis extends TypeKindVisitor<TypeKind, Void> {
duke@1 97 @Override
duke@1 98 public TypeKind visitNoTypeAsVoid(NoType t, Void p) {
duke@1 99 return VOID;
duke@1 100 }
duke@1 101 @Override
duke@1 102 public TypeKind visitNoTypeAsPackage(NoType t, Void p) {
duke@1 103 return PACKAGE;
duke@1 104 }
duke@1 105 @Override
duke@1 106 public TypeKind visitNoTypeAsNone(NoType t, Void p) {
duke@1 107 return NONE;
duke@1 108 }
duke@1 109 }
duke@1 110 if (kind != type.getKind() || kind != new Vis().visit(type))
duke@1 111 throw new AssertionError();
duke@1 112 }
duke@1 113
duke@1 114 // Fodder for the tests
duke@1 115 interface I {
duke@1 116 }
duke@1 117
duke@1 118 class C {
duke@1 119 C() {}
duke@1 120 void m() {}
duke@1 121 }
duke@1 122 }

mercurial