NoTypes.java revision 1465:b52a38d4536c
1284345Ssjg/*
2284345Ssjg * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3284345Ssjg * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4284345Ssjg *
5284345Ssjg * This code is free software; you can redistribute it and/or modify it
6284345Ssjg * under the terms of the GNU General Public License version 2 only, as
7284345Ssjg * published by the Free Software Foundation.
8284345Ssjg *
9284345Ssjg * This code is distributed in the hope that it will be useful, but WITHOUT
10284345Ssjg * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11284345Ssjg * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12284345Ssjg * version 2 for more details (a copy is included in the LICENSE file that
13284345Ssjg * accompanied this code).
14284345Ssjg *
15284345Ssjg * You should have received a copy of the GNU General Public License version
16284345Ssjg * 2 along with this work; if not, write to the Free Software Foundation,
17284345Ssjg * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18284345Ssjg *
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 /tools/javac/lib
30 * @build JavacTestingAbstractProcessor
31 * @compile -g NoTypes.java
32 * @compile -processor NoTypes -proc:only NoTypes.java
33 */
34
35import java.util.Set;
36import javax.annotation.processing.*;
37import javax.lang.model.SourceVersion;
38import javax.lang.model.element.*;
39import javax.lang.model.type.*;
40import javax.lang.model.util.*;
41
42import static javax.lang.model.type.TypeKind.*;
43
44public class NoTypes extends JavacTestingAbstractProcessor {
45    public boolean process(Set<? extends TypeElement> annoTypes,
46                           RoundEnvironment round) {
47        if (!round.processingOver())
48            doit(annoTypes, round);
49        return true;
50    }
51
52    private void doit(Set<? extends TypeElement> annoTypes,
53                      RoundEnvironment round) {
54
55        // The superclass of Object is NONE.
56        TypeElement object = elements.getTypeElement("java.lang.Object");
57        verifyKind(NONE, object.getSuperclass());
58
59        // The enclosing type of a top-level class is NONE
60        verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType());
61
62        // The superclass of an interface is NONE.
63        TypeElement i = elements.getTypeElement("NoTypes.I");
64        verifyKind(NONE, i.getSuperclass());
65
66        // The type of a package is PACKAGE.
67        Element pkg = i.getEnclosingElement().getEnclosingElement();
68        verifyKind(PACKAGE, pkg.asType());
69
70        // A package isn't enclosed.  Not yet, anyway.
71        if (pkg.getEnclosingElement() != null)
72            throw new AssertionError();
73
74        verifyKind(VOID, types.getNoType(VOID));
75        verifyKind(NONE, types.getNoType(NONE));
76
77        // The return type of a constructor or void method is VOID.
78        class Scanner extends ElementScanner<Void, Void> {
79            @Override
80            public Void visitExecutable(ExecutableElement e, Void p) {
81                verifyKind(VOID, e.getReturnType());
82                ExecutableType t = (ExecutableType) e.asType();
83                verifyKind(VOID, t.getReturnType());
84                return null;
85            }
86        }
87        TypeElement c = elements.getTypeElement("NoTypes.C");
88        new Scanner().scan(c);
89    }
90
91    /**
92     * Verify that a NoType instance is of a particular kind, and that
93     * the latest TypeKindVisitor properly dispatches on it.
94     */
95    private void verifyKind(TypeKind kind, TypeMirror type) {
96        class Vis extends TypeKindVisitor<TypeKind, Void> {
97            @Override
98            public TypeKind visitNoTypeAsVoid(NoType t, Void p) {
99                return VOID;
100            }
101            @Override
102            public TypeKind visitNoTypeAsPackage(NoType t, Void p) {
103                return PACKAGE;
104            }
105            @Override
106            public TypeKind visitNoTypeAsNone(NoType t, Void p) {
107                return NONE;
108            }
109        }
110        if (kind != type.getKind() || kind != new Vis().visit(type))
111            throw new AssertionError();
112    }
113
114    // Fodder for the tests
115    interface I {
116    }
117
118    class C {
119        C() {}
120        void m() {}
121    }
122}
123