Skip to content

Commit c7dad69

Browse files
cgdeckerGoogle Java Core Libraries
authored andcommitted
Open source @AutoService KSP processor as auto-service-ksp.
Fixes #2023 RELNOTES=service: added a KSP processor for @autoservice PiperOrigin-RevId: 960429386
1 parent 5c2ae41 commit c7dad69

8 files changed

Lines changed: 1139 additions & 100 deletions

File tree

.github/workflows/ci.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ jobs:
4545
run: mvn -B dependency:go-offline test clean -U --quiet --fail-never -DskipTests=true -f build-pom.xml
4646
- name: 'Test'
4747
shell: bash
48-
run: mvn -B verify -U --fail-at-end -Dsource.skip=true -Dmaven.javadoc.skip=true -f build-pom.xml
48+
# One of the auto-service-ksp test deps was built targeting JDK 11, so we skip its tests
49+
# here and only run them on JDK 11+ in the next step.
50+
run: mvn -B verify -U --fail-at-end -Dsource.skip=true -Dmaven.javadoc.skip=true -pl '!service/processor_ksp' -f build-pom.xml
51+
- name: 'Test auto-service-ksp'
52+
if: ${{ matrix.java >= 11 }}
53+
shell: bash
54+
run: mvn -B verify -U --fail-at-end -Dsource.skip=true -Dmaven.javadoc.skip=true -pl service/processor_ksp -f build-pom.xml
4955

5056
publish_snapshot:
5157
name: 'Publish snapshot'

service/pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,19 @@
6262
<modules>
6363
<module>annotations</module>
6464
<module>processor</module>
65+
<module>processor_ksp</module>
6566
</modules>
6667

6768
<dependencyManagement>
6869
<dependencies>
6970
<!-- main dependencies -->
70-
7171
<dependency>
7272
<groupId>com.google.guava</groupId>
7373
<artifactId>guava</artifactId>
7474
<version>${guava.version}</version>
7575
</dependency>
76-
<dependency>
77-
<groupId>com.google.guava</groupId>
78-
<artifactId>guava-gwt</artifactId>
79-
<version>${guava.version}</version>
80-
</dependency>
8176

8277
<!-- test dependencies -->
83-
8478
<dependency>
8579
<groupId>com.google.testing.compile</groupId>
8680
<artifactId>compile-testing</artifactId>

service/processor/src/main/java/com/google/auto/service/processor/AutoServiceProcessor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.auto.common.MoreElements.getAnnotationMirror;
2020
import static com.google.common.base.Throwables.getStackTraceAsString;
2121
import static com.google.common.collect.ImmutableSet.toImmutableSet;
22+
import static java.nio.charset.StandardCharsets.UTF_8;
2223

2324
import com.google.auto.common.MoreElements;
2425
import com.google.auto.common.MoreTypes;
@@ -28,8 +29,10 @@
2829
import com.google.common.collect.ImmutableSet;
2930
import com.google.common.collect.SortedSetMultimap;
3031
import com.google.common.collect.TreeMultimap;
32+
import java.io.BufferedWriter;
3133
import java.io.IOException;
3234
import java.io.OutputStream;
35+
import java.io.OutputStreamWriter;
3336
import java.util.ArrayList;
3437
import java.util.Arrays;
3538
import java.util.Collections;
@@ -94,8 +97,6 @@ public SourceVersion getSupportedSourceVersion() {
9497
}
9598

9699
/**
97-
*
98-
*
99100
* <ol>
100101
* <li>For each class annotated with {@link AutoService}
101102
* <ul>
@@ -186,8 +187,13 @@ private void generateConfigFiles() {
186187
log("New service file contents: " + newServices);
187188
FileObject fileObject =
188189
filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
189-
try (OutputStream out = fileObject.openOutputStream()) {
190-
ServicesFiles.writeServiceFile(newServices, out);
190+
try (OutputStream out = fileObject.openOutputStream();
191+
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8);
192+
BufferedWriter buffered = new BufferedWriter(writer)) {
193+
for (String service : newServices) {
194+
buffered.write(service);
195+
buffered.write('\n');
196+
}
191197
}
192198
log("Wrote to: " + resourceFile);
193199
} catch (IOException e) {

service/processor/src/main/java/com/google/auto/service/processor/ServicesFiles.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

service/processor_ksp/pom.xml

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2013 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
21+
<parent>
22+
<groupId>com.google.auto.service</groupId>
23+
<artifactId>auto-service-aggregator</artifactId>
24+
<version>HEAD-SNAPSHOT</version>
25+
</parent>
26+
27+
<groupId>com.google.auto.service</groupId>
28+
<artifactId>auto-service-ksp</artifactId>
29+
<version>HEAD-SNAPSHOT</version>
30+
<name>AutoService KSP Processor</name>
31+
<description>
32+
KSP processor for generating ServiceLoader provider configuration files.
33+
</description>
34+
<url>https://github.com/google/auto/tree/main/service</url>
35+
36+
<scm>
37+
<url>http://github.com/google/auto</url>
38+
<connection>scm:git:git://github.com/google/auto.git</connection>
39+
<developerConnection>scm:git:ssh://git@github.com/google/auto.git</developerConnection>
40+
<tag>HEAD</tag>
41+
</scm>
42+
43+
<properties>
44+
<maven.compiler.release>${java.version}</maven.compiler.release>
45+
<kotlin.version>2.4.10</kotlin.version>
46+
<kotlin.compiler.languageVersion>2.0</kotlin.compiler.languageVersion>
47+
<kotlin.compiler.apiVersion>2.0</kotlin.compiler.apiVersion>
48+
<kotlin.compiler.jdkRelease>${java.version}</kotlin.compiler.jdkRelease>
49+
</properties>
50+
51+
<repositories>
52+
<!-- needed for room3-compiler-processing-testing -->
53+
<repository>
54+
<id>google</id>
55+
<url>https://maven.google.com/</url>
56+
</repository>
57+
</repositories>
58+
59+
<dependencies>
60+
<dependency>
61+
<groupId>com.google.auto.service</groupId>
62+
<artifactId>auto-service-annotations</artifactId>
63+
<version>${project.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.google.devtools.ksp</groupId>
67+
<artifactId>symbol-processing-api</artifactId>
68+
<version>2.3.11</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.google.guava</groupId>
72+
<artifactId>guava</artifactId>
73+
</dependency>
74+
<!-- test dependencies -->
75+
<dependency>
76+
<groupId>androidx.room3</groupId>
77+
<artifactId>room3-compiler-processing-testing</artifactId>
78+
<version>3.0.1</version>
79+
<scope>test</scope>
80+
<exclusions>
81+
<!--
82+
room3-compiler-processing-testing declares a dependency on symbol-processing:jar, but the
83+
symbol-processing artifact is (now, at any rate) just a pom with no jar. Simply excluding
84+
the dep seems to make everything work fine.
85+
-->
86+
<exclusion>
87+
<groupId>com.google.devtools.ksp</groupId>
88+
<artifactId>symbol-processing</artifactId>
89+
</exclusion>
90+
</exclusions>
91+
</dependency>
92+
<dependency>
93+
<groupId>junit</groupId>
94+
<artifactId>junit</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>com.google.truth</groupId>
99+
<artifactId>truth</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
<!-- the KSP test actually tests the JAPT processor as well -->
103+
<dependency>
104+
<groupId>com.google.auto.service</groupId>
105+
<artifactId>auto-service</artifactId>
106+
<version>${project.version}</version>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
111+
<build>
112+
<!-- setting these explicitly just prevents warnings about no src/(main|test)/java dirs -->
113+
<sourceDirectory>src/main/kotlin</sourceDirectory>
114+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
115+
<plugins>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-jar-plugin</artifactId>
119+
</plugin>
120+
<plugin>
121+
<groupId>org.jetbrains.kotlin</groupId>
122+
<artifactId>kotlin-maven-plugin</artifactId>
123+
<version>${kotlin.version}</version>
124+
<extensions>true</extensions>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</project>

0 commit comments

Comments
 (0)