Android Development Practice: Jni function Signature Generator

Source: Internet
Author: User

"Function signing" is common in Android NDK development, and because Java supports overloading, a method can only be determined by the function name alone. Thus, JNI provides a set of signature rules that uniquely identify a Java-side defined native method with a string.


The signature string for each of the Java data types is as follows (from the introduction of the Oracle website JNI):


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/56/FD/wKiom1SOxRfjmgaBAAHVKEL-bG0476.jpg "title=" Oracle _jni.png "alt=" Wkiom1soxrfjmgabaahvkel-bg0476.jpg "/>


The principle is not complex, each basic type corresponds to a single character signature, and the class corresponds to the "L" + class of the full path + ";", the array type corresponds to the "[" + element type of signature, the signature of the function is: (each parameter type signature) + return type signature.


Figuring out the fundamentals, we can try customizing a Java tool class to generate a signature string for the Java native function, with the following code:


/* *  copyright notice   *  copyright  (C)  2014,  ticktick <[email protected]> *  http://ticktick.blog.51cto.com/ *     *   @license  under the apache license, version 2.0   * *   @file     SignatureGen.java *   @brief     Implement a java class for jni signature generate *    *   @version  1.0      *   @author    ticktick *   @date     2014/12/15   *  */package  com.ticktick.library;import java.util.HashMap;public class SignatureGen {     public static final HashMap<String,String> Primitives =  New hashmap<string,&nBsp String> ();    static {         Primitives.put (Void.class.getName (), "V");         primitives.put ( Boolean.class.getName (), "Z");         primitives.put (Byte.class.getName (), "B"),         primitives.put (Character.class.getName (), "C");         primitives.put (Short.class.getName (), "S");         primitives.put (Integer.class.getName (), "I");         primitives.put (Long.class.getName (), "J");         primitives.put ( Float.class.getName (), "F");         primitives.put (Double.class.getName (), "D");    }             Public static string&nbSp;getsignature ( Class ret, Class...params )  {         stringbuilder builder = new stringbuilder ();         builder.append ("(");         for ( Class  param : params )  {             Builder.append (Getsignature (param));        }         builder.append (")");         builder.append ( Getsignature (ret));         return builder.tostring ();     }    protected static string getsignature ( Class  param )  {        stringbuilder builder = new  stringbuilder ();         string name =  "";         if ( param.isarray ()  )  {             name = param.getcomponenttype (). GetName ();             builder.append ("[");        }         else {             name = param.getname ();        }             if ( primitives.containskey (name)  )  {    builder.append (Primitives.get (name));         }        else {              Builder.append ("L" +name.replace (".", "/") + ";");         }           Return builder.tostring ();     }}


the Signaturegen class provides a function getsignature that supports arguments to obtain a signature string for a Java function, the first parameter is a class object of the function return value type, and the argument is a class object of each function parameter type .


For example, print out a signature string for a different type of function, as shown below.


LOG.D ("Signature", "void func ()--" + signaturegen.getsignature (void.class)); LOG.D ("Signature", "Boolean func ()--" + signaturegen.getsignature (boolean.class)); LOG.D ("Signature", "int func (Boolean a)--" + signaturegen.getsignature (integer.class,boolean.class)); LOG.D ("Signature", "int func (Boolean a,string B)--" + Signaturegen.getsignature (Integer.class,boolean.class, String.class)); LOG.D ("Signature", "int func (byte[] c)--" + signaturegen.getsignature (integer.class,byte[].class)); LOG.D ("Signature", "long func (int n,string str,int arr)--" + Signaturegen.getsignature (Long.class,integer.class, String.class,integer[].class));


The output screenshot is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/56/FB/wKioL1SOyGLycSFiAAFsjPzuhjU727.jpg "title=" Test_ Out.png "alt=" Wkiol1soyglycsfiaafsjpzuhju727.jpg "/>

About the JNI function signature Generator introduced here, the principle is not complex so I did not carry out too much analysis, I hope this tool can be useful in future projects, have any questions welcome message or letter [email protected] exchange.



This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1590209

Android Development Practice: Jni function Signature Generator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.