mirror of
https://github.com/fankes/moshi.git
synced 2025-10-19 07:59:21 +08:00
Add an argument to be disable proguard code generating (#1353)
Co-authored-by: SeongUg Jung <seongug.jung@hanwha.com> Co-authored-by: Zac Sweers <pandanomic@gmail.com>
This commit is contained in:
committed by
GitHub
parent
3c108919ee
commit
72f464bbdc
@@ -59,6 +59,13 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
|
|||||||
* * `"javax.annotation.Generated"` (JRE <9)
|
* * `"javax.annotation.Generated"` (JRE <9)
|
||||||
*/
|
*/
|
||||||
public const val OPTION_GENERATED: String = "moshi.generated"
|
public const val OPTION_GENERATED: String = "moshi.generated"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This annotation processing argument disables proguard rule generation.
|
||||||
|
* Normally, this is not recommended unless end-users build their own JsonAdapter look-up tool.
|
||||||
|
* This is enabled by default
|
||||||
|
*/
|
||||||
|
public const val OPTION_ENABLE_PROGUARD_RULE_GENERATION: String = "moshi.enabledProguardGenerated"
|
||||||
private val POSSIBLE_GENERATED_NAMES = arrayOf(
|
private val POSSIBLE_GENERATED_NAMES = arrayOf(
|
||||||
ClassName("javax.annotation.processing", "Generated"),
|
ClassName("javax.annotation.processing", "Generated"),
|
||||||
ClassName("javax.annotation", "Generated")
|
ClassName("javax.annotation", "Generated")
|
||||||
@@ -72,6 +79,7 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
|
|||||||
private lateinit var cachedClassInspector: MoshiCachedClassInspector
|
private lateinit var cachedClassInspector: MoshiCachedClassInspector
|
||||||
private val annotation = JsonClass::class.java
|
private val annotation = JsonClass::class.java
|
||||||
private var generatedType: ClassName? = null
|
private var generatedType: ClassName? = null
|
||||||
|
private var generateProguardRules: Boolean = true
|
||||||
|
|
||||||
override fun getSupportedAnnotationTypes(): Set<String> = setOf(annotation.canonicalName)
|
override fun getSupportedAnnotationTypes(): Set<String> = setOf(annotation.canonicalName)
|
||||||
|
|
||||||
@@ -87,6 +95,9 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
|
|||||||
"allowable values are $POSSIBLE_GENERATED_NAMES."
|
"allowable values are $POSSIBLE_GENERATED_NAMES."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generateProguardRules = processingEnv.options[OPTION_ENABLE_PROGUARD_RULE_GENERATION]?.toBooleanStrictOrNull() ?: true
|
||||||
|
|
||||||
this.types = processingEnv.typeUtils
|
this.types = processingEnv.typeUtils
|
||||||
this.elements = processingEnv.elementUtils
|
this.elements = processingEnv.elementUtils
|
||||||
this.filer = processingEnv.filer
|
this.filer = processingEnv.filer
|
||||||
@@ -113,7 +124,7 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
|
|||||||
if (jsonClass.generateAdapter && jsonClass.generator.isEmpty()) {
|
if (jsonClass.generateAdapter && jsonClass.generator.isEmpty()) {
|
||||||
val generator = adapterGenerator(type, cachedClassInspector) ?: continue
|
val generator = adapterGenerator(type, cachedClassInspector) ?: continue
|
||||||
val preparedAdapter = generator
|
val preparedAdapter = generator
|
||||||
.prepare { spec ->
|
.prepare(generateProguardRules) { spec ->
|
||||||
spec.toBuilder()
|
spec.toBuilder()
|
||||||
.apply {
|
.apply {
|
||||||
@Suppress("DEPRECATION") // This is a Java type
|
@Suppress("DEPRECATION") // This is a Java type
|
||||||
@@ -134,9 +145,11 @@ public class JsonClassCodegenProcessor : AbstractProcessor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
preparedAdapter.spec.writeTo(filer)
|
preparedAdapter.spec.writeTo(filer)
|
||||||
|
if (generateProguardRules) {
|
||||||
preparedAdapter.proguardConfig?.writeTo(filer, type)
|
preparedAdapter.proguardConfig?.writeTo(filer, type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@@ -163,7 +163,7 @@ internal class AdapterGenerator(
|
|||||||
.initializer("null")
|
.initializer("null")
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
fun prepare(typeHook: (TypeSpec) -> TypeSpec = { it }): PreparedAdapter {
|
fun prepare(generatedProguard: Boolean, typeHook: (TypeSpec) -> TypeSpec = { it }): PreparedAdapter {
|
||||||
val reservedSimpleNames = mutableSetOf<String>()
|
val reservedSimpleNames = mutableSetOf<String>()
|
||||||
for (property in nonTransientProperties) {
|
for (property in nonTransientProperties) {
|
||||||
// Allocate names for simple property types first to avoid collisions
|
// Allocate names for simple property types first to avoid collisions
|
||||||
@@ -181,7 +181,12 @@ internal class AdapterGenerator(
|
|||||||
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
|
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
|
||||||
result.addAnnotation(COMMON_SUPPRESS)
|
result.addAnnotation(COMMON_SUPPRESS)
|
||||||
result.addType(generatedAdapter)
|
result.addType(generatedAdapter)
|
||||||
return PreparedAdapter(result.build(), generatedAdapter.createProguardRule())
|
val proguardConfig = if (generatedProguard) {
|
||||||
|
generatedAdapter.createProguardRule()
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
return PreparedAdapter(result.build(), proguardConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun TypeSpec.createProguardRule(): ProguardConfig {
|
private fun TypeSpec.createProguardRule(): ProguardConfig {
|
||||||
|
@@ -355,6 +355,23 @@ class JsonClassCodegenProcessorTest {
|
|||||||
"Invalid option value for ${JsonClassCodegenProcessor.OPTION_GENERATED}"
|
"Invalid option value for ${JsonClassCodegenProcessor.OPTION_GENERATED}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
fun disableProguardRulesGenerating() {
|
||||||
|
val result = prepareCompilation(
|
||||||
|
kotlin(
|
||||||
|
"source.kt",
|
||||||
|
"""
|
||||||
|
import com.squareup.moshi.JsonClass
|
||||||
|
|
||||||
|
@JsonClass(generateAdapter = true)
|
||||||
|
data class Foo(val a: Int)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
).apply {
|
||||||
|
kaptArgs[JsonClassCodegenProcessor.OPTION_ENABLE_PROGUARD_RULE_GENERATION] = "false"
|
||||||
|
}.compile()
|
||||||
|
assertThat(result.generatedFiles.filter { it.endsWith(".pro") }).isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun multipleErrors() {
|
fun multipleErrors() {
|
||||||
|
Reference in New Issue
Block a user