title: How to get JNI-style signature string for a field or method?
body: <p>I am trying to inspect a Java class and automatically retrieve all of its methods and fields in a way that I can invoke them via JNI on Android. However, I can't figure out how to actually get the textual signature string programmatically; I know how to get it via <code>javap -s</code> from the command line but I would like to get it from within my program.</p>
  
  <p>The closest I can find is using <code>clazz.getMethods()</code> to get the individual methods, and then <code>Method.toGenericString()</code> to get a human-readable string, but that string is formatted like the Java code signature (e.g. <code>public int someMethod(int foo, java.lang.String bar)</code>) when I really just want the JNI-style signature (e.g. <code>(ILjava/lang/String;)I</code>), as I will need that string to call <code>getMethodID()</code> from JNI and I already have a perfectly-good parser that parses those JNI signatures in the first place.</p>
  
  <p>I could just manually maintain bindings for the classes I actually care about accessing from JNI, but I'd much rather just have everything handled as automatically as possible; less code is better, after all.</p>
  
  <p>I did find ASM which provides helper methods for this, but it appears to be a bytecode generation framework and I am not sure if it will be compatible with Android.</p>
  
  <p>Or, in other words, given the following class:</p>
  
  <pre><code>class SomeClass {
      String foo(int bar) { return "This is foo " + bar; }
  }
  </code></pre>
  
  <p>I want to create a class called <code>ClassInspector</code> which can do something like:</p>
  
  <pre><code>ClassInspector ci = new ClassInspector(SomeClass.class);
  int n = ci.getMethodCount(); // returns 1
  String name = ci.getMethodName(0); // returns "foo"
  String sig = ci.getMethodSignature(0); // returns "(I)Ljava/lang/string;"
  </code></pre>
  
  <p>Which is to say that I want to be able to get the output of <code>javap -s</code> from within Java, so that I can call Java methods from the native side.</p>