hev*_*evi 7 java annotations kotlin
我只是想拥有自己的注释来清理注释质量,并在需要时轻松更改它们;
import javax.persistence.Column
import javax.validation.constraints.Size
class Foo(){
@Column(name="bar_", nullable = false, length = 32)
@Size(min = 32, max = 32)
String bar;
@Column(nullable = false, length = 32)
@Size(min = 32, max = 32)
String bas;
@Column(nullable = false, length = 32, unique=true)
@Size(min = 32, max = 32)
String baq;
}
Run Code Online (Sandbox Code Playgroud)
希望我能
class Foo(){
@MyColumn(name="bar_")
String bar;
@MyColumn
String bas;
@MyColumn(unique=true)
String baq;
}
Run Code Online (Sandbox Code Playgroud)
nullable = false, length = 32 是默认参数。
欢迎使用 Java 或 Kotlin 解决方案。
由于您使用的是从第三方导入的注释,因此javax最好的选择是引入复合注释。(Kotlin不支持注释继承。
@Column(name = "bar_", nullable = false, length = 32)
@Size(min = 32, max = 32)
annotation class Anno
Run Code Online (Sandbox Code Playgroud)
Spring Boot 将大量的配置注释组合在一起做得非常好 -看看吧。
复合注释有问题Anno。您必须提供具有常量值的注释参数。
如果您确定,您需要一个参数化注释,例如
@Column(...)
@Size(min = Anno.max, max = Anno.min)
annotation class Anno(val min: Int, val max: Int)
Run Code Online (Sandbox Code Playgroud)
看看Kapt或 Kotlin 编译器插件,您将需要一段代码生成。
使用 Kapt 或 Kotlin 编译器插件,您只需要重写newField自定义方法ClassBuilder:
override fun newField(
origin: JvmDeclarationOrigin,
access: Int,
name: String,
desc: String,
signature: String?,
value: Any?
): FieldVisitor {
// if field is annotated with Anno -- add two custom annotation with parameters of your choice
// otherwise perform a standard field init
}
Run Code Online (Sandbox Code Playgroud)
然后注册它
class AnnoRegister : ComponentRegistrar {
override fun registerProjectComponents(
project: MockProject,
configuration: CompilerConfiguration
) {
...
}
Run Code Online (Sandbox Code Playgroud)
将此处理集成到现有的 gradle 或 maven 项目中应该相对容易,或者只是传递到kotlinc.
| 归档时间: |
|
| 查看次数: |
314 次 |
| 最近记录: |