DX Application Performance Management

 View Only

JNI Signatures 

Nov 05, 2012 07:38 PM

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.

Statistics
0 Favorited
1 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Comments

Jul 01, 2014 01:19 PM

The tip to use "javap" should be very useful to non-Java developers trying to create PBDs.

Related Entries and Links

No Related Resource entered.