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.
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() {}
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();
}
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'
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.