1212import javax .lang .model .element .AnnotationValue ;
1313import javax .lang .model .element .Element ;
1414import javax .lang .model .element .TypeElement ;
15+ import javax .tools .FileObject ;
1516import java .io .IOException ;
17+ import java .io .Writer ;
1618import java .util .LinkedHashSet ;
1719import java .util .List ;
1820import java .util .Set ;
1921
2022public class ClientProcessor extends AbstractProcessor {
2123
24+ private static final String METAINF_SERVICES_PROVIDER = "META-INF/services/io.avaje.http.client.HttpApiProvider" ;
25+
26+ private final Set <String > generatedClients = new LinkedHashSet <>();
27+
2228 protected ProcessingContext ctx ;
2329
2430 @ Override
@@ -49,15 +55,30 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
4955 for (Element importedElement : round .getElementsAnnotatedWith (Client .Import .class )) {
5056 writeForImported (importedElement );
5157 }
58+ if (round .processingOver ()) {
59+ writeServicesFile ();
60+ }
5261 return false ;
5362 }
5463
64+ private void writeServicesFile () {
65+ try {
66+ final FileObject metaInfWriter = ctx .createMetaInfWriter (METAINF_SERVICES_PROVIDER );
67+ final Writer writer = metaInfWriter .openWriter ();
68+ for (String generatedClient : generatedClients ) {
69+ writer .append (generatedClient ).append ("$Provider\n " );
70+ }
71+ writer .close ();
72+ } catch (IOException e ) {
73+ ctx .logError (null , "Error writing services file " + e , e );
74+ }
75+ }
76+
5577 private void writeForImported (Element importedElement ) {
5678 for (AnnotationMirror annotationMirror : importedElement .getAnnotationMirrors ()) {
5779 for (AnnotationValue value : annotationMirror .getElementValues ().values ()) {
5880 for (Object apiClassDef : (List <?>) value .getValue ()) {
59- String fullName = apiClassDef .toString ();
60- writeImported (fullName );
81+ writeImported (apiClassDef .toString ());
6182 }
6283 }
6384 }
@@ -66,7 +87,6 @@ private void writeForImported(Element importedElement) {
6687 private void writeImported (String fullName ) {
6788 // trim .class suffix
6889 String apiClassName = fullName .substring (0 , fullName .length () - 6 );
69- //ctx.logError(null, "build import:" + apiClassName);
7090 TypeElement typeElement = ctx .getTypeElement (apiClassName );
7191 if (typeElement != null ) {
7292 writeClient (typeElement );
@@ -78,16 +98,16 @@ private void writeClient(Element controller) {
7898 ControllerReader reader = new ControllerReader ((TypeElement ) controller , ctx );
7999 reader .read (false );
80100 try {
81- writeClientAdapter (ctx , reader );
101+ generatedClients . add ( writeClientAdapter (ctx , reader ) );
82102 } catch (Throwable e ) {
83103 e .printStackTrace ();
84104 ctx .logError (reader .getBeanType (), "Failed to write client class " + e );
85105 }
86106 }
87107 }
88108
89- protected void writeClientAdapter (ProcessingContext ctx , ControllerReader reader ) throws IOException {
90- new ClientWriter (reader , ctx ).write ();
109+ protected String writeClientAdapter (ProcessingContext ctx , ControllerReader reader ) throws IOException {
110+ return new ClientWriter (reader , ctx ).write ();
91111 }
92112
93113}
0 commit comments