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 jdk.vm.ci.service.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.RoundEnvironment;
  33 import javax.annotation.processing.SupportedAnnotationTypes;
  34 import javax.lang.model.SourceVersion;
  35 import javax.lang.model.element.Element;
  36 import javax.lang.model.element.TypeElement;
  37 import javax.lang.model.type.MirroredTypeException;
  38 import javax.lang.model.type.TypeMirror;
  39 import javax.tools.Diagnostic.Kind;
  40 import javax.tools.FileObject;
  41 import javax.tools.StandardLocation;
  42 
  43 import jdk.vm.ci.service.ServiceProvider;
  44 
  45 @SupportedAnnotationTypes("jdk.vm.ci.service.ServiceProvider")
  46 public class ServiceProviderProcessor extends AbstractProcessor {
  47 
  48     private final Set<TypeElement> processed = new HashSet<>();
  49 
  50     @Override
  51     public SourceVersion getSupportedSourceVersion() {
  52         return SourceVersion.latest();
  53     }
  54 
  55     private boolean verifyAnnotation(TypeMirror serviceInterface, TypeElement serviceProvider) {
  56         if (!processingEnv.getTypeUtils().isSubtype(serviceProvider.asType(), serviceInterface)) {
  57             String msg = String.format("Service provider class %s must implement service interface %s", serviceProvider.getSimpleName(), serviceInterface);
  58             processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
  59             return false;
  60         }
  61 
  62         return true;
  63     }
  64 
  65     private void processElement(TypeElement serviceProvider) {
  66         if (processed.contains(serviceProvider)) {
  67             return;
  68         }
  69 
  70         processed.add(serviceProvider);
  71         ServiceProvider annotation = serviceProvider.getAnnotation(ServiceProvider.class);
  72         if (annotation != null) {
  73             try {
  74                 annotation.value();
  75             } catch (MirroredTypeException ex) {
  76                 TypeMirror serviceInterface = ex.getTypeMirror();
  77                 if (verifyAnnotation(serviceInterface, serviceProvider)) {
  78                     String interfaceName = ex.getTypeMirror().toString();
  79                     createProviderFile(serviceProvider, interfaceName);
  80                 }
  81             }
  82         }
  83     }
  84 
  85     private void createProviderFile(TypeElement serviceProvider, String interfaceName) {
  86         if (serviceProvider.getNestingKind().isNested()) {
  87             // This is a simplifying constraint that means we don't have to
  88             // processed the qualified name to insert '$' characters at
  89             // the relevant positions.
  90             String msg = String.format("Service provider class %s must be a top level class", serviceProvider.getSimpleName());
  91             processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
  92             return;
  93         }
  94 
  95         String filename = "META-INF/jvmci.providers/" + serviceProvider.getQualifiedName();
  96         try {
  97             FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider);
  98             PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
  99             writer.println(interfaceName);
 100             writer.close();
 101         } catch (IOException e) {
 102             processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), serviceProvider);
 103         }
 104     }
 105 
 106     @Override
 107     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
 108         if (roundEnv.processingOver()) {
 109             return true;
 110         }
 111 
 112         for (Element element : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) {
 113             assert element.getKind().isClass();
 114             processElement((TypeElement) element);
 115         }
 116 
 117         return true;
 118     }
 119 }