thunderbiscuit
03/06/2023, 1:43 PMCLOVIS
03/06/2023, 2:19 PMUByte
etc are inline classes, they should not be visible from Java.
For example,
fun foo(a: UInt) = a + 1u
has the Java signature
int foo(int a)
If they cannot call them directly like this, it's likely you have some other problem, can you share an example?thunderbiscuit
03/06/2023, 2:23 PMdata class BlockTime (
var height: UInt,
var timestamp: ULong
)
An attempt to create an object of this type will give you this error:
BlockTime blockTime = new BlockTime(
10,
10000000
);
// 'BlockTime(int, long)' has private access in org.example.BlockTime
thunderbiscuit
03/06/2023, 2:27 PMkenrube
03/06/2023, 2:58 PMpublic
modifier?thunderbiscuit
03/06/2023, 2:58 PMkenrube
03/06/2023, 2:59 PMthunderbiscuit
03/06/2023, 2:59 PMthunderbiscuit
03/06/2023, 3:36 PMthunderbiscuit
03/06/2023, 3:36 PMkenrube
03/06/2023, 3:47 PMthunderbiscuit
03/06/2023, 3:48 PMCLOVIS
03/06/2023, 3:51 PMthunderbiscuit
03/06/2023, 3:55 PMpublic final data class BlockTime public constructor(height: kotlin.UInt, timestamp: kotlin.ULong) {
public final var height: kotlin.UInt /* compiled code */
public final var timestamp: kotlin.ULong /* compiled code */
public final operator fun component1(): kotlin.UInt { /* compiled code */ }
public final operator fun component2(): kotlin.ULong { /* compiled code */ }
}
And the source file is this:
data class BlockTime (
var `height`: UInt,
var `timestamp`: ULong
)
CLOVIS
03/06/2023, 3:58 PMkotlin("jvm")
plugin to your repo, and add a copy of your BlockTime
class in src/main/kotlin/…
and try to use it? Your reproduction repo doesn't have any weird plugins, so if it works for this you'll be able to search for the differences with your libraries.thunderbiscuit
03/06/2023, 4:09 PMdata class BlockTime2 (
var height: UInt,
var timestamp: ULong
)
Results in the same error:
/Users/user/repos/bdk-java-interop/src/main/java/org/sandbox/Main.java:33: error: BlockTime2(int,long) has private access in BlockTime2
BlockTime2 blockTime = new BlockTime2(
^
When attempting to create the object from the Java sidethunderbiscuit
03/06/2023, 4:12 PMKlitos Kyriacou
03/06/2023, 5:04 PMprivate BlockTime2(int height, long timestamp) {
this.height = height;
this.timestamp = timestamp;
}
// $FF: synthetic method
public BlockTime2(int height, long timestamp, DefaultConstructorMarker $constructor_marker) {
this(height, timestamp);
}
So yes, indeed, it doesn't work from Java. You may be able to hack using some dummy DefaultConstructorMarker parameter.kenrube
03/06/2023, 5:19 PMCLOVIS
03/06/2023, 5:21 PMthunderbiscuit
03/06/2023, 6:18 PMthunderbiscuit
03/06/2023, 6:28 PMGeorge
03/09/2023, 10:57 AMOtherBlockTime.kt
:
@file:JvmName("BlockTimeKt")
package org.sandbox
data class BlockTime2(
var height: UInt,
var timestamp: ULong
)
@Suppress("FunctionName")
@JvmName("from") // disables mangling
fun BlockTime2(height: Int, timestamp: Long): BlockTime2 {
return BlockTime2(height.toUInt(), timestamp.toULong())
}
data class BlockTime3(
var height: Int,
var timestamp: Long
)
`Main.java`:
public class Main {
public static void main(String[] args) throws BdkException {
System.out.println("Hello world!");
blockTime();
}
public static void blockTime() {
// BlockTime blockTime = new org.bitcoindevkit.BlockTime(
// 10,
// 10000000
// );
BlockTime2 blockTime = BlockTimeKt.from(
10,
10000000
); // works
BlockTime3 blockTime3 = new BlockTime3(
10,
10000000
);
}
}
George
03/09/2023, 10:58 AMkenrube
03/09/2023, 11:07 AMdata class BlockTime(
var height: UInt,
var timestamp: ULong
) {
constructor(
height: Int,
timestamp: Long
) : this(
height.toUInt(),
timestamp.toULong()
)
}
Problem is - this code is autogenerated from Rust and it requires changes in generator from library’s authorsGeorge
03/09/2023, 11:10 AM@JvmName
does not work on constructors.kenrube
03/09/2023, 11:11 AMCLOVIS
03/09/2023, 11:12 AMOr (just thought of this) create new wrapper types only for java code and play around them with factory functionsIMO, if factory functions are needed for Java anyway, it's easier to just declare them with
@JvmName
than to create a whole new classkenrube
03/09/2023, 11:12 AMGeorge
03/10/2023, 5:43 PM@JvmName
does not disables mangling in my example, it is just for a nicer java API.
The "bridge" function is still needed though.thunderbiscuit
03/10/2023, 5:45 PMthunderbiscuit
03/10/2023, 5:47 PMkenrube
03/10/2023, 5:48 PMthunderbiscuit
03/10/2023, 5:48 PMConstructor<ElectrumConfig> electrumConfigConstructor = (Constructor<ElectrumConfig>) ElectrumConfig.class.getDeclaredConstructors()[1];
electrumConfigConstructor.setAccessible(true);
ElectrumConfig electrumConfig = electrumConfigConstructor.newInstance(electrumAddress, null, Byte.parseByte("5"), null, 5, true);
thunderbiscuit
03/10/2023, 5:49 PMkenrube
03/10/2023, 5:51 PM