Tag: Java

在java开关表达式中使用kotlin常量

我最近一直在研究Kotlin lang,并与java进行了交互。 我有以下的Java代码: public void select(int code) { switch code { case Service.CONSTANT_ONE: break; case Service.CONSTANT_TWO: break; default: break; } } Service.kt写法如下: class Service { companion object { val CONSTANT_ONE = 1 val CONSTANT_TWO = 2 } } Java编译器说,CONSTANT_ONE和CONSTANT_TWO必须是常量,但我不知道,我怎样才能使它们比现在更加稳定。 所以我的问题是:如何使用java swicth语句中的kotlin常量? 我正在使用jdk8和kotlin M14。

尝试monot kotlin

Java 8没有提供Try monad。 这对功能样式中的异常处理很有用。 kotlin有没有准备好实现? 还是我应该执行一个?

用kotlin扩展属性的java.lang.VerifyError

我试图像这样用kotlin扩展Java类的数字类型属性: // Java class public class Person { private Number old; public Number getOld() { return old; } public void setOld(Number a) { this.old = a; } } 和我的Kotlin代码: // Kotlin code var Person.age: Number inline get() = old inline set(value) { old = value } fun main(args: Array<String>) { val person = Person() person.age […]

Gradle项目:java.lang.NoClassDefFoundError:kotlin / jvm / internal / Intrinsics

我正在开发一个Java项目,在这个项目中,我第一次尝试了Kotlin。 我开始使用Intellij Idea中提供的JavaToKoltin转换器将一些类转换为Kotlin。 其中我的自定义例外现在转换为Kotlin。 但是,这个异常处理不再正确工作了。 如果我在java代码中抛出一个自定义异常(例如MyCustomKotlinException.kt ),那么这个异常不会被捕获(见下面的代码)。 // Example.java package foo import java.util.*; import java.lang.*; import java.io.*; import foo.MyCustomKotlinException; class Example { public static void main (String[] args) { try { // Do some stuff // if Error MyCustomKotlinException e = new MyCustomKotlinException("Error Message"); throw e; } catch (MyCustomKotlinException e) { // <– THIS PART […]

与Kotlin一起使用ExpectedException

我已经宣布了与Kotlin的一个预期的例外: @Rule public var exception = ExpectedException.none() 现在我的集成测试用例: @Test @Transactional fun authorize_withInvalidToken() { val response = controller.authorize(networkType = "facebook", oauthToken = "", oauthTokenSecret = null) exception.expect(UnauthorizedException::class.java) } 我得到的错误: org.junit.internal.runners.rules.ValidationError: The @Rule 'exception' must be public. 有没有办法来解决这个问题? 现在我只使用手动try / catch / assert

在Kotlin中使用BufferedReader的最佳方式

所以我刚刚开始使用Kotlin for Android(希望它可以帮助我使用Swift),并将Android Java代码转换为Kotlin。 在其中一个转换中,我偶然发现了一个BufferedReader,我通常会用Java编写(或者说C#),如下所示: String result = ""; String line = ""; BufferedReader reader = new BufferedReader(someStream); while ( (line = reader.readLine()) != null ) { result += line; } 但是在Kotlin中,似乎Kotlin不允许我在条件的情况下给变量赋值。 目前,我已经编写了如下代码: val reader = BufferedReader(someStream) var line : String? = "" while (line != null) { line = reader.readLine() result += line } […]

Java与Kotlin泛型

我有下面的Java类,我试图转换为使用泛型的Kotlin。 抽象类MvpViewHolder,M,V:View?>(itemView:View):RecyclerView.ViewHolder(itemView){ public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder { protected P presenter; public MvpViewHolder(View itemView) { super(itemView); } public void bindPresenter(P presenter) { this.presenter = presenter; presenter.bindView(this); } public void unbindPresenter() { presenter = null; } } 这是我的Kotlin尝试 abstract class MvpViewHolder<P : BasePresenter>(itemView: View) : RecyclerView.ViewHolder(itemView) { protected var presenter: P? = null […]

Kotlin + Dagger 2:Dagger *文件不会生成

我第一次开始使用Kotlin和Dagger 2。 我认为所有东西都和Java一样,但显然不是。 匕首不会为我生成Dagger *文件。 这是我的代码: 组件: @PerActivity @Subcomponent(modules = arrayOf(ApplicationModule::class)) interface ActivityComponent { fun inject(app: OneAccountApplication) } @Singleton @Component(modules = arrayOf(ApplicationModule::class)) interface ApplicationComponent { fun inject(syncService: SyncService) @ApplicationContext fun context(): Context fun application(): Application fun ribotsService(): OneAccountService fun preferencesHelper(): PreferencesHelper fun databaseHelper(): DatabaseHelper fun dataManager(): DataManager } @ConfigPersistent @Component(dependencies = arrayOf(ApplicationComponent::class)) interface ConfigPersistentComponent { […]

通用铸造在Kotlin

我有以下的类和接口: public interface ActivityComponent<T extends Activity> { void inject(T activity); } public interface MyActivityComponent extends ActivityComponent<MyActivity> { } public abstract class DaggerActivity extends Activity { abstract ActivityComponent getComponent(Context context); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ActivityComponent component = getComponent(this); } } public class MyActivity extends DaggerActivity { @Override ActivityComponent getComponent(Context context) { MyActivityComponent component […]

Kotlin:appendText和关闭资源

我是Kotlin的新手,但是我有一个强大的Java背景(Java是我的日常工作)。 我喜欢Kotlin的一些快捷功能。 其中一个重要的是File.appendText()。 IMO非常方便。 我的问题是关闭资源。 如果我要使用一个作家,我会做这样的事情: out8.writer().use { … } 但是我没有直接在appendText方法上看到任何表示关闭资源的东西。 Kotlin是在幕后为我处理的,还是这个我不得不以另一种方式担心呢? 谢谢。