从Java调用Kotlin内联函数

Exceptions.kt: @Suppress("NOTHING_TO_INLINE") inline fun generateStyleNotCorrectException(key: String, value: String) = AOPException(key + " = " + value) 在kotlin: fun inKotlin(key: String, value: String) { throw generateStyleNotCorrectException(key, value) } 它在kotlin中运行,并且函数被内联。 但是在Java代码中使用时,它不能被内联,并且仍然是一个正常的静态方法调用(从反编译的内容中看到)。 像这样的东西: public static final void inJava(String key, String value) throws AOPException { throw ExceptionsKt.generateStyleNotCorrectException(key, value); // when decompiled, it has the same contents as before , […]

kotlin反射检查可空类型

我怎样才能测试一个KType变量是否包含一个可为空的kotlin类型的值(eG Int?)? 我有 var type: KType 来自KProperty<*>.returnType变量,我需要检测它是否等于某些kotlin类型(Int,Long等)。 这适用于: when (type) { Int::class.defaultType -> … Long::class.defaultType -> … else -> … } 但是这只适用于不可为空的类型,所以第一个分支不匹配Int? 然而,我还没有弄清楚,我可以检测到类型是Int? 其他则显而易见,但不是那么好 type.toString().equals("kotlin.Int?")

咖啡测试Kotlin

每当我尝试测试我的Android应用程序,并重写我在Kotlin的测试,我总是得到: 未找到类:“my.package.modules.container.ContainerViewInstrumentedTest” 空的测试套件。 我正在尝试一切,使浓咖啡看到我的测试…但没有运气。 有谁知道什么是错的?

Kotlin中是否有一个didSet / willSet类似物?

我喜欢这个Swift语法; 这对许多事情非常有帮助: var foo: Bar = Bar() { willSet { baz.prepareToDoTheThing() } didSet { baz.doTheThing() } } 我很想在Kotlin做这个。 但是, 我找不到合适的语法 ! Kotlin有这样的东西吗? var foo: Bar = Bar() willSet() { baz.prepareToDoTheThing() } didSet() { baz.doTheThing() }

如何解析kotlin代码?

我需要分析kotlin文件代码,检测关键字“data”和“?”。 问题是我没有找到像JavaParser任何库。 我不需要强大的工具,只是返回给我的行数。 任何想法?

ByteArray浮在kotlin

我有一个4字节的数组,代表一个浮点值。 由于kotlin缺乏按位操作的字节我怎么能把它转换为最佳的方式浮动?

Kotlin – 与属性问题的工厂类

我试图在Kotlin写工厂类。 在Java中: public class MyFactory { private static MyFactory instance = null; private Properties props = null; private FirstClass firstInstance = null; private SecondClass secondInstance = null; private MyFactory() { props = new Properties(); try { props.load(new FileInputStream("path/to/config")); String firstClass = props.getProperty(“first.class”); String secondClass = props.getProperty(“second.class”); firstInstance = (FirstClass) Class.forName(firstClass).newInstance(); secondInstance = (SecondClass) Class.forName(secondClass).newInstance(); } […]

卡拉Kotlin为网络,未来的集成

非常有兴趣使用Kara Kotlin开始一个项目,只有一些功能可以从中受益: 与Kotlin的Javascript编译器集成,从Kotlin代码生成Javascript(目前支持常规Javascript) 和 资产编制和缩小 我注意到他们的网站,他们正在积极开发这些。 这些功能是否有ETA?

Kotlin转换为Javascript错误:“减法不是函数”

将Kotlin代码转换为JavaScript时遇到问题。 每当我使用减法或加法,它将转换为无效的JavaScript代码。 我有一个Kotlin JavaScript项目,配置了Gradle。 编译器被配置为生成输出到commonjs,我创建了一个简单的NodeJS应用程序来执行生成的代码。 原始Kotlin文件: import kotlin.js.JsName @JsName("testSubtraction") fun testSubtraction(a: Long, b: Long) : Long { return a – b } 生成的输出文件: module.exports = function (Kotlin) { 'use strict'; var _ = Kotlin.defineRootPackage(null, /** @lends _ */ { testSubtraction: function (a, b) { return a.subtract(b); } }); Kotlin.defineModule('output', _); return _; }(require('kotlin')); //@ sourceMappingURL=output.js.map […]

在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") […]