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