Kotlin,JPA和布尔字段

我开始在我们的项目中引入kotlin,并将一些实体转换为kotlin,作为更大的重构的一部分。 我的实体有一个布尔活动属性: private boolean active = true; public boolean isActive() { return active; } public void setActive(final boolean active) { this.active = active; } 现在在kotlin这应该是: var isActive: Boolean = true 问题是,这样我不得不重构现有的查询,这不是什么大问题,但我期待着更平滑的过渡。 我可以做这样的事情: var active: Boolean = true val isActive: Boolean get()= active 但是感觉不对。 什么是最好的方法?

Kotlin Kapt:java.lang.IllegalStateException:endPosTable已经设置

试图使用Kotlin maven插件来构建一个kotlin应用程序。 使用Dagger 2依赖注入框架。 调用时出现以下错误 mvn clean compile package 也可以通过调用来复制 mvn clean package 然后打电话 mvn package 在maven 3和java 8中似乎有一个错误,但是我找不到kotlin maven插件的解决方法。 [ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.1.3-2:kapt (kapt) on project process: Compilation failure [ERROR] java.lang.IllegalStateException: failed to analyze: java.lang.IllegalStateException: endPosTable already set [ERROR] at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57) [ERROR] at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.kt:344) [ERROR] at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileBunchOfSources(KotlinToJVMBytecodeCompiler.kt:226) [ERROR] at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:200) [ERROR] at […]

Kotlin:方法参考不起作用?

看来我无法在Kotlin中使用对象的方法引用。 这个特性存在于Java中。 例如在Java中,如果我通过字符串循环来将每个字符附加到一个作者: string.forEach(writer::append); 但在Kotlin使用相同的语法不起作用,因为:

Kotlin反射操作符得到实现

我正在学习Kotlin,目前使用的是Fedora 25 OpenJDK 8和Kotlin 1.1。 我复制了Kotlin网站的示例: https : //kotlinlang.org/docs/reference/delegated-properties.html并更改了get运算符。 class Example { var p: String by Delegate() } class Delegate { operator fun getValue(thisRef: Any?, property: KProperty<*>): String { // My implementation return property.getter.call(thisRef) as String } operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { println("$value has been assigned to '${property.name} in $thisRef.'") } […]

引用Kotlin类时的ClassNotFoundException

我试着开始将基于Java的multidexed项目迁移到Kotlin,但是每当应用程序尝试引用Kotlin类时,我都会得到ClassNotFoundException。 奇怪的是,有时相同的代码有时不工作。 有计算机产生工作apk和其他人没有。 有时候重建就足以解决问题。 我试图谷歌它,但我只找到这个线程,但没有解决方案。 这个东西是否适用于其他人或者没有任何multidexed Kotlin项目呢? 我已经尝试了AS 2.3.3和AS 3.0.0-beta6,2.3.3 android插件,kotoin版本1.1.50, 没有proguard 。 buildscript { ext.kotlin_version = '1.1.50' ext.android_plugin_version = '2.3.3' ext.support_lib_version = '25.3.1' ext.play_services_version = '11.0.4' repositories { jcenter() maven { url "https://maven.google.com" } } dependencies { classpath "com.android.tools.build:gradle:$android_plugin_version" classpath 'me.tatarka:gradle-retrolambda:3.2.5' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:3.0.0' classpath 'com.google.firebase:firebase-plugins:1.1.1' } } allprojects { repositories { jcenter() […]

在Kotlin中创建未绑定的服务

我试图在kotlin中创建一个简单的未绑定服务,但是我不能。 当我在Java中重写onBind()方法时,我可以返回null ,但是在kotlin中它说我只允许返回IBinder而不是IBinder? 这意味着它不能为null 。 任何想法如何解决,除了重写MyService类到Java? [已解决]谢谢,伙计们! 我可以真的把IBinder IBinder? 。 有用!!

Kotlin prepend元素

我正在寻找Kotlin替代: (cons 1 '(2 3))在lisp或 1 : [2, 3]在haskell或 1 :: List(2, 3)在斯卡拉, (所有的结果都像[1,2,3]) 所以我可以在List<T> (或者其他你可以提供的列表)中添加一个元素。 如果可以提供O(1) head和tail Kotlin替代品(我刚刚发现first() ),也是没有问题的。

与Kotlin的Mockito:AbstractMethodError

我正在努力使谷歌的测试实验室代码示例与kotlin一起工作。 但转换后,我想嘲笑kotlin接口我得到一个AbstractMethodError ,我无法解决。 的build.gradle: testCompile "org.hamcrest:hamcrest-all:1.3" testCompile "junit:junit:4.12" testCompile "org.mockito:mockito-core:2.8.47" testCompile "org.powermock:powermock-module-junit4:1.6.2" testCompile "org.powermock:powermock-api-mockito:1.6.2" NotesPresenterTest.kt package com.example.android.testing.notes.notes import org.mockito.Matchers.any /** * Unit tests for the implementation of [NotesPresenter] */ class NotesPresenterTest { @Mock private val mNotesView: NotesContract.View? = null @Before fun setupNotesPresenter() { MockitoAnnotations.initMocks(this) // Get a reference to the class under test mNotesPresenter = […]

maven-source-plugin不适用于kotlin

我正在尝试使用maven-source-plugin来为我的kotlin项目创建一个source.jar,但是似乎maven-source-plugin不适用于kotlin项目。 当我运行“mvn源:瓶”,输出消息总是说: [INFO] No sources in project. Archive not created. 这里是项目的pom文件中的maven-source-plugin配置: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <executions> <execution> <id>attach-sources</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <attach>true</attach> <includes> <!– i am trying to specify the include dir manually, but not work –> <include>${project.basedir}/src/main/kotlin/*</include> </includes> <forceCreation>true</forceCreation> </configuration> </execution> </executions> </plugin> </plugins> </build> 我的问题是:如何使用maven-source-plugin附上kotlin源文件? 谢谢~~

在kotlin广播Reciver

如何在Kotlin中使用注册并在Android中创建Broadcast Receiver。 任何建议….在Java中,您可以通过声明它为广播接收器来创建它。但是在Kotlin中没有广播接收器功能…以及如果有,那么我无法找到它或如何使用它。