पर तर्क कास्टिंग रोकने के लिए कैसे करें मैं हाल ही में कोटलिन के साथ घूम रहा हूं, और यह बहुत बढ़िया रहा है!कोटलिन को गलत वर्ग (इंटरफ़ेस)
मैं ट्विटर एपीआई के साथ कुछ सामानों को आजमाने के लिए ट्विटर 4j लाइब्रेरी के साथ काम कर रहा था। मैं 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()
}
}
में इस कोड को लिखा था लेकिन हर बार मैं इसे भाग गया, मैं 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)
मिला आगे की जांच पड़ताल, उपकरण> Kotlin-> दिखाएं Kotlin Bytecode पर। मैं जावा पर डूब गया, केवल यह पता लगाने के लिए कि यह उत्पन्न किया जा रहा था।
@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();
}
Kotlin StreamListener
को श्रोता कास्ट करने के लिए कोशिश कर रहा था जो लाइब्रेरी में निजी है (StatusListener यह फैली हुई है) इसलिए IllegalAccessError
कोई भी विचार कैसे कृपया हल करने?
पुनश्च: यहाँ कोड का एक काम कर जावा संस्करण है - https://github.com/yusuke/twitter4j/blob/master/twitter4j-examples/src/main/java/twitter4j/examples/stream/PrintSampleStream.java
पी पी एस: मैं Kotlin 1.1.4, उपयोग कर रहा हूँ IntelliJ विचार पर 2017.2.2
आपकी प्रतिक्रिया @ किस्का के लिए धन्यवाद। मैं इसे आज़माउंगा। दरअसल यह जावा में पूरी तरह से काम करता है इसलिए फिक्सर – Enoobong
की कोई ज़रूरत नहीं है, ठीक आकर्षण की तरह काम किया। बहुत धन्यवाद – Enoobong