有没有一种方法可以强制仅在var属性上使用注释
我正在制作一个注释处理器,以通过法律加密个人数据。因此,为了加密这些字段,它应该能够设置新值,以便我希望我的注释仅在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果仅在注释目标中包括
property_setter
,则注释只能应用于属性设置器,这实际上是您想要的 - 因为属性setter 暗示是可突变的属性。使用此注释时,您通常必须指定使用网站目标:
将其更改为
val
会给您带来错误,就像您期望的那样,因为val
确实没有二传手。ispersonaldatapresent
还需要进行一些修改,以检查设置器是否具有注释: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.When using this annotation, you'd usually have to specify the use site target:
Changing it to
val
would give you an error, just like you expect, because aval
does not have a setter.isPersonalDataPresent
also needs a little modification, to check whether the setter has the annotation: