Click here to Skip to main content
15,892,643 members
Articles / Programming Languages / Java
Tip/Trick

JNI Signature for Java Method

Rate me:
Please Sign up or sign in to vote.
4.89/5 (2 votes)
15 Sep 2016CPOL 16.2K   3
How to build the JNI signature for a Java method

Introduction

This code snippet is useful to build the JNI signature from a java.lang.reflect.Method.

Background

I'm currently working with JDI for a project and I need to search for the JDI counterpart of Java methods.

Interested API is ReferenceType.methodsByName(String name, String signature), but it works with JDI signature.

I haven't found anything on the web to build a JNI signature without using a bytecode manipulator library, so I decided to write it by myself.

Using the Code

The code is pretty easy to use.

Once you have in your hand a java.lang.reflect.Method object, just call JNIUtil.getJNIMethodSignature() passing your method variable.

/**    Commodity utility for JNI
 */
final class JNIUtil
{
    private static final Map<Object, String> PRIMITIVE_SIGNATURES = new HashMap<>();
    static
    {
        PRIMITIVE_SIGNATURES.put(boolean.class, "Z");
        PRIMITIVE_SIGNATURES.put(byte.class, "B");
        PRIMITIVE_SIGNATURES.put(char.class, "C");
        PRIMITIVE_SIGNATURES.put(double.class, "D");
        PRIMITIVE_SIGNATURES.put(float.class, "F");
        PRIMITIVE_SIGNATURES.put(int.class, "I");
        PRIMITIVE_SIGNATURES.put(long.class, "J");
        PRIMITIVE_SIGNATURES.put(short.class, "S");
        PRIMITIVE_SIGNATURES.put(void.class, "V");
    }
    
    private JNIUtil()    {}
        
    /**    Build JNI signature for a method
     * @param m
     * @return
     */
    public static final String getJNIMethodSignature(Method m)
    {
        final StringBuilder sb = new StringBuilder("(");
        for(final Class<?> p : m.getParameterTypes())
        {
            sb.append(getJNIClassSignature(p));
        }
        sb.append(')').append(getJNIClassSignature(m.getReturnType()));
        return sb.toString();
    }

    /**    Build JNI signature from a class
     * @param c
     * @return
     */
    static String getJNIClassSignature(Class<?> c)
    {
        if(c.isArray())
        {
            final Class<?> ct = c.getComponentType();
            return '[' + getJNIClassSignature(ct);
        }
        else if(c.isPrimitive())
        {
            return PRIMITIVE_SIGNATURES.get(c);
        }
        else
        {
            return 'L' + c.getName().replace('.', '/') + ';';
        }        
    }
}

External References

Java VM Type Signatures are available here.

History

  • 15.09.2016 - First release
  • 15.09.2016 - Added 'External references'

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Italy Italy
I had interest in C/C++ during high school, I'm working as developer since 1998.

2016-Now : I'm working on a basic debugger for DSL using JDI.

2014-2016: Small project focused on XBRL files production.
Built a stand-alone engine for XBRL.
Engine is capable to parse XBRL taxonomies and read/write XBRL instance files.

2010-2014: I have worked on a data conversion program from legacy system software heavly based on SQL/Hibernate/Spring written in Java.
Advanced use of Spring framework and its built-in aspect programming.
This program was thought and written from scratch only by me,so I was involved not only as developer but also as architect.

2009-2010 : Worked on a CASE suite used to create tax programs based on data models and DSL.
I had a deep experience about application engineering and embedded DSL creation with ANTLR (as part of a CASE suite used to create tax programs based on data models).

2004-2009 : Worked as web developer with Java/Hibernate/Spring for server side and JSP/CSS/Prototype on client side.

1998-2004 : I started with C++/MFC for various client application.
MS/Access and SQLServer for data storage and COM for modules inter-operability.

Comments and Discussions

 
QuestionMinor correction Pin
Nelek15-Sep-16 2:57
protectorNelek15-Sep-16 2:57 
PraiseRe: Minor correction Pin
Luca Basso Ricci15-Sep-16 3:39
Luca Basso Ricci15-Sep-16 3:39 
GeneralRe: Minor correction Pin
Nelek15-Sep-16 3:43
protectorNelek15-Sep-16 3:43 
You are welcome Smile | :)
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.