JNI Signatures

Document created by jakbutler on Nov 6, 2012Last modified by jakbutler on Jun 30, 2014
Version 5Show Document
  • View in full screen mode

When instrumenting a method in a PBD file, it can often be useful to use the JNI signature of the Java method rather than just the method name. This allows you to select a specific overloaded method. Particularly in situations where the overloaded methods are chained, as below, specifying the method JNI signature can allow you to avoid unnecesasry redundency when instrumenting:

public void start(String param1) {
     start(param1, null);
}

public void start(String param1, String param2) {
     start(param1, null, null);
}

public void start(String param1, String param2, String param3) {
     //Do stuff
}


Overview 

There are three parts to the signature. The first is the method name itself, which is the Java method name in UTF-8 form. You can specify the method name for a constructor of a class by enclosing the word "init" within angle brackets (this appears as "<init>").The second part is enclosed within the parentheses and represents the method's arguments. The third portion follows the closing parenthesis and represents the return type.

The general form of a method signature argument is:

method-name(argument-types)return-type


The mapping between the Java type and C type is:

Type SignatureJava Type
Zboolean
Bbyte
Cchar
Ddouble
Ffloat
Ishort
Jlong
Lobject
Sshort
Vvoid
Lfully-qualified-class;fully-qualified-class
[typearray of type[]

Note: To specify an object, the "L" is followed by the object's fully qualified package & class name (e.g. java/lang/String) and ends with a semi-colon, ';'. Additionally, array types are indicated by a leading square bracket ([) followed by the type (in using JNI type signatures) of the array elements.


Examples 

MethodSignature
void f1()f1()V
int f2(int n, long l)f2(IJ)I
boolean f3(int[] arr)f3([I)B
double f4(String s, int n)f4(Ljava/lang/String;I)D
void f5(int n, String[] arr, char c)f5(I[Ljava/lang/String;C)V
long f6(int n, String s, int[] arr)f6(ILjava/lang/String;[I)J
String f7(Boolean b, boolean b2)f7(Ljava/lang/Boolean;Z)Ljava/lang/String;

 

To eliminate the mistakes in deriving method signatures by hand, you can use the javap tool (included with the JDK) to show the signature to be used in JNI and can be used against any class:

C:\>javap -s java.awt.Label
Compiled from Label.java
public class java.awt.Label extends java.awt.Component {
     public java.awt.Label(java.lang.String);
          /* (Ljava/lang/String;)V */
     public java.awt.Label(java.lang.String,int);
          /* (Ljava/lang/String;I)V */
     public void setText(java.lang.String);
          /* (Ljava/lang/String;)V */
C:\>javap -s -p Prompt
Compiled from Prompt.java
class Prompt extends java.lang.Object 
    /* ACC_SUPER bit set */
{
     private native getLine (Ljava/lang/String;)Ljava/lang/String;
     public static main ([Ljava/lang/String;)V
     <init> ()V
     static <clinit> ()V
}

Note: The "-s" flag informs javap to output signatures rather than normal Java types. The "-p" flag causes private members to be included.

Attachments

    Outcomes