Name/signature for Java constructor with name parameter (from JNI)

I’m trying to call a Java constructor with named parameter from a JNI DLL. How does its name get formatted? From the DLL I normally call JNI’s GetMethodID function, passing it the method name and signature. This works well but I’m a little unclear as to what the name will be for a constructor with a named parameter.

Normal Java constructor with int parameter:
name = “[init]” (using lesser/greater than characters, not square brackets)
signature = “(I)V”

I was trying to find out the answer by myself but couldn’t figure out where to retrieve this info from.

IIRC, Swift named Constructors map to static factory methods prefixed with $New, since the JVM. @ck will know the exact specs.

Actually, this is probably my fault as I’m relying Swift’s enum behaviour:

enum SomeEnum: Int {
    case value1 = 1
    case value2 = 2
    // ...
}

let test = SomeEnum(rawValue: 2)

I assume this doesn’t translate to Java at all.

TBH I’m not sure, since Enums aren’t classes and don’t really get .ctors; I’d expect that the compiler would just generate the underlying JVM equivalent for creating an enum value — but this is a bit beyond my area, I’m afraid, we’ll need to wait for Carlo to answer this.

It kinda of depends on the enum… A simple enum derived from Int will be stored as a plain integer. A more complex enum will be it’s own object.

So if I want it to have a custom constructor, what should I do? A static instantiator would do, too.

Really the easiest is to use something like https://github.com/Konloch/bytecode-viewer to see what gets generated. I think you get a plain integer, so just passing a Jint with value 0 will do.

1 Like