xref: /aosp_15_r20/external/auto/service/README.md (revision 1c2bbba85eccddce6de79cbbf1645fda32e723f0)
1*1c2bbba8SAndroid Build Coastguard Worker# AutoService
2*1c2bbba8SAndroid Build Coastguard Worker
3*1c2bbba8SAndroid Build Coastguard WorkerA configuration/metadata generator for java.util.ServiceLoader-style service
4*1c2bbba8SAndroid Build Coastguard Workerproviders
5*1c2bbba8SAndroid Build Coastguard Worker
6*1c2bbba8SAndroid Build Coastguard Worker## AutoWhat‽
7*1c2bbba8SAndroid Build Coastguard Worker
8*1c2bbba8SAndroid Build Coastguard Worker[Java][java] annotation processors and other systems use
9*1c2bbba8SAndroid Build Coastguard Worker[java.util.ServiceLoader][sl] to register implementations of well-known types
10*1c2bbba8SAndroid Build Coastguard Workerusing META-INF metadata. However, it is easy for a developer to forget to update
11*1c2bbba8SAndroid Build Coastguard Workeror correctly specify the service descriptors. \
12*1c2bbba8SAndroid Build Coastguard WorkerAutoService generates this metadata for the developer, for any class annotated
13*1c2bbba8SAndroid Build Coastguard Workerwith `@AutoService`, avoiding typos, providing resistance to errors from
14*1c2bbba8SAndroid Build Coastguard Workerrefactoring, etc.
15*1c2bbba8SAndroid Build Coastguard Worker
16*1c2bbba8SAndroid Build Coastguard Worker## Example
17*1c2bbba8SAndroid Build Coastguard Worker
18*1c2bbba8SAndroid Build Coastguard WorkerSay you have:
19*1c2bbba8SAndroid Build Coastguard Worker
20*1c2bbba8SAndroid Build Coastguard Worker```java
21*1c2bbba8SAndroid Build Coastguard Workerpackage foo.bar;
22*1c2bbba8SAndroid Build Coastguard Worker
23*1c2bbba8SAndroid Build Coastguard Workerimport javax.annotation.processing.Processor;
24*1c2bbba8SAndroid Build Coastguard Worker
25*1c2bbba8SAndroid Build Coastguard Worker@AutoService(Processor.class)
26*1c2bbba8SAndroid Build Coastguard Workerfinal class MyProcessor implements Processor {
27*1c2bbba8SAndroid Build Coastguard Worker  // …
28*1c2bbba8SAndroid Build Coastguard Worker}
29*1c2bbba8SAndroid Build Coastguard Worker```
30*1c2bbba8SAndroid Build Coastguard Worker
31*1c2bbba8SAndroid Build Coastguard WorkerAutoService will generate the file
32*1c2bbba8SAndroid Build Coastguard Worker`META-INF/services/javax.annotation.processing.Processor` in the output classes
33*1c2bbba8SAndroid Build Coastguard Workerfolder. The file will contain:
34*1c2bbba8SAndroid Build Coastguard Worker
35*1c2bbba8SAndroid Build Coastguard Worker```
36*1c2bbba8SAndroid Build Coastguard Workerfoo.bar.MyProcessor
37*1c2bbba8SAndroid Build Coastguard Worker```
38*1c2bbba8SAndroid Build Coastguard Worker
39*1c2bbba8SAndroid Build Coastguard WorkerIn the case of javax.annotation.processing.Processor, if this metadata file is
40*1c2bbba8SAndroid Build Coastguard Workerincluded in a jar, and that jar is on javac's classpath, then `javac` will
41*1c2bbba8SAndroid Build Coastguard Workerautomatically load it, and include it in its normal annotation processing
42*1c2bbba8SAndroid Build Coastguard Workerenvironment. Other users of java.util.ServiceLoader may use the infrastructure
43*1c2bbba8SAndroid Build Coastguard Workerto different ends, but this metadata will provide auto-loading appropriately.
44*1c2bbba8SAndroid Build Coastguard Worker
45*1c2bbba8SAndroid Build Coastguard Worker## Getting Started
46*1c2bbba8SAndroid Build Coastguard Worker
47*1c2bbba8SAndroid Build Coastguard WorkerYou will need `auto-service-annotations-${version}.jar` in your compile-time
48*1c2bbba8SAndroid Build Coastguard Workerclasspath, and you will need `auto-service-${version}.jar` in your
49*1c2bbba8SAndroid Build Coastguard Workerannotation-processor classpath.
50*1c2bbba8SAndroid Build Coastguard Worker
51*1c2bbba8SAndroid Build Coastguard WorkerIn Maven, you can write:
52*1c2bbba8SAndroid Build Coastguard Worker
53*1c2bbba8SAndroid Build Coastguard Worker```xml
54*1c2bbba8SAndroid Build Coastguard Worker<dependencies>
55*1c2bbba8SAndroid Build Coastguard Worker  <dependency>
56*1c2bbba8SAndroid Build Coastguard Worker    <groupId>com.google.auto.service</groupId>
57*1c2bbba8SAndroid Build Coastguard Worker    <artifactId>auto-service-annotations</artifactId>
58*1c2bbba8SAndroid Build Coastguard Worker    <version>${auto-service.version}</version>
59*1c2bbba8SAndroid Build Coastguard Worker  </dependency>
60*1c2bbba8SAndroid Build Coastguard Worker</dependencies>
61*1c2bbba8SAndroid Build Coastguard Worker
62*1c2bbba8SAndroid Build Coastguard Worker...
63*1c2bbba8SAndroid Build Coastguard Worker
64*1c2bbba8SAndroid Build Coastguard Worker<plugins>
65*1c2bbba8SAndroid Build Coastguard Worker  <plugin>
66*1c2bbba8SAndroid Build Coastguard Worker    <artifactId>maven-compiler-plugin</artifactId>
67*1c2bbba8SAndroid Build Coastguard Worker    <configuration>
68*1c2bbba8SAndroid Build Coastguard Worker      <annotationProcessorPaths>
69*1c2bbba8SAndroid Build Coastguard Worker        <path>
70*1c2bbba8SAndroid Build Coastguard Worker          <groupId>com.google.auto.service</groupId>
71*1c2bbba8SAndroid Build Coastguard Worker          <artifactId>auto-service</artifactId>
72*1c2bbba8SAndroid Build Coastguard Worker          <version>${auto-service.version}</version>
73*1c2bbba8SAndroid Build Coastguard Worker        </path>
74*1c2bbba8SAndroid Build Coastguard Worker      </annotationProcessorPaths>
75*1c2bbba8SAndroid Build Coastguard Worker    </configuration>
76*1c2bbba8SAndroid Build Coastguard Worker  </plugin>
77*1c2bbba8SAndroid Build Coastguard Worker</plugins>
78*1c2bbba8SAndroid Build Coastguard Worker```
79*1c2bbba8SAndroid Build Coastguard Worker
80*1c2bbba8SAndroid Build Coastguard WorkerAlternatively, you can include the processor itself (which transitively depends
81*1c2bbba8SAndroid Build Coastguard Workeron the annotation) in your compile-time classpath. (However, note that doing so
82*1c2bbba8SAndroid Build Coastguard Workermay pull unnecessary classes into your runtime classpath.)
83*1c2bbba8SAndroid Build Coastguard Worker
84*1c2bbba8SAndroid Build Coastguard Worker```xml
85*1c2bbba8SAndroid Build Coastguard Worker<dependencies>
86*1c2bbba8SAndroid Build Coastguard Worker  <dependency>
87*1c2bbba8SAndroid Build Coastguard Worker    <groupId>com.google.auto.service</groupId>
88*1c2bbba8SAndroid Build Coastguard Worker    <artifactId>auto-service</artifactId>
89*1c2bbba8SAndroid Build Coastguard Worker    <version>${version}</version>
90*1c2bbba8SAndroid Build Coastguard Worker    <optional>true</optional>
91*1c2bbba8SAndroid Build Coastguard Worker  </dependency>
92*1c2bbba8SAndroid Build Coastguard Worker</dependencies>
93*1c2bbba8SAndroid Build Coastguard Worker```
94*1c2bbba8SAndroid Build Coastguard Worker
95*1c2bbba8SAndroid Build Coastguard Worker## License
96*1c2bbba8SAndroid Build Coastguard Worker
97*1c2bbba8SAndroid Build Coastguard Worker    Copyright 2013 Google LLC
98*1c2bbba8SAndroid Build Coastguard Worker
99*1c2bbba8SAndroid Build Coastguard Worker    Licensed under the Apache License, Version 2.0 (the "License");
100*1c2bbba8SAndroid Build Coastguard Worker    you may not use this file except in compliance with the License.
101*1c2bbba8SAndroid Build Coastguard Worker    You may obtain a copy of the License at
102*1c2bbba8SAndroid Build Coastguard Worker
103*1c2bbba8SAndroid Build Coastguard Worker       http://www.apache.org/licenses/LICENSE-2.0
104*1c2bbba8SAndroid Build Coastguard Worker
105*1c2bbba8SAndroid Build Coastguard Worker    Unless required by applicable law or agreed to in writing, software
106*1c2bbba8SAndroid Build Coastguard Worker    distributed under the License is distributed on an "AS IS" BASIS,
107*1c2bbba8SAndroid Build Coastguard Worker    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
108*1c2bbba8SAndroid Build Coastguard Worker    See the License for the specific language governing permissions and
109*1c2bbba8SAndroid Build Coastguard Worker    limitations under the License.
110*1c2bbba8SAndroid Build Coastguard Worker
111*1c2bbba8SAndroid Build Coastguard Worker[java]: https://en.wikipedia.org/wiki/Java_(programming_language)
112*1c2bbba8SAndroid Build Coastguard Worker[sl]: http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html
113