1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/model/type/NoTypes.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,135 @@ 1.4 +/* 1.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6418666 6423973 6453386 1.30 + * @summary Test the NoTypes: VOID, PACKAGE, NONE 1.31 + * @author Scott Seligman 1.32 + * @compile -g NoTypes.java 1.33 + * @compile -processor NoTypes -proc:only NoTypes.java 1.34 + */ 1.35 + 1.36 +import java.util.Set; 1.37 +import javax.annotation.processing.*; 1.38 +import javax.lang.model.SourceVersion; 1.39 +import javax.lang.model.element.*; 1.40 +import javax.lang.model.type.*; 1.41 +import javax.lang.model.util.*; 1.42 + 1.43 +import static javax.lang.model.type.TypeKind.*; 1.44 + 1.45 + 1.46 +@SupportedSourceVersion(SourceVersion.RELEASE_6) 1.47 +@SupportedAnnotationTypes("*") 1.48 +public class NoTypes extends AbstractProcessor { 1.49 + 1.50 + Elements elements; 1.51 + Types types; 1.52 + 1.53 + public void init(ProcessingEnvironment penv) { 1.54 + super.init(penv); 1.55 + elements = penv.getElementUtils(); 1.56 + types = penv.getTypeUtils(); 1.57 + } 1.58 + 1.59 + public boolean process(Set<? extends TypeElement> annoTypes, 1.60 + RoundEnvironment round) { 1.61 + if (!round.processingOver()) 1.62 + doit(annoTypes, round); 1.63 + return true; 1.64 + } 1.65 + 1.66 + private void doit(Set<? extends TypeElement> annoTypes, 1.67 + RoundEnvironment round) { 1.68 + 1.69 + // The superclass of Object is NONE. 1.70 + TypeElement object = elements.getTypeElement("java.lang.Object"); 1.71 + verifyKind(NONE, object.getSuperclass()); 1.72 + 1.73 + // The enclosing type of a top-level class is NONE 1.74 + verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType()); 1.75 + 1.76 + // The superclass of an interface is NONE. 1.77 + TypeElement i = elements.getTypeElement("NoTypes.I"); 1.78 + verifyKind(NONE, i.getSuperclass()); 1.79 + 1.80 + // The type of a package is PACKAGE. 1.81 + Element pkg = i.getEnclosingElement().getEnclosingElement(); 1.82 + verifyKind(PACKAGE, pkg.asType()); 1.83 + 1.84 + // A package isn't enclosed. Not yet, anyway. 1.85 + if (pkg.getEnclosingElement() != null) 1.86 + throw new AssertionError(); 1.87 + 1.88 + verifyKind(VOID, types.getNoType(VOID)); 1.89 + verifyKind(NONE, types.getNoType(NONE)); 1.90 + 1.91 + // The return type of a constructor or void method is VOID. 1.92 + class Scanner extends ElementScanner6<Void, Void> { 1.93 + @Override 1.94 + public Void visitExecutable(ExecutableElement e, Void p) { 1.95 + verifyKind(VOID, e.getReturnType()); 1.96 + ExecutableType t = (ExecutableType) e.asType(); 1.97 + verifyKind(VOID, t.getReturnType()); 1.98 + return null; 1.99 + } 1.100 + } 1.101 + TypeElement c = elements.getTypeElement("NoTypes.C"); 1.102 + new Scanner().scan(c); 1.103 + } 1.104 + 1.105 + /** 1.106 + * Verify that a NoType instance is of a particular kind, 1.107 + * and that TypeKindVisitor6 properly dispatches on it. 1.108 + */ 1.109 + private void verifyKind(TypeKind kind, TypeMirror type) { 1.110 + class Vis extends TypeKindVisitor6<TypeKind, Void> { 1.111 + @Override 1.112 + public TypeKind visitNoTypeAsVoid(NoType t, Void p) { 1.113 + return VOID; 1.114 + } 1.115 + @Override 1.116 + public TypeKind visitNoTypeAsPackage(NoType t, Void p) { 1.117 + return PACKAGE; 1.118 + } 1.119 + @Override 1.120 + public TypeKind visitNoTypeAsNone(NoType t, Void p) { 1.121 + return NONE; 1.122 + } 1.123 + } 1.124 + if (kind != type.getKind() || kind != new Vis().visit(type)) 1.125 + throw new AssertionError(); 1.126 + } 1.127 + 1.128 + 1.129 + // Fodder for the tests 1.130 + 1.131 + interface I { 1.132 + } 1.133 + 1.134 + class C { 1.135 + C() {} 1.136 + void m() {} 1.137 + } 1.138 +}