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