Tag: Java

如何在Kotlin Native中导入Java包

我正在试图导入Java包在Kotlin native中使用,如下所示: /* main.kt */ import java.util.* fun main(args: Array<String>) { print("Simple program") } 并用下面的命令编译它 kotlinc main.kt -o main 我收到了错误信息 main.kt:2:8: error: unresolved reference: java import java.util.* 显然我需要显示kotlinc在哪里可以找到java.util,我怎样才能实现这一点,而不使用命令行? 我在使用Kotlin本机0.3的Windows 10 64位。

如何在IntelliJ插件中执行Kotlin代码?

我想要构建一个IntelliJ Idea插件, 检测到某个Kotlin类出现在源代码文件(如SomeGraph )中 如果存在这样的类,则执行代码并将结果显示在侧窗格中。 Kotlin代码作为低端DSL。 例如,假设我有以下一段代码: val vertex1 = GraphElement() val vertex2 = GraphElement() fun main(args : Array<String>) { val myGraph = SomeGraph() // Initialize graph, add data to it myGraph.addEdge(vertex1, vertex2) // Now myGraph.toDot() will return a Graphviz dot diagram in text form } myGraph.toDot()将返回Graphviz点图的代码( digraph G {… )。 我的插件需要这个文本,让Dot渲染图形并显示图形。 技术上可行吗? 如果是的话,我怎样才能从一个IntelliJ插件执行一段Kotlin代码? 笔记: […]

在kotlin中使用Interface时获取平台声明冲突

我将Java中的一些类转换为kotlin,并且在尝试从接口继承时遇到编译错误: 平台声明冲突:以下声明具有相同的JVM签名(getContentID()Ljava / lang / String;): 公开开放的乐趣get-content-id():String? public open fun getContentId():String? 这里是界面: interface Media { val contentId: String val displayRunTime: String val genres: List<String> val programId: String val runTime: String val type: String } 这里是班级: class Airing : Media { override val contentId: String? = null override val displayRunTime: String? = null override val genres: […]

Spring 5和Kotlin 1.1协程:类型rx.Scheduler不存在

我正在使用Kotlin 1.1.4-3和Spring-context 5.0.0.RELEASE。 在启动项目,我得到这个错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coroutineAnnotationBeanPostProcessor' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/ProxyCoroutineConfiguration.class]: Unsatisfied dependency expressed through method 'coroutineAnnotationBeanPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'globalCoroutineContextResolver' defined in class path resource [org/springframework/kotlin/experimental/coroutine/context/CoroutineContextResolverConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.reflect.InvocationTargetException at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458) at […]

用Kotlin编写的Android库提供对Java应用程序的支持

我正在Kotlin编写一个Android库。 但是在用Java编写的Android应用程序中使用时,支持非常差。 例如,Java中仍然需要Kotlin中的默认参数。 另一个是,非null参数在Kotlin应用程序中不接受null(编译时错误),但在Java中传递null时抛出运行时异常。 我知道空检查,但他们使代码看起来很脏。 什么是支持Android Java应用程序的正确方法? 方向,建议,例子,欢迎。 谢谢。

Kotlin中的Java泛型:在Kotlin中使用Java raw类型作为方法参数

我正在Facebook的SDK中看这个类: public final class ShareDialog extends FacebookDialogBase<ShareContent, Sharer.Result> 我想在Kotlin中有一个方法接受它的父类,类似于以下内容: fun shareToFacebook(shareDialog: FacebookDialogBase<ShareContent<*, *>, Sharer.Result>, activity: Activity) 但是当我尝试调用它的形式java: tournamentPresenter.shareToFacebook(new ShareDialog(this), this); 我得到编译时错误: Error:(751, 45) error: incompatible types: ShareDialog cannot be converted to FacebookDialogBase<ShareContent<?,?>,Result> PS我知道最好不要给主持人分享对话,我不打算这样做,我们只是用它作为问题的一个例子

为什么Mockito不能在Kotlin中使用数字类型来模拟一个通用的参数类型?

我们正在将我们的项目转移到Kotlin语言。 我们决定从测试开始,但面临一些奇怪的行为。 这是我们的测试案例: Service.java public final class Service { private final JdbcTemplate jdbcTemplate; public Service(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public long check() { return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM table", Long.class); } } JavaTest.java (工作正常) @RunWith(MockitoJUnitRunner.class) public final class JavaTest { @Mock private JdbcTemplate jdbcTemplate; @InjectMocks private Service testSubject; @Test public void test() { //given […]

kotlin grpc.StreamObserver去rx.PublishSubject

每当我们声明流API时使用GRPC rpc heartBeat(Empty) returns (stream ServiceStatus){} 我们已经为观察者模式StreamObserver简单的接口(这是protobuf会为我们生成的) public interface StreamObserver<V> { void onNext(V var1); void onError(Throwable var1); void onCompleted(); } 现在你想要做的就是将其转换为一个实际的Observable并且只有在它被传递之后才能继续使用。 override fun heartBeat(arg: Empty): Observable<ServiceStatus> { // we create rx java subject val subject = PublishSubject.create<ServiceStatus>() // we create grpc observer and delegate all calls to rx java val observer = object : StreamObserver<ServiceStatus> […]

从Java转换到Kotlin时出错。 错误 – org.gradle.api.tasks.TaskExecutionException:任务执行失败:app:compileDebugKotlin'

我将我的Android项目从Java转换为Kotlin。 我已经完成了另外3个项目,并成功完成了这个过程。 但是这个有点大,结构复杂。 我已经将一些文件转换成Kotlin和项目编译。 但是当我将它们转换为Kotlin时,有些文件会引发编译异常。 我正在面对的编译问题出现在调试和发布模式。 简短如下(以下列出) org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugKotlin'. Caused by: org.gradle.api.GradleException: Compilation error. See log for more details 你怎么看,我怎么解决这个问题? 我认为这是Android Studio中的一个错误。 我努力了。 没有任何结果: 使缓存失效/重新启动 建设 – >清洁工程 更新kotlin插件,使用最新版本的kotlin,google API。 我的应用程序build.gradle是: buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply […]

如何在处理程序中将java转换为kotlin

如何在处理程序中将java转换为kotlin new Handler().postDelayed(new Runnable(){ @Override public void run() { /* Create an Intent that will start the Menu-Activity. */ Intent mainIntent = new Intent(Splash.this,Menu.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } }, 3000);