001/* 002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package jdk.internal.jvmci.service.processor; 024 025import java.io.*; 026import java.util.*; 027 028import javax.annotation.processing.*; 029import javax.lang.model.*; 030import javax.lang.model.element.*; 031import javax.lang.model.type.*; 032import javax.tools.Diagnostic.Kind; 033import javax.tools.*; 034 035import jdk.internal.jvmci.service.*; 036 037@SupportedAnnotationTypes("jdk.internal.jvmci.service.ServiceProvider") 038public class ServiceProviderProcessor extends AbstractProcessor { 039 040 private final Set<TypeElement> processed = new HashSet<>(); 041 042 @Override 043 public SourceVersion getSupportedSourceVersion() { 044 return SourceVersion.latest(); 045 } 046 047 private boolean verifyAnnotation(TypeMirror serviceInterface, TypeElement serviceProvider) { 048 if (!processingEnv.getTypeUtils().isSubtype(serviceProvider.asType(), serviceInterface)) { 049 String msg = String.format("Service provider class %s must implement service interface %s", serviceProvider.getSimpleName(), serviceInterface); 050 processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider); 051 return false; 052 } 053 054 return true; 055 } 056 057 private void processElement(TypeElement serviceProvider) { 058 if (processed.contains(serviceProvider)) { 059 return; 060 } 061 062 processed.add(serviceProvider); 063 ServiceProvider annotation = serviceProvider.getAnnotation(ServiceProvider.class); 064 if (annotation != null) { 065 try { 066 annotation.value(); 067 } catch (MirroredTypeException ex) { 068 TypeMirror serviceInterface = ex.getTypeMirror(); 069 if (verifyAnnotation(serviceInterface, serviceProvider)) { 070 String interfaceName = ex.getTypeMirror().toString(); 071 createProviderFile(serviceProvider, interfaceName); 072 } 073 } 074 } 075 } 076 077 private void createProviderFile(TypeElement serviceProvider, String interfaceName) { 078 if (serviceProvider.getNestingKind().isNested()) { 079 // This is a simplifying constraint that means we don't have to 080 // processed the qualified name to insert '$' characters at 081 // the relevant positions. 082 String msg = String.format("Service provider class %s must be a top level class", serviceProvider.getSimpleName()); 083 processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider); 084 return; 085 } 086 087 String filename = "META-INF/jvmci.providers/" + serviceProvider.getQualifiedName(); 088 try { 089 FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider); 090 PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8")); 091 writer.println(interfaceName); 092 writer.close(); 093 } catch (IOException e) { 094 processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), serviceProvider); 095 } 096 } 097 098 @Override 099 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 100 if (roundEnv.processingOver()) { 101 return true; 102 } 103 104 for (Element element : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) { 105 assert element.getKind().isClass(); 106 processElement((TypeElement) element); 107 } 108 109 return true; 110 } 111}