-
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 all 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,92 @@ | ||
| /* | ||
| * Copyright (C) 2025 Square, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package retrofit2.keeper | ||
|
|
||
| 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 = element.qualifiedName?.asString() ?: return@forEach | ||
|
|
||
| val dependencies = Dependencies(aggregating = false, containingFile) | ||
| codeGenerator.createNewFile(dependencies, "", proguardFilePath(typeName), "") | ||
| .bufferedWriter().use { w -> | ||
| w.write("# $typeName\n") | ||
| for (referencedType in referencedTypes.sorted()) { | ||
| w.write(keepRuleForType(referencedType)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
|
|
||
| class Provider : SymbolProcessorProvider { | ||
| override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = | ||
| RetrofitResponseTypeKeepSymbolProcessor(environment) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (C) 2025 Square, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| 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 fun keepRuleForType(referencedType: String): String = | ||
| "-keep,allowoptimization,allowshrinking,allowobfuscation class $referencedType\n" | ||
|
|
||
| internal fun proguardFilePath(typeName: String) = | ||
| "META-INF/proguard/retrofit-response-type-keeper-$typeName.pro" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| retrofit2.keeper.RetrofitResponseTypeKeepSymbolProcessor$Provider |
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.
Copyright header please