Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)

To access instance fields and call methods that are defined in the Java programming language, you need to learn the rules for "mangling" the names of data types and method signatures. (A method signature describes the parameters and return type of the method.) Here is the encoding scheme:

B

byte

C

char

D

double

F

float

I

int

J

long

Lclassname;

a class type

S

short

V

void

Z

boolean

Note that the semicolon at the end of the L expression is the terminator of the type expression, not a separator between parameters. For example, the constructor

Employee(java.lang.String, double, java.util.Date)

has a signature

"(Ljava/lang/String;DLjava/util/Date;)V"

Note that there is no separator between the D and Ljava/util/Date;.

Also note that in this encoding scheme, you must use / instead of . to separate the package and class names.

To describe an array type, use a [. For example, an array of strings is

[Ljava/lang/String;

A float[][] is mangled into

[[F

For the complete signature of a method, you list the parameter types inside a pair of parentheses and then list the return type. For example, a method receiving two integers and returning an integer is encoded as

(II)I

The print method that we used in the preceding example has a mangled signature of

(Ljava/lang/String;)V

That is, the method receives a string and returns void.

TIP

You can use the javap command with option -s to generate the method signatures from class files. For example, run

javap -s -private Employee

You get the following output, displaying the signatures of all fields and methods.

Compiled from "Employee.java" public class Employee extends java.lang.Object{ private java.lang.String name; Signature: Ljava/lang/String; private double salary; Signature: D public Employee(java.lang.String, double); Signature: (Ljava/lang/String;D)V public native void raiseSalary(double); Signature: (D)V public void print(); Signature: ()V static {}; Signature: ()V }

NOTE

There is no rationale whatsoever for forcing programmers to use this mangling scheme for describing signatures. The designers of the native calling mechanism could have just as easily written a function that reads signatures in the Java programming language style, such as void(int,java.lang.String), and encodes them into whatever internal representation they prefer. Then again, using the mangled signatures lets you partake in the mystique of programming close to the virtual machine.

    Категории