-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Support using response type keeper with KSP #4526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from 4 commits
460d9bf
cb2c809
50e1b45
1af22c5
01caf49
4d95d22
362d976
5b6c1b5
8a08507
b55af1d
b5e403b
4b7c87b
97bf8f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package retrofit2.keeper | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copyright header please |
||
|
|
||
| import com.google.auto.service.AutoService | ||
| import com.google.devtools.ksp.processing.CodeGenerator | ||
| import com.google.devtools.ksp.processing.Dependencies | ||
| import com.google.devtools.ksp.processing.Resolver | ||
| import com.google.devtools.ksp.processing.SymbolProcessor | ||
| import com.google.devtools.ksp.processing.SymbolProcessorEnvironment | ||
| import com.google.devtools.ksp.processing.SymbolProcessorProvider | ||
| import com.google.devtools.ksp.symbol.KSAnnotated | ||
| import com.google.devtools.ksp.symbol.KSClassDeclaration | ||
| import com.google.devtools.ksp.symbol.KSFunctionDeclaration | ||
| import com.google.devtools.ksp.symbol.KSType | ||
| import com.google.devtools.ksp.symbol.Modifier | ||
|
|
||
| class RetrofitResponseTypeKeepSymbolProcessor( | ||
| environment: SymbolProcessorEnvironment, | ||
| ) : SymbolProcessor { | ||
| private val codeGenerator: CodeGenerator = environment.codeGenerator | ||
|
|
||
| override fun process(resolver: Resolver): List<KSAnnotated> { | ||
| val elementToReferencedTypes = mutableMapOf<KSClassDeclaration, MutableSet<String>>() | ||
|
|
||
| annotationNames.flatMap { resolver.getSymbolsWithAnnotation(it) } | ||
| .filterIsInstance<KSFunctionDeclaration>() | ||
| .forEach { function -> | ||
| val serviceType = function.parentDeclaration as? KSClassDeclaration ?: return@forEach | ||
| val referenced = elementToReferencedTypes.getOrPut(serviceType, ::LinkedHashSet) | ||
|
|
||
| // Retrofit has special support for 'suspend fun' in Kotlin which manifests as a | ||
| // final Continuation parameter whose generic type is the declared return type. | ||
| if (function.modifiers.contains(Modifier.SUSPEND)) { | ||
| function.parameters.forEach { | ||
| it.type.resolve().recursiveParameterizedTypesTo(referenced) | ||
| } | ||
| } | ||
|
|
||
| val returnType = function.returnType?.resolve() ?: return@forEach | ||
| returnType.recursiveParameterizedTypesTo(referenced) | ||
| } | ||
|
|
||
| elementToReferencedTypes.forEach { (element, referencedTypes) -> | ||
| val containingFile = element.containingFile ?: return@forEach | ||
| val typeName = requireNotNull(element.qualifiedName).asString() | ||
|
|
||
| val dependencies = Dependencies(aggregating = false, containingFile) | ||
| codeGenerator.createNewFile(dependencies, "", outputFile(typeName), "") | ||
| .bufferedWriter().use { writer -> | ||
| writer.write("# $typeName\n") | ||
| for (referencedType in referencedTypes.sorted()) { | ||
| writer.write("$KEEP_RULE_PREFIX $referencedType\n") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return emptyList() | ||
| } | ||
|
|
||
| private fun KSType.recursiveParameterizedTypesTo(types: MutableSet<String>) { | ||
| val declaration = this.declaration | ||
| if (declaration is KSClassDeclaration) { | ||
| var qualifiedName = declaration.qualifiedName?.asString() | ||
| if (qualifiedName == "kotlin.Any") { | ||
| qualifiedName = "java.lang.Object" | ||
| } | ||
|
Comment on lines
+77
to
+79
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I left it in the annotation processor because it was harmless. I'm fine filtering them out if you want to do that in both. |
||
| qualifiedName?.let { types.add(it) } | ||
| } | ||
|
|
||
| for (typeArgument in arguments) { | ||
| typeArgument.type?.resolve()?.recursiveParameterizedTypesTo(types) | ||
| } | ||
| } | ||
|
|
||
| @Suppress("unused") // Used in service file. | ||
| @AutoService(SymbolProcessorProvider::class) | ||
| class Provider : SymbolProcessorProvider { | ||
| override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
| RetrofitResponseTypeKeepSymbolProcessor(environment) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package retrofit2.keeper | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copyright header please |
||
|
|
||
| internal val annotationNames = setOf( | ||
| "retrofit2.http.DELETE", | ||
| "retrofit2.http.GET", | ||
| "retrofit2.http.HEAD", | ||
| "retrofit2.http.HTTP", | ||
| "retrofit2.http.OPTIONS", | ||
| "retrofit2.http.PATCH", | ||
| "retrofit2.http.POST", | ||
| "retrofit2.http.PUT", | ||
| ) | ||
|
|
||
| internal const val KEEP_RULE_PREFIX = "-keep,allowoptimization,allowshrinking,allowobfuscation class" | ||
|
|
||
| internal fun outputFile(typeName: String) = | ||
| "META-INF/proguard/retrofit-response-type-keeper-$typeName.pro" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,147 +16,85 @@ | |
| package retrofit2.keeper | ||
|
|
||
| import com.google.common.truth.Truth.assertAbout | ||
| import com.google.common.truth.Truth.assertThat | ||
| import com.google.testing.compile.JavaFileObjects | ||
| import com.google.testing.compile.JavaSourceSubjectFactory.javaSource | ||
| import com.google.testing.junit.testparameterinjector.TestParameter | ||
| import com.google.testing.junit.testparameterinjector.TestParameterInjector | ||
| import com.tschuchort.compiletesting.KotlinCompilation | ||
| import com.tschuchort.compiletesting.KotlinCompilation.ExitCode | ||
| import com.tschuchort.compiletesting.SourceFile | ||
| import com.tschuchort.compiletesting.configureKsp | ||
| import com.tschuchort.compiletesting.kspSourcesDir | ||
| import java.nio.charset.StandardCharsets.UTF_8 | ||
| import java.nio.file.NoSuchFileException | ||
| import javax.tools.StandardLocation.CLASS_OUTPUT | ||
| import kotlin.io.path.readText | ||
| import kotlin.io.path.toPath | ||
| import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| class RetrofitResponseTypeKeepProcessorTest { | ||
| @RunWith(TestParameterInjector::class) | ||
| class RetrofitResponseTypeKeepProcessorTest( | ||
| @param:TestParameter private val useKsp: Boolean, | ||
| ) { | ||
| @OptIn(ExperimentalCompilerApi::class) | ||
| @Test | ||
| fun allHttpMethods() { | ||
| val service = JavaFileObjects.forSourceString( | ||
| "test.Service", | ||
| """ | ||
| package test; | ||
| import retrofit2.*; | ||
| import retrofit2.http.*; | ||
| fun process( | ||
| @TestParameter( | ||
| "all-http-methods", | ||
| "nesting", | ||
| "kotlin-suspend", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm I'm not sure we have enough tests to necessitate such a robust mechanism (as opposed to just three functions with the input source and output rules).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably do an enum that's like enum class Generator {
AnnotationProcessor {
override fun validate(source: String, rules: String) { .. }
},
Ksp {
override fun validate(source: String, rules: String) { .. }
},
;
abstract fun validate(source: String, rules: String)
}and then make that enum the class-level test parameter. Then each method is basically just @Test fun stuff() {
generator.validate(
source = """
|stuff
""".trimMargin(),
rules = """
|stuff
""".trimMargin(),
)
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. But I would still suggest using parametermized tests to reduce boilerplates. |
||
| ) name: String, | ||
| ) { | ||
| val rules = readResourceAsText("$name/Service.pro") | ||
| val generatedPath = "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro" | ||
|
|
||
| class DeleteUser {} | ||
| class GetUser {} | ||
| class HeadUser {} | ||
| class HttpUser {} | ||
| class OptionsUser {} | ||
| class PatchUser {} | ||
| class PostUser {} | ||
| class PutUser {} | ||
|
|
||
| interface Service { | ||
| @DELETE("/") Call<DeleteUser> delete(); | ||
| @GET("/") Call<GetUser> get(); | ||
| @HEAD("/") Call<HeadUser> head(); | ||
| @HTTP(method = "CUSTOM", path = "/") Call<HttpUser> http(); | ||
| @OPTIONS("/") Call<OptionsUser> options(); | ||
| @PATCH("/") Call<PatchUser> patch(); | ||
| @POST("/") Call<PostUser> post(); | ||
| @PUT("/") Call<PutUser> put(); | ||
| if (useKsp) { | ||
| val compilation = KotlinCompilation().apply { | ||
| configureKsp { | ||
| inheritClassPath = true | ||
| symbolProcessorProviders += RetrofitResponseTypeKeepSymbolProcessor.Provider() | ||
| sources = listOf( | ||
| SourceFile.kotlin( | ||
| "Service.kt", | ||
| readResourceAsText("$name/Service.kt"), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| """.trimIndent(), | ||
| ) | ||
| val result = compilation.compile() | ||
|
|
||
| assertAbout(javaSource()) | ||
| .that(service) | ||
| .processedWith(RetrofitResponseTypeKeepProcessor()) | ||
| .compilesWithoutError() | ||
| .and() | ||
| .generatesFileNamed( | ||
| CLASS_OUTPUT, | ||
| "", | ||
| "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", | ||
| ).withStringContents( | ||
| UTF_8, | ||
| """ | ||
| |# test.Service | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.DeleteUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.GetUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HeadUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.HttpUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.OptionsUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PatchUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PostUser | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.PutUser | ||
| | | ||
| """.trimMargin(), | ||
| assertThat(result.exitCode).isEqualTo(ExitCode.OK) | ||
| assertThat(compilation.kspSourcesDir.resolve("resources/$generatedPath").readText()) | ||
| .isEqualTo(rules) | ||
| } else { | ||
| val service = JavaFileObjects.forSourceString( | ||
| "test.Service", | ||
| readResourceAsText("$name/Service.java"), | ||
| ) | ||
| assertAbout(javaSource()) | ||
| .that(service) | ||
| .processedWith(RetrofitResponseTypeKeepProcessor()) | ||
| .compilesWithoutError() | ||
| .and() | ||
| .generatesFileNamed( | ||
| CLASS_OUTPUT, | ||
| "", | ||
| generatedPath, | ||
| ).withStringContents( | ||
| UTF_8, | ||
| rules, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun nesting() { | ||
| val service = JavaFileObjects.forSourceString( | ||
| "test.Service", | ||
| """ | ||
| package test; | ||
| import retrofit2.*; | ||
| import retrofit2.http.*; | ||
|
|
||
| class One<T> {} | ||
| class Two<T> {} | ||
| class Three {} | ||
|
|
||
| interface Service { | ||
| @GET("/") Call<One<Two<Three>>> get(); | ||
| } | ||
| """.trimIndent(), | ||
| ) | ||
|
|
||
| assertAbout(javaSource()) | ||
| .that(service) | ||
| .processedWith(RetrofitResponseTypeKeepProcessor()) | ||
| .compilesWithoutError() | ||
| .and() | ||
| .generatesFileNamed( | ||
| CLASS_OUTPUT, | ||
| "", | ||
| "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", | ||
| ).withStringContents( | ||
| UTF_8, | ||
| """ | ||
| |# test.Service | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class retrofit2.Call | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.One | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Three | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Two | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun kotlinSuspend() { | ||
| val service = JavaFileObjects.forSourceString( | ||
| "test.Service", | ||
| """ | ||
| package test; | ||
| import kotlin.coroutines.Continuation; | ||
| import retrofit2.*; | ||
| import retrofit2.http.*; | ||
|
|
||
| class Body {} | ||
|
|
||
| interface Service { | ||
| @GET("/") Object get(Continuation<? extends Body> c); | ||
| } | ||
| """.trimIndent(), | ||
| ) | ||
|
|
||
| assertAbout(javaSource()) | ||
| .that(service) | ||
| .processedWith(RetrofitResponseTypeKeepProcessor()) | ||
| .compilesWithoutError() | ||
| .and() | ||
| .generatesFileNamed( | ||
| CLASS_OUTPUT, | ||
| "", | ||
| "META-INF/proguard/retrofit-response-type-keeper-test.Service.pro", | ||
| ).withStringContents( | ||
| UTF_8, | ||
| """ | ||
| |# test.Service | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class java.lang.Object | ||
| |-keep,allowoptimization,allowshrinking,allowobfuscation class test.Body | ||
| | | ||
| """.trimMargin(), | ||
| ) | ||
| private companion object { | ||
| fun readResourceAsText(name: String): String { | ||
| val resource = this::class.java.classLoader.getResource(name) | ||
| ?: throw NoSuchFileException("Resource $name not found.") | ||
| return resource.toURI().toPath().readText() | ||
| } | ||
| } | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of pulling out the constant, lets pull out this little block into a function that writes the whole file.