Java method signature ljava lang string

udaniweeraratne

The term method signature is not new to any of the developers. The moment you hear the term ‘method signature’ you will be thinking about something like follows.

When using a general programming language as Java, C++, Python we start writing a method using a method signature, which includes a method name, return type and a parameter list wrapped in round brackets ‘()’.

But when you are working with Javassist, the byte code instrumentation framework, you may want to tell the JVM to access a specific method (CtMethod) of a given Class (CtClass) during the run time. [If you are not familiar with bytecode instrumentation and Javassist please visit here for more information.]

Suppose we have a simple test class, ‘TestSignature’ with three overloaded methods (named as testMethod).

TestSignature.java

package test.signature; public class TestSignature < public void testMethod()< //statements >public String testMethod(String x) < //statements return x; >public void testMethod(String x, int y) < //statements >public int testMethod(String[] x) < //statements return x.length; >>

A call to getDeclaredMethods() in Javassist API with the method name as testMethod’ will return all the methods with the given name as a CtMethod array. In order to pick the right method you have to go an extra mile by iterating through the array. But, if you know the JNI (Java Native Interface) type signature of the method, you can get the exact method with single call to getMethod() in Javassist API.

The JNI type method signature can be derived by following the naming convention below. The set of special notations used are,

Type Signature Java Type
Z Boolean
B byte
C char
S short
I int
J long
F float
D double
V void
L object
[ array

A method signature derived using these notations is written in the reverse order compared to the normal format. It starts with the list of parameters enclosed in round brackets and ends with the return type [ (parameter_types) return_type ]. It does not contain the method name. Some special points to note are,

  • If the parameter/return value is an object, use the fully qualified class name start with L and end with a semicolon (;). E.g: Ljava/lang/String;
  • If the parameter/return value is an array, start with a square bracket ‘[‘ which is followed by the above notations or the fully qualified name. E.g: [I or [java/lang/String;

If you follow these instructions you can derive the signature of any given method easily as shown below.

public void testMethod(String name) (Ljava/lang/String;)V // derived signature

But still, there is a chance of making a mistake while trying to convert it to JNI format. Therefore, Java has provided a tool to automatically generate signatures of all the methods in a given class using a single command. The javap tool will make your life lot easier with javap -s command followed by the fully qualified class name.

Читайте также:  Java constructor invoke super

Steps to be followed to generate the method signatures.

signature2

  • Write the Java class and save as a java file (TestSignature.java).
  • Compile your java file using the command line (javac test.signature.TestSignature) or using the IDE.
  • Go to the folder which contains your .class files and run the command javap -s test.signature.TestSignature.
  • If you are going to the exact location of the .class file, use the class name instead of using the fully qualified name)
  • You may use the above command if you have only the .class files too. But you should have all the depending classes used by the respective class, in order to generate the signature.

Well, that’s it for the topic. Hope you will find this article useful if you are stuck with a java agent and instrumenting overloaded methods. Till the next article, have a nice time … 🙂

Источник

Java method signature ljava lang string

We use cookies to collect and analyze information on site performance and usage, to provide social media features and to enhance and customize content and advertisements.

long myMethod (int n, String s, int[] arr);

There are two parts to the signature. The first part is enclosed within the parentheses and represents the method’s arguments. The second portion follows the closing parenthesis and represents the return type. The mapping between the Java type and C type is

Type Chararacter boolean Z byte B char C double D float F int I long J object L short S void V array [

Note that to specify an object, the «L» is followed by the object’s class name and ends with a semi-colon, ‘;’ .

The javap utility (included with the JDK) is very useful to show the signature to be used in JNI.

X:\>javap -s java.awt.Label Compiled from Label.java public class java.awt.Label extends java.awt.Component < public static final int LEFT; /* I */ public static final int CENTER; /* I */ public static final int RIGHT; /* I */ java.lang.String text; /* Ljava/lang/String; */ int alignment; /* I */ static <>; /* ()V */ public java.awt.Label(); /* ()V */ 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 addNotify(); /* ()V */ java.lang.String constructComponentName(); /* ()Ljava/lang/String; */ public int getAlignment(); /* ()I */ public java.lang.String getText(); /* ()Ljava/lang/String; */ protected java.lang.String paramString(); /* ()Ljava/lang/String; */ public synchronized void setAlignment(int); /* (I)V */ public void setText(java.lang.String); /* (Ljava/lang/String;)V */

Источник

Java method signature ljava lang string

This section illustrates how to call Java methods from native language methods. Our example program, Callbacks.java , invokes a native method. The native method then makes a call back to a Java method. To make things a little more interesting, the Java method again (recursively) calls the native method. This process continues until the recursion is five levels deep, at which time the Java method returns without making any more calls to the native method. To help you see this, the Java method and the native method print a sequence of tracing information.

Calling a Java Method from Native Code

To see how native code calls a Java method, let us focus on the implementation of Callbacks_nativeMethod , which is implemented in Callbacks.c . This native method contains a call back to the Java method Callbacks.callback .

JNIEXPORT void JNICALL Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth) < jclass cls = (*env)->GetObjectClass(env, obj); jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "(I)V"); if (mid == 0) < return; >printf("In C, depth = %d, about to enter Java\n", depth); (*env)->CallVoidMethod(env, obj, mid, depth); printf("In C, depth = %d, back from Java\n", depth); >
  1. Your native method calls the JNI function GetObjectClass , which returns the Java class object that is the type of the Java object.
  2. Your native method then calls the JNI function GetMethodID , which performs a lookup for the Java method in a given class. The lookup is based on the name of the method as well as the method signature. If the method does not exist, GetMethodID returns zero (0). An immediate return from the native method at that point causes a NoSuchMethodError to be thrown in the Java application code.
  3. Lastly, your native method calls the JNI function CallVoidMethod . The CallVoidMethod function invokes an instance method that has void return type. You pass the object, method ID, and the actual arguments to CallVoidMethod .
Читайте также:  Как посмотреть исходники python

Forming the Method Name and Method Signature

The JNI performs a symbolic lookup based on the method’s name and type signature. This ensures that the same native method will work even after new methods have been added to the corresponding Java class.

The method name is the Java method name in UTF-8 form. Specify the constructor of a class by enclosing the word init within angle brackets (this appears as «»).

Note that the JNI uses the method signature to denote the return type of a Java method. The signature (I)V , for example, denotes a Java method that takes one argument of type int and has a return type void . The general form of a method signature argument is:

The following table summarizes the encoding for the Java type signatures:

Java VM Type Signatures

For example, the Prompt.getLine method has the signature:

(Ljava/lang/String;)Ljava/lang/String;

The Callbacks.main method has the signature:

The signature indicates that the Callbacks.main method takes one parameter, a Java String object, and the method type is void.

Array types are indicated by a leading square bracket ([) followed by the type of the array elements.

Using javap to Generate Method Signatures

The Java class file disassembler tool, javap , helps you to eliminate the mistakes that can occur when deriving method signatures by hand. You can use the javap tool to print out member variables and method signatures for specified classes. Run the javap tool with the options -s and -p and give it the name of a Java class, as follows:

This gives you the following output:

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 ()V static ()V >

The «-s» flag informs javap to output signatures rather than normal Java types. The «-p» flag instructs javap to include private members.

Читайте также:  Php blob to json

Calling Java Methods Using Method IDs

When you invoke a method in the JNI, you pass the method ID to the actual method invocation function. Obtaining a method ID is a relatively expensive operation. Because you obtain the method ID separately from the method invocation, you need only perform this operation once. Thus, it is possible to first obtain the method ID one time and then use the method ID many times at later points to invoke the same method.

It is important to keep in mind that a method ID is valid only for as long as the class from which it is derived is not unloaded. Once the class is unloaded, the method ID becomes invalid. As a result, if you want to cache the method ID, be sure to keep a live reference to the Java class from which the method ID is derived. As long as the reference to the Java class (the jclass value) exists, the native code keeps a live reference to the class. The section Local and Global References explains how to keep a live reference even after the native method returns and the jclass value goes out of scope.

Passing Arguments to Java Methods

The JNI provides several ways to pass arguments to a Java method. Most often, you pass the arguments following the method ID. There are also two variations of method invocation functions that take arguments in an alternative format. For example, the CallVoidMethodV function receives all its arguments in one va_list type argument. A va_list type is a special C type that allows a C function to accept a variable number of arguments. The CallVoidMethodA function expects the arguments in an array of jvalue union types. The array of jvalue union types are as follows:

typedef union jvalue < jboolean z; jbyte b; jchar c; jshort s; jint i; jlong j; jfloat f; jdouble d; jobject l; >jvalue;

In addition to the CallVoidMethod function, the JNI also supports instance method invocation functions with other return types, such as CallBooleanMethod , CallIntMethod , and so on. The return type of the method invocation function must match with the type of the Java method you wish to invoke.

Calling Class Methods

  • Obtain the method ID using the JNI function GetStaticMethodID rather than the function GetMethodID .
  • Pass the class, method ID, and arguments to the family of class method invocation functions: CallStaticVoidMethod , CallStaticBooleanMethod , and so on.
static int incDepth(int depth) ;

We can call this class method incDepth from Java_Callback_nativeMethod using the following JNI functions:

JNIEXPORT void JNICALL Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth) < jclass cls = (*env)->GetObjectClass(env, obj); jmethodID mid = (*env)->GetStaticMethodID(env, cls, "incDepth", "(I)I"); if (mid == 0) < return; >depth = (*env)->CallStaticIntMethod(env, cls, mid, depth); .

Calling Instance Methods of a Superclass

  • Obtain the method ID from the superclass using GetMethodID rather than GetStaticMethodID .
  • Pass the object, superclass, method Id, and arguments to the family of nonvirtual invocation functions: CallNonvirtualVoidMethod , CallNonvirtualBooleanMethod , and so on.

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.

Источник

Оцените статью