12
12
import javax .lang .model .element .AnnotationValue ;
13
13
import javax .lang .model .element .Element ;
14
14
import javax .lang .model .element .TypeElement ;
15
+ import javax .tools .FileObject ;
15
16
import java .io .IOException ;
17
+ import java .io .Writer ;
16
18
import java .util .LinkedHashSet ;
17
19
import java .util .List ;
18
20
import java .util .Set ;
19
21
20
22
public class ClientProcessor extends AbstractProcessor {
21
23
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
+
22
28
protected ProcessingContext ctx ;
23
29
24
30
@ Override
@@ -49,15 +55,30 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
49
55
for (Element importedElement : round .getElementsAnnotatedWith (Client .Import .class )) {
50
56
writeForImported (importedElement );
51
57
}
58
+ if (round .processingOver ()) {
59
+ writeServicesFile ();
60
+ }
52
61
return false ;
53
62
}
54
63
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
+
55
77
private void writeForImported (Element importedElement ) {
56
78
for (AnnotationMirror annotationMirror : importedElement .getAnnotationMirrors ()) {
57
79
for (AnnotationValue value : annotationMirror .getElementValues ().values ()) {
58
80
for (Object apiClassDef : (List <?>) value .getValue ()) {
59
- String fullName = apiClassDef .toString ();
60
- writeImported (fullName );
81
+ writeImported (apiClassDef .toString ());
61
82
}
62
83
}
63
84
}
@@ -66,7 +87,6 @@ private void writeForImported(Element importedElement) {
66
87
private void writeImported (String fullName ) {
67
88
// trim .class suffix
68
89
String apiClassName = fullName .substring (0 , fullName .length () - 6 );
69
- //ctx.logError(null, "build import:" + apiClassName);
70
90
TypeElement typeElement = ctx .getTypeElement (apiClassName );
71
91
if (typeElement != null ) {
72
92
writeClient (typeElement );
@@ -78,16 +98,16 @@ private void writeClient(Element controller) {
78
98
ControllerReader reader = new ControllerReader ((TypeElement ) controller , ctx );
79
99
reader .read (false );
80
100
try {
81
- writeClientAdapter (ctx , reader );
101
+ generatedClients . add ( writeClientAdapter (ctx , reader ) );
82
102
} catch (Throwable e ) {
83
103
e .printStackTrace ();
84
104
ctx .logError (reader .getBeanType (), "Failed to write client class " + e );
85
105
}
86
106
}
87
107
}
88
108
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 ();
91
111
}
92
112
93
113
}
0 commit comments