Tag: kotlin

修改一个Kotlin类

我想为Intellij IDEA编写一个插件来修改Java和Kotlin代码。 我使用的方法 PsiClass.getMethods() 以获得Java和Kotlin类的所有方法。 到目前为止好,那么我使用类似的方法 PsiClass.add(), PsiClass.addAfter(), PsiClass.addBefore() 一旦它们被调用到Java文件上,它们都可以正常工作,但是却开始抛出一个exception IncorrectOperationException 一旦我把他们叫做Kotlin课堂。 我很感激任何提示,我可以修改Kotlin和Java类(最好使用相同的方法)。

使Intellij Idea插件与Kotlin文件一起工作

我已经创建了这个非常简单的Intellij Idea插件 ,它可以折叠一些引用expression式。 它适用于Java文件,但对Kotlin不起作用。 这里是来源: https : //github.com/nodes-android/nstack-translation-folding 。 我将在这里包括重要的部分: plugin.xml中 com.intellij.modules.all com.nodes.folding.TranslationFoldingBuilder TranslationFoldingBuilder.kt class TranslationFoldingBuilder : FoldingBuilderEx() { override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array { if (root !is PsiJavaFile) { return FoldingDescriptor.EMPTY } val descriptors = ArrayList() // Get all the reference expressions in this Java file val referenceExpressions = PsiTreeUtil.findChildrenOfType(root, […]

spring的数据mongodb和kotlin

使用弹簧数据mongodb使用kotlin时,我遇到了一个问题。 当我尝试从mongodb读取对象时,我得到一个错误,抱怨说我的数据类没有默认的无参数构造函数。 我可以通过给我的数据类中的每个字段的值来解决这个问题,所以编译器会生成一个默认的无参数构造函数。 当然,我不是真的想这样做。 我知道有一个jacksonkotlin模块,它包含在我的maven文件中。 它适用于反序列化对象,我得到了HTTP,所以我知道spring拿起它。 但似乎spring的数据mongodb不使用jackson对象映射器? 有没有办法我可以在弹簧数据mongodb中使用jackson对象映射器或修复没有非参数构造函数的问题?

什么时候应该使用let {},什么时候只是简单的!= null

你会怎么写更多的kotlinic方式下面的代码? var returnValue = … val s3data = presignedUrl.body() if (s3data != null) { val uploadImage = api.uploadImage(s3data.bucketUrl, s3data.awsAccessKeyId, s3data.policy, s3data.key, s3data.signature, body).execute() if (!uploadImage.isSuccessful) { crashReporterService.sendIssue(“Failed uploading file”, “Failed uploading file ${uploadImage.raw()}”) returnValue = Result.FAILURE } else { returnValue = Result.SUCCESS } } else { crashReporterService.sendIssue(“Failed uploading image”, “Error – ${presignedUrl.raw()}”) returnValue = Result.FAILURE […]

未解决的参考:使用@ExtendWith注释的SpringExtension

我正在使用SpringBoot中的Junit 5来利用它提供的一些新function。 基本的设置工作,我可以使用新的注释,如@BeforeAll和@AfterAll但是,我相信,因为我不能解决@ExtendWith(SpringExtension::class)我不能初始化我的测试类,以使用注入资源。 我已经尝试将所需的依赖项添加到我的build.gradle文件,但我假设我缺少一个键依赖项: buildscript { ext.dokka_version = ‘0.9.15’ ext.dockerVersion = ‘1.2’ ext.junit4Version = ‘4.12’ ext.junitVintageVersion = ‘4.12.2’ ext.junitPlatformVersion = ‘1.0.2’ ext.junitJupiterVersion = ‘5.0.2’ ext.kotlin_version = ‘1.1.60’ ext.log4jVersion = ‘2.9.0’ ext.springVersion = ‘1.5.8.RELEASE’ repositories { mavenCentral() jcenter() } dependencies { // Needed for the ‘kotlin’ plugin classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}”) // Needed for the ‘org.springframework.boot’ plugin classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springVersion}”) // […]

我不能使用Kotlin的Serenity-bdd

我尝试使用kotlin为我的测试与宁静床框架,但是这不起作用例如 public class EndUserSteps { var dictionaryPage: DictionaryPage = null!! @Step fun enters(keyword: String) { dictionaryPage.enter_keywords(keyword) } @Step fun starts_search() { dictionaryPage.lookup_terms() } @Step fun should_see_definition(definition: String) { assertThat(dictionaryPage.definitions, hasItem(containsString(definition))) } @Step fun is_the_home_page() { dictionaryPage.open() } @Step fun looks_for(term: String) { enters(term) starts_search() } } 其他代码已经用Java编写了! 输出:(net.serenitybdd.core.exceptions.StepInitialisationException:无法为EndUserSteps创建步骤库:无法inheritance最终的类类ru.tinkoff.atesting.steps.serenity.EndUserSteps) 你可以帮我吗? 有什么想法?

如何用kotlin @Parcelize使用null

这里是非常简单的代码: @Parcelize data class Inner(val a: Int): Parcelable @Parcelize data class Test(val a: Int, val inner: Inner?): Parcelable @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun testParcel() { val test = Test(0, null) val parcel = Parcel.obtain() parcel.writeParcelable(test, test.describeContents()) } } 我有可空的属性Test.inner 。 如果它不为空,代码工作正常,但是当它为空,那么我有例外: java.lang.NullPointerException: Attempt to invoke interface method ‘void android.os.Parcelable.writeToParcel(android.os.Parcel, int)’ on a null object […]

在Kotlin试用资源

当我试图在Kotlin中编写一个与try-with-resources代码相同的代码时,它并不适用于我。 我尝试了以下的不同变化: try (writer = OutputStreamWriter(r.getOutputStream())) { // … } 但是都没有用。 有谁知道应该用什么来代替? 显然Kotlin语法没有定义这样的结构,但可能是我错过了一些东西。 它为try块定义语法如下: try : “try” block catchBlock* finallyBlock?;

是否有可能使用Kotlin构建完整的Node.js Web应用程序?

如果我理解正确,随着Kotlin 1.1的发布 ,我们可以将JavaScript设置为 Kotlin项目的JavaScript 编译目标 。 是否有可能(或可行)使用Kotlin代码编写完整的Node.js应用程序,如快速网络服务器? 如这个问题所示,我们可以将Node模块导入到Kotlin类中: external fun require(module: String): dynamic val express = require(‘express’) …这似乎可以创建一个应用程序使用: val app = express() 这是Kotlin建立快速应用程序的方式吗? 或者,我应该按照文档中的说明申报一个class级: @JsModule(“express”) external class Express { … } 为Node.js应用程序开发建立一个Kotlin项目的规范方法是什么? Kotlin的JavaScript互操作性足够强大,能够继续走下去吗,还是会比它的价值更麻烦呢?

exception:java.lang.ClassNotFoundException:org.reactivestreams.Publisher

在IntelliJ IDEA 2017.1.2(Build#IC-171.4249.39)中使用RxJava和Kotlin开发的基于JavaFX Gradle的应用程序中,出现exception: 线程“JavaFX Application Thread”中的exceptionjava.lang.NoClassDefFoundError:org / reactivestreams / Publisher 每次这样的代码 return Completable.complete() 被执行。 在类似问题的建议之后为什么我得到NoClassDefFoundError:org / reactivestreams / Publisher ,我试图添加包括反应流到我的build.gradle脚本的dependencies块 dependencies { compile ‘org.jetbrains.kotlin:kotlin-stdlib:1.1.2’ compile ‘org.reactivestreams:reactive-streams:1.0.0’ compile ‘io.reactivex.rxjava2:rxkotlin:2.0.0’ } 但问题依然存在。 依赖关系树看起来像这样: compile – Dependencies for source set ‘main’ (deprecated, use ‘implementation ‘ instead). +— org.jetbrains.kotlin:kotlin-stdlib:1.1.2 | \— org.jetbrains:annotations:13.0 +— org.reactivestreams:reactive-streams:1.0.0 \— io.reactivex.rxjava2:rxkotlin:2.0.0 +— io.reactivex.rxjava2:rxjava:2.0.7 […]