最近のKotlinで回ってきた、それは素晴らしいです!Kotlinが間違ったクラス(インタフェース)に引数をキャストするのを止める方法
私はTwitter4jライブラリを使ってTwitter APIでいくつかのことを試していました。私はKotlin
object Demo {
private val twitterStream = TwitterStreamFactory().instance
@JvmStatic
fun main(args: Array<String>) {
val listener = object : StatusListener {
override fun onStallWarning(warning: StallWarning?) {
println("Got stall warning:" + warning)
}
override fun onScrubGeo(userId: Long, upToStatusId: Long) {
println("Got scrub_geo event userId:$userId upToStatusId:$upToStatusId")
}
override fun onStatus(status: Status) {
println("@" + status.user.screenName + " - " + status.text)
}
override fun onDeletionNotice(statusDeletionNotice: StatusDeletionNotice) {
println("Got a status deletion notice id:" + statusDeletionNotice.statusId)
}
override fun onTrackLimitationNotice(numberOfLimitedStatuses: Int) {
println("Got track limitation notice:" + numberOfLimitedStatuses)
}
override fun onException(ex: Exception) {
ex.printStackTrace()
}
}
twitterStream.addListener(listener)
twitterStream.sample()
}
}
にこのコードを書いたが、私はそれを実行したたびに、私はさらに調査、ツール - > Kotlin->ショーKotlinバイトコードの際に
exception in thread "main" java.lang.IllegalAccessError: tried to access class twitter4j.StreamListener from class co.enoobong.eno.twitter.bot.Demo at co.enoobong.eno.twitter.bot.Demo.main(Demo.kt:63)
を得ました。私はJavaに逆コンパイルして、これが生成されていることを発見するだけです。 (StatusListenerはそれを拡張する)ので、IllegalAccessError
任意のアイデアはどのようにしてください解決するためのライブラリでプライベートでKotlinはStreamListener
にリスナーをキャストしようとしていた
@JvmStatic
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
<undefinedtype> listener = new StatusListener() {
public void onStallWarning(@Nullable StallWarning warning) {
String var2 = "Got stall warning:" + warning;
System.out.println(var2);
}
public void onScrubGeo(long userId, long upToStatusId) {
String var5 = "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId;
System.out.println(var5);
}
public void onStatus(@NotNull Status status) {
Intrinsics.checkParameterIsNotNull(status, "status");
String var2 = "@" + status.getUser().getScreenName() + " - " + status.getText();
System.out.println(var2);
}
public void onDeletionNotice(@NotNull StatusDeletionNotice statusDeletionNotice) {
Intrinsics.checkParameterIsNotNull(statusDeletionNotice, "statusDeletionNotice");
String var2 = "Got a status deletion notice id:" + statusDeletionNotice.getStatusId();
System.out.println(var2);
}
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
String var2 = "Got track limitation notice:" + numberOfLimitedStatuses;
System.out.println(var2);
}
public void onException(@NotNull Exception ex) {
Intrinsics.checkParameterIsNotNull(ex, "ex");
ex.printStackTrace();
}
};
twitterStream.addListener((StreamListener)listener);
twitterStream.sample();
}?
PS:ここでは、コードの作業Java版だ - https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintSampleStream.java
PPS:私はのIntelliJ IDEAに、Kotlin 1.1.4を使用していKotlinは正しい方法を確認するためにこれらのキャストを追加2017年2月2日
ご回答ありがとうございます@Kiskae。私はこれを試してみよう。実際にはJavaで完璧に動作するので、修正プログラムの必要はありません – Enoobong
あまりにも魅力的なように働いた。多くのありがとう – Enoobong