1 /* 2 * Copyright (c) 2013, 2018, 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 package org.graalvm.compiler.replacements.processor; 26 27 import java.util.ArrayList; 28 import java.util.HashSet; 29 import java.util.List; 30 import java.util.Set; 31 32 import javax.annotation.processing.RoundEnvironment; 33 import javax.lang.model.SourceVersion; 34 import javax.lang.model.element.AnnotationMirror; 35 import javax.lang.model.element.Element; 36 import javax.lang.model.element.TypeElement; 37 import javax.tools.Diagnostic.Kind; 38 39 import org.graalvm.compiler.processor.AbstractProcessor; 40 41 /** 42 * Processor for annotation types in the {@code org.graalvm.compiler.replacements} name space. 43 */ 44 public class ReplacementsAnnotationProcessor extends AbstractProcessor { 45 46 private List<AnnotationHandler> handlers; 47 48 @Override 49 public SourceVersion getSupportedSourceVersion() { 50 return SourceVersion.latest(); 51 } 52 53 @Override 54 public boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 55 if (!roundEnv.processingOver()) { 56 PluginGenerator generator = new PluginGenerator(); 57 for (AnnotationHandler handler : getHandlers()) { 58 TypeElement annotationClass = getTypeElementOrNull(handler.annotationTypeName); 59 if (annotationClass != null) { 60 for (Element e : roundEnv.getElementsAnnotatedWith(annotationClass)) { 61 AnnotationMirror annotationMirror = getAnnotation(e, annotationClass.asType()); 62 handler.process(e, annotationMirror, generator); 63 } 64 } else { 65 Set<? extends Element> roots = roundEnv.getRootElements(); 66 String message = String.format("Processor %s disabled as %s is not resolvable on the compilation class path", handler.getClass().getName(), handler.annotationTypeName); 67 if (roots.isEmpty()) { 68 env().getMessager().printMessage(Kind.WARNING, message); 69 } else { 70 env().getMessager().printMessage(Kind.WARNING, message, roots.iterator().next()); 71 } 72 } 73 } 74 generator.generateAll(this); 75 } 76 return false; 77 } 78 79 public List<AnnotationHandler> getHandlers() { 80 if (handlers == null) { 81 handlers = new ArrayList<>(); 82 handlers.add(new ClassSubstitutionHandler(this)); 83 handlers.add(new MethodSubstitutionHandler(this)); 84 handlers.add(new NodeIntrinsicHandler(this)); 85 handlers.add(new FoldHandler(this)); 86 } 87 return handlers; 88 } 89 90 @Override 91 public Set<String> getSupportedAnnotationTypes() { 92 Set<String> annotationTypes = new HashSet<>(); 93 for (AnnotationHandler handler : getHandlers()) { 94 annotationTypes.add(handler.annotationTypeName); 95 } 96 return annotationTypes; 97 } 98 }