Home > OS >  IDE autocomplete does not know argument names of native methods
IDE autocomplete does not know argument names of native methods

Time:06-11

I am working on two library projects. One being a native library in c and the other a Java library that uses JNI to wrap the functions from the native library. So in total I got 3 libraries:

  • The native library in c (project 1)
  • The wrapper library in c that uses headers generated by javac (project 2)
  • The wrapper library in Java which I use to generate headers with (project 2)

My question relates only to project 2. To compile project 2 I use a script that runs both cmake, javac and jar in one go to generate a single jar file with all my .class and .so files. With javac I use the '-g' parameter to generate debugging info. This works fine for non-native (as in the keyword 'native', not c native) methods but it does not for native. The compiled .class files do not contain the right signature either. This is rather frustrating because when I use my libraries in another java project (project 3) I do not know what to fill in based on the helper popup IntelliJ shows. My methods look like this in Java and like this in c . Any idea if I can fix that?

Also, any idea what the term is called for changing the signature of a Java method like this? I tried to lookup my problem for a couple of days now but could not find it so I was wondering if I was using the wrong search phrases in Google.

Thanks in advance!

CodePudding user response:

It appears the names of arguments are stored as "local" variables, which is not filled in for a native method. You can get around this with a small "trampoline" method:

class Window {
    public long create(int width, int height, String title) {
        return native_create(width, height, title);
    }
    private native long native_create(int width, int height, String title);
}

which results in the following javap output:

  public long create(int, int, java.lang.String);
    descriptor: (IILjava/lang/String;)J
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=4, locals=4, args_size=4
         0: aload_0
         1: iload_1
         2: iload_2
         3: aload_3
         4: invokevirtual #7                  // Method native_create:(IILjava/lang/String;)J
         7: lreturn
      LineNumberTable:
        line 4: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       8     0  this   LWindow;
            0       8     1 width   I
            0       8     2 height   I
            0       8     3 title   Ljava/lang/String;
  • Related