Tag: 注释

在kotlin中使用时,@XmlElement不起作用

当我序列化一个类ReturnValue的实例时,我发现@XmlElement不起作用。 生成的XML仍然有<summary>的标签,而不是<comment> 。 ReturnValue类: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) data class ReturnValue(val type: String, @XmlElement(name="comment") val summary: String){ constructor(): this(type="java.lang.Object", summary="no summary") } 测试程序: fun main(args: Array<String>) { val jaxbContext = JAXBContext.newInstance(ReturnValue::class.java) val marshaller = jaxbContext.createMarshaller() marshaller.marshal( ReturnValue(type = "java.lang.Object",summary = "hello"), System.out) } 和输出: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><returnValue><type>type2</type><summary>hello</summary></returnValue> 所以,我想把<summary>改成<comment> 。 我怎样才能做到这一点?

Kotlin反射无法访问使用网站的目标注释

我使用注释网站目标@get困惑了我一会儿。 class GetSiteTarget1(@get:Annotation val value: Int = 1); class GetSiteTarget2 { val value = 1 @Annotation get () { return field; } }; 上面两个类都有不同的结果: GetSiteTarget1::value.getter.annotations; // not has @Annotation GetSiteTarget1::value.getter.javaMethod!!.annotations // has @Annotation GetSiteTarget2::value.getter.annotations; // has @Annotation GetSiteTarget2::value.getter.javaMethod!!.annotations // has @Annotation 而且这两个类都用@Annotation注解: RuntimeVisibleAnnotations: 0: #10() // @Annotation 谁能告诉我为什么?

kapt:如何处理测试资源?

我有一个使用kapt进行注释处理和代码生成的项目(基于注释)。 它在主要来源工作,但不在测试来源。 一些来源(例如, 如何在androidTest范围内使用kapt )建议运行gradle kaptTest ,但这也不起作用。 它将任务报告为“最新”,即使在清理完毕后也是如此。 也许这个建议是唯一的android。 我从https://github.com/JetBrains/kotlin-examples/tree/master/gradle/kotlin-code-generation下载了示例项目,在测试源中添加了一个注释用法,我在那里得到了相同的行为。 它适用于主要来源,不适用于测试来源。 输出中唯一奇怪的是: > Task :example:compileKotlin Using kotlin incremental compilation w: [kapt] Sources output directory is not specified, skipping annotation processing 但是正如你所看到的,这不是测试来源,而是主要来源,而注释处理器就是应用在那些上。 我在注释处理器中添加了一个打印输出。 它显示为compileKotlin,但不是compileTestKotlin。 那么,使kapt在测试源上工作的魔术是什么? PS:我想有人会问我的build.gradle。 这与我链接的例子是一样的,所以如果你能使它适用于这个例子,我可以集成到我的构建文件中。 PPS:我发现只有类似的问题是Kotlin的kapt插件for gradle不能用于自定义源集(JMH) ,他们建议发布一个bug报告。 所以也许这是kapt-gradle-plugin中的一个bug?

注释类型的扩展功能

是否有可能像这样在注释类型上定义Kotlin扩展函数? @ColorInt fun @ColorInt Int.darken(): Int { return ColorUtils.blendARGB(this, Color.BLACK, 0.2f) } 替代形式: @ColorInt fun (@ColorInt Int).darken(): Int { return ColorUtils.blendARGB(this, Color.BLACK, 0.2f) } 这将对应于以下静态功能: @ColorInt fun darken(@ColorInt color: Int): Int { return ColorUtils.blendARGB(color, Color.BLACK, 0.2f) } 我不认为使用Kotlin是可能的,但是在Kotlin的后续版本中可以添加这个功能吗? 注意:同样的问题也适用于@[resource type]Res @StringDef , @[resource type]Res @StringDef , @[resource type]Res 。

如何在Kotlin中列出字段注释?

我有一个注释 public @interface Field { String value(); } 和java类,由它注释: public class Animal { @Field("name") private String name; } 我尝试通过下一个代码列出所有字段的注释: for(field in clazz.declaredFields){ for(annotation in field.annotations){ when(annotation){ is Field -> { //do something } } } } 其中clazz是Class<T> 但是field.annotations是空的。 如何正确列出注释?

我可以从命令行或从Ant调用Kotlin noargs插件吗?

Kotlin有一个noarg编译器插件 ,可以用来为特殊注释类生成合成的无参数构造函数。 文档页面清楚地解释了如何在Maven和Gradle中使用它,但是不清楚它是否可以使用,或者如何使用它,无论是从Ant还是在命令行使用kotlinc 。 到目前为止,我一直无法做到这一点。 这是我的NoArgs.kt源文件: package test annotation class NoArgs @NoArgs data class Test(val x: String, val y: String) 以下是我尝试过的一些命令行,根本没有任何作用: $ kotlinc -verbose "-Xplugin:$KOTLIN_HOME/lib/noarg-compiler-plugin.jar -P plugin:org.jetbrains.kotlin.noarg:annotation=test.NoArgs NoArgs.kt 和: $ kotlinc -verbose "-Xplugin=$KOTLIN_HOME/lib/kotlin-annotation-processing.jar;$KOTLIN_HOME/lib/noarg-compiler-plugin.jar;$JRE_HOME/lib/tools.jar" -P plugin:org.jetbrains.kotlin.noarg:annotation=test.NoArgs NoArgs.kt 当我运行$ javap test.Test ,没有生成无参数构造函数的证据。 还有一些证据表明,插件并没有运行,事实上,如果我把垃圾,而不是-P选项的单词注释 ,没有错误信息或行为改变。 如果你能告诉我如何使用kotlinc或Ant来运行它,我真的很感激它!

注释Kotlin中的属性时,注释的默认目标是什么?

Kotlin中的注释可以具有不同的使用地点目标,如下所述: https : //kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets 我的问题是:当use-site没有明确定义的时候,像下面这个例子那样在注释一个类的属性的时候默认的目标是什么? class Test { @SomeAnnotation var someProperty: String? = null } 背景 我正在尝试Jongo作为Kotlin中的MongoDB客户端,并注意到id字段的问题。 Jongo没有正确映射id属性,如下所示: @MongoId @MongoObjectId var id: String? = null 这些注释只是Jackson的元注释。 但是,当我注释这样的属性,它似乎工作,指示使用现场问题: @field:[MongoId MongoObjectId] var id: String? = null 我期望@field是默认的使用网站,但它似乎不是。

什么是有限通配符类型的Java注释字段的kotlin equivlant?

说我有这样的Java注释: public @interface Foo { Class<? extends Bar> value(); } public interface Bar {} 我应该如何将Foo翻译成kotlin? 在这种情况下,我只能切换回Java吗?

在Kotlin项目中使用的传统Java库中保持零安全

假设我在旧/旧Java库中有特定的代码: public class JavaClass { private String notNullString; private String nullableString; private String unannotatedString; public JavaClass(@NotNull String notNullString, @Nullable String nullableString, String unannotatedString) { this.notNullString = notNullString; this.nullableString = nullableString; this.unannotatedString = unannotatedString; } @NotNull public String getNotNullString() { return notNullString; } @Nullable public String getNullableString() { return nullableString; } public String getUnannotatedString() { return […]

Kotlin注释处理:是否可以生成扩展方法?

我正在和Kotlin玩耍,我觉得有一个“ 功能课 ”真是太好了,看看我想要的例子: functional class MyStrFunction(val function: MyStrFunction.(String) -> String) { val memory = HashMap<String, String>() fun output(message: String) = println(message) } 这个想法是,我可以像这样使用它: val f = MyStrFunction { output("reversing $it"); if (it in memory) output(memory[it]) return it.reverse() } 如果我要写我自己想要的MyStrFunction ,最终的类将是: class MyStrFunction(val function: MyStrFunction.(String) -> String) { val memory = HashMap<String, String>() fun output(message: String) […]