1 /*
   2  * Copyright (c) 2013, 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 package org.graalvm.compiler.serviceprovider.processor;
  24 
  25 import java.io.IOException;
  26 import java.io.OutputStreamWriter;
  27 import java.io.PrintWriter;
  28 import java.util.HashSet;
  29 import java.util.Set;
  30 
  31 import javax.annotation.processing.AbstractProcessor;
  32 import javax.annotation.processing.FilerException;
  33 import javax.annotation.processing.RoundEnvironment;
  34 import javax.annotation.processing.SupportedAnnotationTypes;
  35 import javax.lang.model.SourceVersion;
  36 import javax.lang.model.element.Element;
  37 import javax.lang.model.element.TypeElement;
  38 import javax.lang.model.type.MirroredTypeException;
  39 import javax.lang.model.type.TypeMirror;
  40 import javax.tools.Diagnostic.Kind;
  41 import javax.tools.FileObject;
  42 import javax.tools.StandardLocation;
  43 
  44 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  45 
  46 /**
  47  * Processes classes annotated with {@link ServiceProvider}. For a service defined by {@code S} and
  48  * a class {@code P} implementing the service, this processor generates the file
  49  * {@code META-INF/providers/P} whose contents are a single line containing the fully qualified name
  50  * of {@code S}.
  51  */
  52 @SupportedAnnotationTypes("org.graalvm.compiler.serviceprovider.ServiceProvider")
  53 public class ServiceProviderProcessor extends AbstractProcessor {
  54 
  55     private final Set<TypeElement> processed = new HashSet<>();
  56 
  57     @Override
  58     public SourceVersion getSupportedSourceVersion() {
  59         return SourceVersion.latest();
  60     }
  61 
  62     private boolean verifyAnnotation(TypeMirror serviceInterface, TypeElement serviceProvider) {
  63         if (!processingEnv.getTypeUtils().isSubtype(serviceProvider.asType(), serviceInterface)) {
  64             String msg = String.format("Service provider class %s must implement service interface %s", serviceProvider.getSimpleName(), serviceInterface);
  65             processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
  66             return false;
  67         }
  68 
  69         return true;
  70     }
  71 
  72     private void processElement(TypeElement serviceProvider) {
  73         if (processed.contains(serviceProvider)) {
  74             return;
  75         }
  76 
  77         processed.add(serviceProvider);
  78         ServiceProvider annotation = serviceProvider.getAnnotation(ServiceProvider.class);
  79         if (annotation != null) {
  80             try {
  81                 annotation.value();
  82             } catch (MirroredTypeException ex) {
  83                 TypeMirror serviceInterface = ex.getTypeMirror();
  84                 if (verifyAnnotation(serviceInterface, serviceProvider)) {
  85                     String interfaceName = ex.getTypeMirror().toString();
  86                     createProviderFile(serviceProvider, interfaceName);
  87                 }
  88             }
  89         }
  90     }
  91 
  92     private void createProviderFile(TypeElement serviceProvider, String interfaceName) {
  93         if (serviceProvider.getNestingKind().isNested()) {
  94             // This is a simplifying constraint that means we don't have to
  95             // processed the qualified name to insert '$' characters at
  96             // the relevant positions.
  97             String msg = String.format("Service provider class %s must be a top level class", serviceProvider.getSimpleName());
  98             processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
  99             return;
 100         }
 101 
 102         String filename = "META-INF/providers/" + serviceProvider.getQualifiedName();
 103         try {
 104             FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider);
 105             PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
 106             writer.println(interfaceName);
 107             writer.close();
 108         } catch (IOException e) {
 109             processingEnv.getMessager().printMessage(isBug367599(e) ? Kind.NOTE : Kind.ERROR, e.getMessage(), serviceProvider);
 110         }
 111     }
 112 
 113     /**
 114      * Determines if a given exception is (most likely) caused by
 115      * <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.
 116      */
 117     public static boolean isBug367599(Throwable t) {
 118         if (t instanceof FilerException) {
 119             for (StackTraceElement ste : t.getStackTrace()) {
 120                 if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
 121                     // See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
 122                     return true;
 123                 }
 124             }
 125         }
 126         if (t.getCause() != null) {
 127             return isBug367599(t.getCause());
 128         }
 129         return false;
 130     }
 131 
 132     @Override
 133     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 134         if (roundEnv.processingOver()) {
 135             return true;
 136         }
 137 
 138         for (Element element : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) {
 139             assert element.getKind().isClass();
 140             processElement((TypeElement) element);
 141         }
 142 
 143         return true;
 144     }
 145 }