Tag: 注释

Kotlin对委托属性的注释

在Kotlin中,是否有一种方法可以在委托属性(例如: lazy )上定义注释? class MyActivity: Activity() { @ColorInt val textColor: Int by lazy { ContextCompat.getColor(this, R.color.someColor) } … IDE在@ColorInt注释中引发错误: 这个注解不适用于'具有委托的成员财产'

在Kotlin嵌套注释

在Java中,可以创建如下所示的嵌套注释: public @interface InnerInnotation { String value(); } public @interface OuterAnnotation { InnerAnnotation[] value() default { @InnerAnnotation("Hello"), @InnerAnnotation("World") } } annotation class InnerAnnotation(val value: String) 但是当我试图在Kotlin中做同样的事情时,我得到一个编译错误: annotation class OuterAnnotation( // Next line doesn't compile: "Annotation class cannot be instantiated" val value: Array<InnerAnnotation> = arrayOf(InnerAnnotation("Test")) ) 但是,单个实例注记字段正常工作: annotation class OuterAnnotation( val value: InnerAnnotation = InnerAnnotation("Hello World") […]

Kotlin总是空的Field Annotation

我有以下Kotlin注释 @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY_GETTER) @Retention(AnnotationRetention.RUNTIME) annotation class Field(val value: String) 和下面的测试代码 class TestObject(@field:Field("id") val id: Long) { @field:Field("string") val string = "Hello world" @get:Field("prop") val prop get() = string } class AnnotationTest { @Test fun test() { val obj = TestObject(200L) for (member in obj::class.declaredMemberProperties) { if (member.findAnnotation<Field>() != null) { println(member) } println(member) println(member.annotations) } […]

Kotlin – 如何获得注释属性值

说,我有一个Kotlin类与注释: @Entity @Table(name="user") data class User (val id:Long, val name:String) 我怎样才能从@Table注释获得名称属性的值? fun <T> tableName(c: KClass<T>):String { // i can get the @Table annotation like this: val t = c.annotations.find { it.annotationClass == Table::class } // but how can i get the value of "name" attribute from t? }

Kotlin可重复@annotations不能在jdk-8上工作

我已经在@Parameter中声明了一个可重复的注解 kotlin如下: @Repeatable annotation class Parameter(val name: String); 但是当我如下所示使用它时,编译器会报告一个错误: 1.8之前的JVM版本只能重复带有源保留的注释 @Parameter("foo") @Parameter("bar") fun repeat() = 1; 我确定我正在使用jdk-8 kotlin 。 而对于kotlin-1.1.2 gradle插件,选项jvmTarget也被设置为1.8 。 问:为什么它不能正常工作? sourceCompatibility = 1.8 targetCompatibility = 1.8 compileKotlin { kotlinOptions{ jvmTarget = "1.8" } }

在Kotlin中枚举注释

我有一个由Gson序列化/反序列化的枚举: enum class PacketType { NONE; [SerializedName("request")] REQUEST; [SerializedName("response")] RESPONSE; [SerializedName("event")] EVENT; } 不幸的是,我注意到Gson忽略了SerializedName注解,并使用枚举值的大写名称。 我决定找出为什么序列化不能按预期工作,并发现Kotlin删除枚举值的所有注释。 我怎样才能使这些注释出现在生成的字节码?

是否可以注释Kotlin中的类构造函数?

澄清在kotlin命中1.0版之前,这个问题被问到。 示例中的语言语法现在已经过时,请按照官方文档。 我正在玩kotlin和春天DI 。 我想使用基于构造函数的依赖注入,所以我需要注释构造函数。 我尝试了下面的方法: Configuration Import(javaClass<DataSourceConfig>()) public open class AppConfig(dataSource: DataSource) { private val dataSource: DataSource Autowired { this.dataSource = dataSource } } Configuration public open class DataSourceConfig { Bean public open fun dataSource(): DataSource { // source omitted } } 但它不起作用。 甚至有可能在kotlin中注释构造函数吗? PS我正在使用Kotlin M10.1和Spring 4.1.4 更新:在kotlin中注释构造函数是可能的。 问题是,不允许使用基于构造函数的DI进行@Configuration

在kotlin注释错误?

这里有两个代码示例 Java的: public class Q { @Retention(RetentionPolicy.SOURCE) @IntDef({LOL.one, LOL.two}) @interface Lol{} public final class LOL{ public final static int one = 1; public final static int two = 2; } public Q(){ q(1); } void q (@Lol int q){ } } 科特林: class Q { @Retention(AnnotationRetention.SOURCE) @IntDef(LOL.one, LOL.two) internal annotation class Lol object LOL { […]

Kotlin注释IntDef

我有这个代码示例: class MeasureTextView: TextView { constructor(context: Context?) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) companion object{ val UNIT_NONE = -1 val UNIT_KG = 1 val UNIT_LB = 0 } fun […]