Skip to content

Commit

Permalink
Remove the legacy options (#285)
Browse files Browse the repository at this point in the history
The `kotshi.createAnnotationsUsingConstructor` and
`kotshi.useLegacyDataClassRenderer` options have been removed.
  • Loading branch information
ansman authored Dec 29, 2023
1 parent 6d521ef commit 877fcde
Show file tree
Hide file tree
Showing 12 changed files with 6 additions and 532 deletions.
24 changes: 1 addition & 23 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '19'
- uses: gradle/gradle-build-action@v2
with:
arguments: check --continue --stacktrace -Pkotshi.createAnnotationsUsingConstructor=false
testWithAnnotations:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '19'
- uses: gradle/gradle-build-action@v2
with:
arguments: check --continue --stacktrace
testWithLegacyDataClassRenderer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '19'
- uses: gradle/gradle-build-action@v2
with:
arguments: check --continue --stacktrace -Pkotshi.useLegacyDataClassRenderer=true
testOldestSupportedMoshi:
runs-on: ubuntu-latest
steps:
Expand All @@ -58,7 +36,7 @@ jobs:
arguments: check --continue --stacktrace -Pkotshi.internal.useLegacyMoshi=true
deploySnapshot:
runs-on: ubuntu-latest
needs: [test, testWithAnnotations, testWithLegacyDataClassRenderer, testOldestSupportedMoshi]
needs: [test, testOldestSupportedMoshi]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
Expand Down
63 changes: 0 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,69 +106,6 @@ that extends `JsonAdapter` with `@RegisterJsonAdapter` and Kotshi will generate

### Options

#### `kotshi.createAnnotationsUsingConstructor`
This option enables a new way of creating annotations instances at runtime. Normally Kotshi uses reflection to create
the qualifier annotations but as of 1.5.30 of Kotlin you can enable creating annotations by calling the constructor.

This behavior is enabled by default when using language version 1.6 but can be explicitly enabled or disabled using this
option.

Examples:
<details open>
<summary>KSP</summary>

```kotlin
ksp {
arg("kotshi.createAnnotationsUsingConstructor", "false")
}
```
</details>


<details>
<summary>KAPT</summary>

```kotlin
kapt {
arguments {
arg("kotshi.createAnnotationsUsingConstructor", false)
}
}
```
</details>

See more about instantiating annotations here: https://kotlinlang.org/docs/whatsnew1530.html#instantiation-of-annotation-classes

#### `kotshi.useLegacyDataClassRenderer`
This option allows you to use the old way of creating classes with parameters that has default values.

From 2.10.0 reflection is used create data classes, but by setting this option to `true` the old behavior can be used
instead which used the `copy` method.

Examples:
<details open>
<summary>KSP</summary>

```kotlin
ksp {
arg("kotshi.createAnnotationsUsingConstructor", "false")
}
```
</details>


<details>
<summary>KAPT</summary>

```kotlin
kapt {
arguments {
arg("kotshi.useLegacyDataClassRenderer", true)
}
}
```
</details>

#### `kotshi.generatedAnnotation`
This option tells Kotshi to add the `@Generated` annotation to all generated classes which is disabled by default.

Expand Down
2 changes: 0 additions & 2 deletions compiler/src/main/kotlin/se/ansman/kotshi/Options.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package se.ansman.kotshi

object Options {
const val createAnnotationsUsingConstructor = "kotshi.createAnnotationsUsingConstructor"
const val useLegacyDataClassRenderer = "kotshi.useLegacyDataClassRenderer"
const val generatedAnnotation = "kotshi.generatedAnnotation"

val possibleGeneratedAnnotations = setOf(Types.Java.generatedJDK9, Types.Java.generatedPreJDK9)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class AdaptersProcessingStep(
private val types: Types,
private val elements: Elements,
private val generatedAnnotation: GeneratedAnnotation?,
private val createAnnotationsUsingConstructor: Boolean?,
private val useLegacyDataClassRenderer: Boolean,
) : KotshiProcessor.GeneratingProcessingStep() {
override val annotations: Set<Class<out Annotation>> =
setOf(
Expand Down Expand Up @@ -146,8 +144,6 @@ class AdaptersProcessingStep(
adapters += generator.generateAdapter(
generatedAnnotation = generatedAnnotation,
filer = filer,
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor,
useLegacyDataClassRenderer = useLegacyDataClassRenderer
)
} catch (e: KaptProcessingError) {
messager.logKotshiError(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class FactoryProcessingStep(
private val generatedAnnotation: GeneratedAnnotation?,
private val generatedAdapters: List<GeneratedAdapter<Element>>,
private val metadataAccessor: MetadataAccessor,
private val createAnnotationsUsingConstructor: Boolean?,
) : KotshiProcessor.GeneratingProcessingStep() {

@OptIn(ExperimentalKotshiApi::class)
Expand Down Expand Up @@ -101,9 +100,7 @@ class FactoryProcessingStep(
manuallyRegisteredAdapters = manuallyRegisteredAdapters,
)

val createAnnotationsUsingConstructor =
createAnnotationsUsingConstructor
?: metadataAccessor.getMetadata(element).supportsCreatingAnnotationsWithConstructor
val createAnnotationsUsingConstructor = metadataAccessor.getMetadata(element).supportsCreatingAnnotationsWithConstructor

JsonAdapterFactoryRenderer(factory, createAnnotationsUsingConstructor)
.render(generatedAnnotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import javax.lang.model.util.Types
@AutoService(Processor::class)
@IncrementalAnnotationProcessor(AGGREGATING)
class KotshiProcessor : AbstractProcessor() {
private var createAnnotationsUsingConstructor: Boolean? = null
private var useLegacyDataClassRenderer: Boolean = false
private var generatedAnnotation: GeneratedAnnotation? = null
private lateinit var elements: Elements
private lateinit var types: Types
Expand All @@ -49,8 +47,6 @@ class KotshiProcessor : AbstractProcessor() {
types = types,
elements = processingEnv.elementUtils,
generatedAnnotation = generatedAnnotation,
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor,
useLegacyDataClassRenderer = useLegacyDataClassRenderer,
),
FactoryProcessingStep(
processor = this,
Expand All @@ -61,16 +57,13 @@ class KotshiProcessor : AbstractProcessor() {
generatedAnnotation = generatedAnnotation,
generatedAdapters = adapters,
metadataAccessor = metadataAccessor,
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor,
)
)
}

@Synchronized
override fun init(processingEnv: ProcessingEnvironment) {
super.init(processingEnv)
createAnnotationsUsingConstructor = processingEnv.options[Options.createAnnotationsUsingConstructor]?.toBooleanStrict()
useLegacyDataClassRenderer = processingEnv.options[Options.useLegacyDataClassRenderer]?.toBooleanStrict() ?: useLegacyDataClassRenderer
generatedAnnotation = processingEnv.options[Options.generatedAnnotation]
?.let { name ->
Options.possibleGeneratedAnnotations[name] ?: run {
Expand All @@ -95,7 +88,7 @@ class KotshiProcessor : AbstractProcessor() {
override fun getSupportedAnnotationTypes(): Set<String> =
getSupportedAnnotationClasses().mapTo(mutableSetOf()) { it.canonicalName }

override fun getSupportedOptions(): Set<String> = setOf("kotshi.createAnnotationsUsingConstructor")
override fun getSupportedOptions(): Set<String> = setOf(Options.generatedAnnotation)

override fun process(annotations: Set<TypeElement>, roundEnv: RoundEnvironment): Boolean {
if (!roundEnv.processingOver()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ abstract class AdapterGenerator(
fun generateAdapter(
generatedAnnotation: GeneratedAnnotation?,
filer: Filer,
createAnnotationsUsingConstructor: Boolean?,
useLegacyDataClassRenderer: Boolean,
): GeneratedAdapter<Element> {
when {
kmClass.isInner ->
Expand All @@ -76,9 +74,7 @@ abstract class AdapterGenerator(

val generatedAdapter = getGeneratableJsonAdapter()
.createRenderer(
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor
?: metadataAccessor.getMetadata(targetElement).supportsCreatingAnnotationsWithConstructor,
useLegacyDataClassRenderer = useLegacyDataClassRenderer,
createAnnotationsUsingConstructor = metadataAccessor.getMetadata(targetElement).supportsCreatingAnnotationsWithConstructor,
error = { KaptProcessingError(it, targetElement) },
)
.render(generatedAnnotation, originatingElement = targetElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ import se.ansman.kotshi.model.findKotshiConstructor
import se.ansman.kotshi.renderer.JsonAdapterFactoryRenderer

class KotshiSymbolProcessor(private val environment: SymbolProcessorEnvironment) : SymbolProcessor {
private val createAnnotationsUsingConstructor = environment.options[Options.createAnnotationsUsingConstructor]
?.toBooleanStrict()
?: environment.kotlinVersion.isAtLeast(1, 6)

private val useLegacyDataClassRenderer = environment.options[Options.useLegacyDataClassRenderer]
?.toBooleanStrict()
?: false
private val createAnnotationsUsingConstructor = environment.kotlinVersion.isAtLeast(1, 6)

private val generatedAnnotation = environment.options[Options.generatedAnnotation]
?.let { name ->
Expand Down Expand Up @@ -329,7 +323,6 @@ class KotshiSymbolProcessor(private val environment: SymbolProcessorEnvironment)

generator.generateAdapter(
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor,
useLegacyDataClassRenderer = useLegacyDataClassRenderer,
generatedAnnotation = generatedAnnotation,
)
} catch (e: KspProcessingError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ abstract class AdapterGenerator(

fun generateAdapter(
createAnnotationsUsingConstructor: Boolean,
useLegacyDataClassRenderer: Boolean,
generatedAnnotation: GeneratedAnnotation?,
): GeneratedAdapter<KSFile> {
when {
Expand All @@ -81,7 +80,6 @@ abstract class AdapterGenerator(
val generatedAdapter = getGeneratableJsonAdapter()
.createRenderer(
createAnnotationsUsingConstructor = createAnnotationsUsingConstructor,
useLegacyDataClassRenderer = useLegacyDataClassRenderer,
error = { KspProcessingError(it, targetElement) },
)
.render(generatedAnnotation, originatingElement = targetElement.containingFile!!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,10 @@ abstract class AdapterRenderer(private val adapter: GeneratableJsonAdapter) {

fun GeneratableJsonAdapter.createRenderer(
createAnnotationsUsingConstructor: Boolean,
useLegacyDataClassRenderer: Boolean,
error: (String) -> Throwable,
): AdapterRenderer =
when (this) {
is DataClassJsonAdapter ->
if (useLegacyDataClassRenderer) {
LegacyDataClassAdapterRenderer(this, createAnnotationsUsingConstructor)
} else {
DataClassAdapterRenderer(this, createAnnotationsUsingConstructor)
}
is DataClassJsonAdapter -> DataClassAdapterRenderer(this, createAnnotationsUsingConstructor)
is EnumJsonAdapter -> EnumAdapterRenderer(this)
is ObjectJsonAdapter -> ObjectAdapterRenderer(this)
is SealedClassJsonAdapter -> SealedClassAdapterRenderer(this, error)
Expand Down
Loading

0 comments on commit 877fcde

Please sign in to comment.