java-interop

Changing kotlin extension function receiver JVM name

久未见 提交于 2021-01-27 04:30:34
问题 This is a general question. Let's say I have an extension function written in kotlin which converts DP to PX and return a NonNull Int fun Int.toPx() { /** implementation */ } The function in java will look something like this public int toPx(int $receiver) { /** implementation */ } In my opinion the $receiver makes the Java-interop feels generated and uninviting. I know that you can use the @JvmName annotation with some combinations like @file:JvmName to change the name in java. When i'm

In Karate, what is the advantage of wrapping a Java function in a JavaScript function?

家住魔仙堡 提交于 2021-01-05 07:17:26
问题 I can wrap a Java function like this: * def myJavaMethod = """ function() { var Utils = Java.type('Utils'); // use Number type in constructor var obj = new Utils(...); return obj.myJavaMethod(); } """ But why would I? I can use Java functions straight in the test scenarios, like this: Scenario: Test exec and error value * def Utils = Java.type('Utils'); * def ret = Utils.exec('echo "From Java" > /home/richard//karate-test/karate-0.9.6/out.txt'); * match read('out.txt') == "From Java\n"; *

In Karate, what is the advantage of wrapping a Java function in a JavaScript function?

佐手、 提交于 2021-01-05 07:17:05
问题 I can wrap a Java function like this: * def myJavaMethod = """ function() { var Utils = Java.type('Utils'); // use Number type in constructor var obj = new Utils(...); return obj.myJavaMethod(); } """ But why would I? I can use Java functions straight in the test scenarios, like this: Scenario: Test exec and error value * def Utils = Java.type('Utils'); * def ret = Utils.exec('echo "From Java" > /home/richard//karate-test/karate-0.9.6/out.txt'); * match read('out.txt') == "From Java\n"; *

Clojure - convert list into Java array

本秂侑毒 提交于 2019-12-12 11:25:05
问题 Is there any idiomatic way of converting Clojure list into Java array, other than first converting it to vector and using into-array (means, something other than (into-array (vec my-list)) , as I don't want additional overhead)? 回答1: Your question seems to be based on a false premise. into-array does not need to take a vector, it takes a seqable value. The documentation ( http://clojuredocs.org/clojure_core/clojure.core/into-array ) contains examples of using into-array on a non-vector

Add constructor to deftype created class

馋奶兔 提交于 2019-12-06 19:33:21
问题 For the purposes of interoperability with Java, I need a class that has a nullary constructor that performs initialization. Objects of this class need to have something resembling mutable java fields (namely, the object represents the backend of a game, and needs to keep game state). deftype does everything I want to do except provide a nullary constructor (since I'm creating a class with fields). I don't need the fields to be publicly readable, so I can think of 4 solutions: Use gen-class; I

How do I invoke a Java method from perl6

本小妞迷上赌 提交于 2019-12-03 17:37:10
问题 use java::util::zip::CRC32:from<java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8') { $crc.'method/update/(B)V'($_); } say $crc.getValue(); sadly, this does not work Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32' This code is available at the following links. It is the only example I've been able to find Rakudo Perl 6 on the JVM (slides) Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM 回答1: Final answer Combining the code cleanups

How do I invoke a Java method from perl6

放肆的年华 提交于 2019-12-03 07:41:59
use java::util::zip::CRC32:from<java>; my $crc = CRC32.new(); for 'Hello, Java'.encode('utf-8') { $crc.'method/update/(B)V'($_); } say $crc.getValue(); sadly, this does not work Method 'method/update/(B)V' not found for invocant of class 'java.util.zip.CRC32' This code is available at the following links. It is the only example I've been able to find Rakudo Perl 6 on the JVM (slides) Perl 6 Advent Calendar: Day 03 – Rakudo Perl 6 on the JVM Final answer Combining the code cleanups explained in the Your answer cleaned up section below with Pepe Schwarz's improvements mentioned in the

Does Clojure have an equivalent of Java's import package.*?

不想你离开。 提交于 2019-12-01 02:18:26
Or do I have to specifically enumerate every class that I import? I'm just learning Clojure now, and it seems useful to be able to do something like this in the REPL: (import '(java.io *)) Not that this is valid syntax, but it would be nice to have something that does the equivalent. It would save some typing, especially when tinkering around. In actual production code I always enumerate each class that I'm importing, regardless of the language, but it's pretty convenient not to have to do so. Rich Hickey explains why it is not possible . Unless I missed an update, there is no way to wild card

Does Clojure have an equivalent of Java's import package.*?

百般思念 提交于 2019-11-30 21:51:16
问题 Or do I have to specifically enumerate every class that I import? I'm just learning Clojure now, and it seems useful to be able to do something like this in the REPL: (import '(java.io *)) Not that this is valid syntax, but it would be nice to have something that does the equivalent. It would save some typing, especially when tinkering around. In actual production code I always enumerate each class that I'm importing, regardless of the language, but it's pretty convenient not to have to do so

What's the intended use of @JvmSynthetic in Kotlin?

别来无恙 提交于 2019-11-28 13:17:05
I have come across the @JvmSynthetic annotation in kotlin-stdlib, and I'm wondering what it is for, but, unfortunately, it is undocumented. (UPD: it was at that moment) As far as I understand, applying it to a program element will add the synthetic modifier to the corresponding bytecode elements. As a consequence, the element becomes invisible from Java: class MyClass { @JvmSynthetic fun f() { } } Somewhere in Java code: MyClass c = new MyClass(); c.f() // Error: cannot resolve method f() But the same elements are still visible in Kotlin code: val c = MyClass() c.f() // OK Is hiding