Skip to content

Commit

Permalink
Rename new annotation to Poko.Skip
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamilton committed Dec 12, 2024
1 parent 3d5e80f commit 9f99ea1
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PokoBuildPlugin : Plugin<Project> {
buildConfigField("COMPILER_PLUGIN_ARTIFACT", "poko-compiler-plugin")
buildConfigField("DEFAULT_POKO_ENABLED", true)
buildConfigField("DEFAULT_POKO_ANNOTATION", "dev/drewhamilton/poko/Poko")
buildConfigField("SKIPPED_ANNOTATION", "dev/drewhamilton/poko/Skipped")
buildConfigField("SKIP_ANNOTATION_SHORT_NAME", "Skip")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions poko-annotations/api/poko-annotations.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public abstract interface annotation class dev/drewhamilton/poko/ArrayContentSup
public abstract interface annotation class dev/drewhamilton/poko/Poko : java/lang/annotation/Annotation {
}

public abstract interface annotation class dev/drewhamilton/poko/Skipped : java/lang/annotation/Annotation {
public abstract interface annotation class dev/drewhamilton/poko/Poko$Skip : java/lang/annotation/Annotation {
}

public abstract interface annotation class dev/drewhamilton/poko/SkippedSupport : java/lang/annotation/Annotation {
public abstract interface annotation class dev/drewhamilton/poko/SkipSupport : java/lang/annotation/Annotation {
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ package dev.drewhamilton.poko
*/
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
public annotation class Poko
public annotation class Poko {

/**
* Primary constructor properties marked with this annotation will be omitted from generated
* `equals`, `hashCode`, and `toString` functions, as if they were not properties.
*
* This annotation has no effect on properties declared outside the primary constructor.
*/
@SkipSupport
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.PROPERTY)
public annotation class Skip
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.drewhamilton.poko

/**
* Denotes an experimental API that enables the ability to skip a Poko class primary constructor property when
* generating Poko functions.
* Denotes an experimental API that enables the ability to skip a Poko class primary constructor
* property when generating Poko functions.
*/
@RequiresOptIn
public annotation class SkippedSupport
public annotation class SkipSupport

/**
* Denotes an API that enables support for array content reading, which is experimental and may
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.drewhamilton.poko.fir

import dev.drewhamilton.poko.BuildConfig.SKIPPED_ANNOTATION
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.diagnostics.AbstractSourceElementPositioningStrategy
Expand All @@ -21,25 +20,21 @@ import org.jetbrains.kotlin.fir.analysis.checkers.hasModifier
import org.jetbrains.kotlin.fir.analysis.extensions.FirAdditionalCheckersExtension
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.declarations.FirRegularClass
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
import org.jetbrains.kotlin.fir.declarations.primaryConstructorIfAny
import org.jetbrains.kotlin.fir.declarations.utils.isData
import org.jetbrains.kotlin.fir.declarations.utils.isInner
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.ClassId

internal class PokoFirCheckersExtension(
session: FirSession,
) : FirAdditionalCheckersExtension(session) {
override val declarationCheckers: DeclarationCheckers =
object : DeclarationCheckers() {
override val regularClassCheckers: Set<FirRegularClassChecker> =
setOf(PokoFirRegularClassChecker(session))
setOf(PokoFirRegularClassChecker)
}

private class PokoFirRegularClassChecker(
private val session: FirSession,
) : FirRegularClassChecker(
private object PokoFirRegularClassChecker : FirRegularClassChecker(
mppKind = MppCheckerKind.Common,
) {
override fun check(
Expand Down Expand Up @@ -73,7 +68,7 @@ internal class PokoFirCheckersExtension(
it.source?.kind is KtFakeSourceElementKind.PropertyFromParameter
}
.filter {
!it.hasAnnotation(ClassId.fromString(SKIPPED_ANNOTATION), session)
matcher.pokoSkipAnnotation(it) == null
}
if (constructorProperties.isEmpty()) {
reporter.reportOn(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.drewhamilton.poko.fir

import dev.drewhamilton.poko.BuildConfig.SKIP_ANNOTATION_SHORT_NAME
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
Expand All @@ -8,6 +9,7 @@ import org.jetbrains.kotlin.fir.extensions.FirExtensionSessionComponent.Factory
import org.jetbrains.kotlin.fir.types.classId
import org.jetbrains.kotlin.fir.types.coneTypeOrNull
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name

internal class PokoFirExtensionSessionComponent(
session: FirSession,
Expand All @@ -19,6 +21,15 @@ internal class PokoFirExtensionSessionComponent(
}
}

fun pokoSkipAnnotation(declaration: FirDeclaration): FirAnnotation? {
val skipAnnotation = pokoAnnotation.createNestedClassId(
name = Name.identifier(SKIP_ANNOTATION_SHORT_NAME),
)
return declaration.annotations.firstOrNull { firAnnotation ->
firAnnotation.classId() == skipAnnotation
}
}

private fun FirAnnotation.classId(): ClassId? {
return annotationTypeRef.coneTypeOrNull?.classId
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.drewhamilton.poko.ir

import dev.drewhamilton.poko.BuildConfig.SKIPPED_ANNOTATION
import dev.drewhamilton.poko.BuildConfig.SKIP_ANNOTATION_SHORT_NAME
import org.jetbrains.kotlin.KtFakeSourceElementKind
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
Expand Down Expand Up @@ -130,7 +130,11 @@ internal class PokoMembersTransformer(
}
}
.filter {
!it.hasAnnotation(ClassId.fromString(SKIPPED_ANNOTATION))
!it.hasAnnotation(
classId = ClassId.fromString(
string = "${pokoAnnotationName}.$SKIP_ANNOTATION_SHORT_NAME"
),
)
}
if (properties.isEmpty()) {
messageCollector.log("No primary constructor properties")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package poko

import dev.drewhamilton.poko.Poko
import dev.drewhamilton.poko.Skipped
import dev.drewhamilton.poko.SkippedSupport
import dev.drewhamilton.poko.SkipSupport

@OptIn(SkippedSupport::class)
@OptIn(SkipSupport::class)
@Suppress("Unused")
@Poko class SkippedProperty(
val id: String,
@Skipped val callback: () -> Unit,
@Poko.Skip val callback: () -> Unit,
)
7 changes: 3 additions & 4 deletions poko-tests/src/commonMain/kotlin/poko/SkippedProperty.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package poko

import dev.drewhamilton.poko.Poko
import dev.drewhamilton.poko.Skipped
import dev.drewhamilton.poko.SkippedSupport
import dev.drewhamilton.poko.SkipSupport

@OptIn(SkippedSupport::class)
@OptIn(SkipSupport::class)
@Suppress("Unused")
@Poko class SkippedProperty(
val id: String,
@Skipped val callback: () -> Unit,
@Poko.Skip val callback: () -> Unit,
)

0 comments on commit 9f99ea1

Please sign in to comment.