Tag: jvm

我怎样才能确定一个Kotlin类型将被映射到一个Java类型?

上下文 Kotlin的kotlin.String类型目前定义如下(1.1.2) : public class String : Comparable<String>, CharSequence { companion object {} // Operator and override function definitions. } 在kotlin.String定义的一些扩展将接收实例转换为java.lang.String类型以转发方法调用。 例如(1.1.2) : @kotlin.internal.InlineOnly public inline fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase() 然而, kotlin.String类型定义中没有任何东西可以让我清楚,这个转换是保证成功的。 问题 有什么方法可以轻松确定是否有任何给定的Kotlin类型以这种方式映射到相应的Java类型? 这个转换实际发生在哪里? (如果可能,请查找相关源代码的链接。)

无法使用JAXB和Kotlin解组xml对象

我正试图反序列化/解组包含People标记中包含的Person元素集合的XML。 我试图使用JAXB在Kotlin中反序列化这个XML。 所有东西都编译和运行,但是我班里的人员总是空的。 我怎样解组这个集合? import java.io.StringReader import javax.xml.bind.JAXB import javax.xml.bind.annotation.XmlAccessType import javax.xml.bind.annotation.XmlAccessorType import javax.xml.bind.annotation.XmlElement import javax.xml.bind.annotation.XmlElementWrapper @XmlAccessorType(XmlAccessType.FIELD) data class Person( var name:String = "", var age:Int = 0 ) @XmlAccessorType(XmlAccessType.FIELD) data class Report( var statusCode:Int = 0, @XmlElementWrapper(name = "people") @XmlElement(name = "person") var people:List<Person>? = null ) val xml = """ <report> <statusCode>3</statusCode> <people> […]

Kotlin Back-Tick在方法名称中转义:它是如何工作的?

我发现,在Kotlin中可以命名这样的方法: fun `i am a test method`(){ Assert.assertEquals("x", "x") } 编译器生成一个带有下划线而不是空格的方法:“i_am_a_test_method”,这似乎是合理的,因为JVM 不允许带空格的方法。 Junit和/或Gradle如何使用备份名称报告这些测试?

Kotlin的指定类型是否对于JVM上的原语不正确?

如果一个Kotlin函数调用引用了一个原语,比如说Int ,那么这个“被传递”的类就是盒装原语的类,而不是未装箱的版本。 inline fun <reified T> reify() = T::class @Test fun reified_type_doesnt_match_for_primitive() { assertNotEquals(Int::class, reify<Int>()) assertNotEquals(Int::class.java, reify<Int>().java) assertNotEquals<Any>(Int::class, reify<Int?>()) val nullableInt: Int? = 42 assertNotEquals(nullableInt!!.javaClass.kotlin, reify<Int>()) assertEquals<Any>(java.lang.Integer::class.java, reify<Int>().java) } @Test fun reified_type_matches_for_class() { assertEquals(String::class, reify<String>()) } 这是一个错误?

包在一个JAR中的Kotlin .kt类

如何构建HelloWorld.kt作为JAR以便运行? thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ kotlinc HelloWorld.kt –include-runtime -d HelloWorld.jar error: invalid argument: –include-runtime info: use -help for more information thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ thufir@dur:~/kotlin$ kotlinc HelloWorld.kt -d HelloWorld.jar WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean) WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory WARNING: Use –illegal-access=warn […]

如何在内存中运行编译kotlin文件的测试并检查结果?

到目前为止,我有 import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler MyProjectCompiler.initialize("SampleKtFileOutput") .packageName("com.test.sample") .compile(File(someFile.path)) .result { ktSource: String -> K2JVMCompiler() .exec(System.out, /** arguments here?*/) } 这个手动启动编译器,但我想从第一个编译器(生成kotlin源的MyProjectCompiler生成的字符串在内存中,并检查结果而不写入文件。 如果可能的话,我想包括当前类路径中的所有内容。

为什么Scala编译器在将Kotlin密封类传递给构造函数时给我一个错误?

我有一个密封的课,写在科特林: sealed class Schema { class RecordSchema(val fields: List<Field>): Schema() class ArraySchema(val elementSchema: Schema): Schema() … } 另一个将RecordSchema作为参数的类: class Enrichment(config: Config, val schema: RecordSchema) { … } 在Scala中,我有一个类,其中包含一个RecordSchema实例,然后创建一个Enrichment实例。 object Job { def main(args: Array[String]): Unit = { /// some initializing of resources… and then… val recordSchema = schemas.getSchema(id) // type is Schema.RecordSchema val enrichment = […]

Kotlin:为什么! 运算符为jvm编译时生成空检查?

我使用https://javap.yawk.at/来检查Kotlin生成的字节码。 我发现,每当! 运算符被使用,相应的空检查被生成。 例如,对于以下kotlin代码: fun main(args: Array<String>) { var a : Int? = null; println(a!!+2) } 这个代码是生成的: public final class MainKt { public static final void main(@NotNull final String[] args) { Intrinsics.checkParameterIsNotNull((Object)args, "args"); final Integer n; final Integer a = n = null; if (n == null) { Intrinsics.throwNpe(); } System.out.println(((Number)n).intValue() + 2); } } […]

Kotlin初始化一个对象

我有一个基类,我正在扩展,但想膨胀一个普通的Java构造函数将视图。 class TextView(context: Context?) : ViewAbstractClass(context) 我不知道如何在Kotlin做到这一点。 Kotlin有什么构造可以让你对对象进行复杂的初始化?

将kotlin代码编译为JVM和JavaScript

我真的很喜欢编写一个框架的想法,然后能够将其编译为jvm字节代码以及用于Web的JavaScript使用。 这是目前可能与kotlin编译器?