Processor.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2013, 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 24import java.io.IOException; 25import java.io.Writer; 26import java.lang.annotation.*; 27import java.util.Set; 28import javax.annotation.processing.*; 29import javax.lang.model.element.*; 30import javax.lang.model.util.ElementFilter; 31import javax.tools.*; 32import com.sun.tools.javac.util.Assert; 33 34public class Processor extends JavacTestingAbstractProcessor { 35 private boolean seenGenerated; 36 37 @Override 38 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 39 for (Element e : roundEnv.getElementsAnnotatedWith(Gen.class)) { 40 Gen gen = e.getAnnotation(Gen.class); 41 try { 42 JavaFileObject source = processingEnv.getFiler().createSourceFile(gen.fileName()); 43 44 try (Writer out = source.openWriter()) { 45 out.write(gen.content()); 46 } 47 } catch (IOException ex) { 48 throw new IllegalStateException(ex); 49 } 50 } 51 52 TypeElement generated = processingEnv.getElementUtils().getTypeElement("Generated"); 53 54 if (generated != null) { 55 Check check = ElementFilter.methodsIn(generated.getEnclosedElements()).get(0).getAnnotation(Check.class); 56 57 checkCorrectException(check::classValue, "java.lang.Class<java.lang.String>"); 58 checkCorrectException(check::intConstValue, "boolean"); 59 checkCorrectException(check::enumValue, "java.lang.String"); 60 checkCorrectException(check::incorrectAnnotationValue, "java.lang.Deprecated"); 61 checkCorrectException(check::incorrectArrayValue, "<any>"); 62 checkCorrectException(check::incorrectClassValue, "<any>"); 63 64 seenGenerated = true; 65 } 66 67 if (roundEnv.processingOver() && !seenGenerated) { 68 Assert.error("Did not see the generated class!"); 69 } 70 71 return true; 72 } 73 74 private static void checkCorrectException(Runnable runCheck, String expectedType) { 75 try { 76 runCheck.run(); 77 Assert.check(false); //Should not reach here 78 } catch (AnnotationTypeMismatchException ex) { 79 Assert.check(expectedType.equals(ex.foundType()), ex.foundType()); 80 } 81 } 82 83} 84 85@interface Gen { 86 String fileName(); 87 String content(); 88} 89 90@interface Check { 91 Class<? extends Number> classValue(); 92 int intConstValue(); 93 E enumValue(); 94 int incorrectAnnotationValue(); 95 int incorrectArrayValue(); 96 Class<?> incorrectClassValue(); 97} 98 99enum E { 100 A; 101} 102