有没有一种方法可以强制仅在var属性上使用注释

发布于 2025-01-26 13:21:22 字数 1293 浏览 2 评论 0原文

我正在制作一个注释处理器,以通过法律加密个人数据。因此,为了加密这些字段,它应该能够设置新值,以便我希望我的注释仅在var属性上使用强制。

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
annotation class PersonalData {
}

data class CreateAccountCommand(
  @PersonalData
  var fullname: String // it would throw an error if using val since it's not mutable
)

class TestAnnotation {
  @Test
  fun testFindAnnotation() {
    val message = EasyRandom().nextObject(CreateAccountCommand::class.java)
    val wrappedCommand = GenericCommandMessage.asCommandMessage<CreateAccountCommand>(message)
    if (isPersonalDataPresent(wrappedCommand.payload)) {
      encryptData(wrappedCommand.payload)
      wrappedCommand.payload.fullname shouldContain "-encrypted"
    }
  }

  private fun isPersonalDataPresent(payload: Any): Boolean {
    return payload::class.memberProperties.any {
      it.annotations.any { ann -> ann.annotationClass == PersonalData::class }
    }
  }

  private fun encryptData(payload: Any) {
    payload::class.memberProperties.map {
      if (it is KMutableProperty<*>) {
        it.setter.call(payload, "${it.getter.call(payload)}-encrypted")
      }
    }
  }
}

还是更好的方法?

I'm making an annotation processor for encrypting personal data due to law. So in order to encrypt those field it should be able to set new value so I want my annotation to force using only on a var property.

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
annotation class PersonalData {
}

data class CreateAccountCommand(
  @PersonalData
  var fullname: String // it would throw an error if using val since it's not mutable
)

class TestAnnotation {
  @Test
  fun testFindAnnotation() {
    val message = EasyRandom().nextObject(CreateAccountCommand::class.java)
    val wrappedCommand = GenericCommandMessage.asCommandMessage<CreateAccountCommand>(message)
    if (isPersonalDataPresent(wrappedCommand.payload)) {
      encryptData(wrappedCommand.payload)
      wrappedCommand.payload.fullname shouldContain "-encrypted"
    }
  }

  private fun isPersonalDataPresent(payload: Any): Boolean {
    return payload::class.memberProperties.any {
      it.annotations.any { ann -> ann.annotationClass == PersonalData::class }
    }
  }

  private fun encryptData(payload: Any) {
    payload::class.memberProperties.map {
      if (it is KMutableProperty<*>) {
        it.setter.call(payload, "${it.getter.call(payload)}-encrypted")
      }
    }
  }
}

or any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-02-02 13:21:22

如果仅在注释目标中包括property_setter,则注释只能应用于属性设置器,这实际上是您想要的 - 因为属性setter 暗示是可突变的属性。

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY_SETTER)
annotation class PersonalData

使用此注释时,您通常必须指定使用网站目标:

data class CreateAccountCommand(
    @set:PersonalData
    var fullname: String
)

将其更改为val会给您带来错误,就像您期望的那样,因为val确实没有二传手。

ispersonaldatapresent还需要进行一些修改,以检查设置器是否具有注释:

return payload::class.memberProperties
    .filterIsInstance<KMutableProperty<*>>()
    .any {
        it.setter.annotations.any { ann ->
            ann.annotationClass == PersonalData::class
        }
    }

If you only include PROPERTY_SETTER in the annotation targets, the annotation can only be applied to property setters, which is effectively what you want - since a property setter implies a mutable property.

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY_SETTER)
annotation class PersonalData

When using this annotation, you'd usually have to specify the use site target:

data class CreateAccountCommand(
    @set:PersonalData
    var fullname: String
)

Changing it to val would give you an error, just like you expect, because a val does not have a setter.

isPersonalDataPresent also needs a little modification, to check whether the setter has the annotation:

return payload::class.memberProperties
    .filterIsInstance<KMutableProperty<*>>()
    .any {
        it.setter.annotations.any { ann ->
            ann.annotationClass == PersonalData::class
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文