NoTypes.java revision 494:fe17a9dbef03
1181027Sjfv/* 2169240Sjfv * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 3269196Sjfv * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4169240Sjfv * 5169240Sjfv * This code is free software; you can redistribute it and/or modify it 6169240Sjfv * under the terms of the GNU General Public License version 2 only, as 7169240Sjfv * published by the Free Software Foundation. 8169240Sjfv * 9169240Sjfv * This code is distributed in the hope that it will be useful, but WITHOUT 10169240Sjfv * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11169240Sjfv * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12169240Sjfv * version 2 for more details (a copy is included in the LICENSE file that 13169240Sjfv * accompanied this code). 14169240Sjfv * 15169240Sjfv * You should have received a copy of the GNU General Public License version 16169240Sjfv * 2 along with this work; if not, write to the Free Software Foundation, 17169240Sjfv * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18169240Sjfv * 19169240Sjfv * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20169240Sjfv * CA 95054 USA or visit www.sun.com if you need additional information or 21169240Sjfv * have any questions. 22169240Sjfv */ 23169240Sjfv 24169240Sjfv/* 25169240Sjfv * @test 26169240Sjfv * @bug 6418666 6423973 6453386 27169240Sjfv * @summary Test the NoTypes: VOID, PACKAGE, NONE 28169240Sjfv * @author Scott Seligman 29169240Sjfv * @compile -g NoTypes.java 30169240Sjfv * @compile -processor NoTypes -proc:only NoTypes.java 31169240Sjfv */ 32181027Sjfv 33181027Sjfvimport java.util.Set; 34169240Sjfvimport javax.annotation.processing.*; 35169240Sjfvimport javax.lang.model.SourceVersion; 36169240Sjfvimport javax.lang.model.element.*; 37169240Sjfvimport javax.lang.model.type.*; 38228386Sjfvimport javax.lang.model.util.*; 39228386Sjfv 40228386Sjfvimport static javax.lang.model.type.TypeKind.*; 41228386Sjfv 42228386Sjfv@SupportedAnnotationTypes("*") 43228386Sjfvpublic class NoTypes extends AbstractProcessor { 44228386Sjfv 45228386Sjfv Elements elements; 46228386Sjfv Types types; 47228386Sjfv 48228386Sjfv public void init(ProcessingEnvironment penv) { 49228386Sjfv super.init(penv); 50228386Sjfv elements = penv.getElementUtils(); 51228386Sjfv types = penv.getTypeUtils(); 52228386Sjfv } 53256200Sjfv 54256200Sjfv public boolean process(Set<? extends TypeElement> annoTypes, 55256200Sjfv RoundEnvironment round) { 56256200Sjfv if (!round.processingOver()) 57228386Sjfv doit(annoTypes, round); 58238148Sjfv return true; 59228386Sjfv } 60228386Sjfv 61228386Sjfv @Override 62269196Sjfv public SourceVersion getSupportedSourceVersion() { 63247064Sjfv return SourceVersion.latest(); 64247064Sjfv } 65228386Sjfv 66247064Sjfv private void doit(Set<? extends TypeElement> annoTypes, 67247064Sjfv RoundEnvironment round) { 68228386Sjfv 69228386Sjfv // The superclass of Object is NONE. 70228386Sjfv TypeElement object = elements.getTypeElement("java.lang.Object"); 71228386Sjfv verifyKind(NONE, object.getSuperclass()); 72228386Sjfv 73228386Sjfv // The enclosing type of a top-level class is NONE 74228386Sjfv verifyKind(NONE, ((DeclaredType)object.asType()).getEnclosingType()); 75228386Sjfv 76228386Sjfv // The superclass of an interface is NONE. 77228386Sjfv TypeElement i = elements.getTypeElement("NoTypes.I"); 78228386Sjfv verifyKind(NONE, i.getSuperclass()); 79228386Sjfv 80247064Sjfv // The type of a package is PACKAGE. 81228386Sjfv Element pkg = i.getEnclosingElement().getEnclosingElement(); 82228386Sjfv verifyKind(PACKAGE, pkg.asType()); 83228386Sjfv 84228386Sjfv // A package isn't enclosed. Not yet, anyway. 85228386Sjfv if (pkg.getEnclosingElement() != null) 86228386Sjfv throw new AssertionError(); 87228386Sjfv 88228386Sjfv verifyKind(VOID, types.getNoType(VOID)); 89228386Sjfv verifyKind(NONE, types.getNoType(NONE)); 90228386Sjfv 91228386Sjfv // The return type of a constructor or void method is VOID. 92228386Sjfv class Scanner extends ElementScanner6<Void, Void> { 93228386Sjfv @Override 94228386Sjfv public Void visitExecutable(ExecutableElement e, Void p) { 95228386Sjfv verifyKind(VOID, e.getReturnType()); 96228386Sjfv ExecutableType t = (ExecutableType) e.asType(); 97228386Sjfv verifyKind(VOID, t.getReturnType()); 98228386Sjfv return null; 99228386Sjfv } 100228386Sjfv } 101228386Sjfv TypeElement c = elements.getTypeElement("NoTypes.C"); 102256200Sjfv new Scanner().scan(c); 103228386Sjfv } 104228386Sjfv 105228386Sjfv /** 106228386Sjfv * Verify that a NoType instance is of a particular kind, 107228386Sjfv * and that TypeKindVisitor6 properly dispatches on it. 108228386Sjfv */ 109247064Sjfv private void verifyKind(TypeKind kind, TypeMirror type) { 110228386Sjfv class Vis extends TypeKindVisitor6<TypeKind, Void> { 111228386Sjfv @Override 112269196Sjfv public TypeKind visitNoTypeAsVoid(NoType t, Void p) { 113228386Sjfv return VOID; 114228386Sjfv } 115228386Sjfv @Override 116228386Sjfv public TypeKind visitNoTypeAsPackage(NoType t, Void p) { 117228386Sjfv return PACKAGE; 118228386Sjfv } 119228386Sjfv @Override 120228386Sjfv public TypeKind visitNoTypeAsNone(NoType t, Void p) { 121228386Sjfv return NONE; 122228386Sjfv } 123228386Sjfv } 124228386Sjfv if (kind != type.getKind() || kind != new Vis().visit(type)) 125228386Sjfv throw new AssertionError(); 126228386Sjfv } 127228386Sjfv 128228386Sjfv 129228386Sjfv // Fodder for the tests 130228386Sjfv 131228386Sjfv interface I { 132228386Sjfv } 133228386Sjfv 134228386Sjfv class C { 135228386Sjfv C() {} 136228386Sjfv void m() {} 137228386Sjfv } 138228386Sjfv} 139228386Sjfv