12

Desired output examples:

(Lorg/w3c/dom/Node;)Lorg/w3c/dom/Node;
(Ljava/lang/String;)Lorg/w3c/dom/Attr;

Such signatures can be generated using javap utility:

javap -s -p org.w3c.dom.Node

But is there any way to generate them programmatically. I dont' want to manually code all the rules from jni specification.

alex2k8
  • 42,496
  • 57
  • 170
  • 221
  • @bemace, @birryre. Sorry to be not clear, added more details. – alex2k8 Dec 16 '10 at 02:43
  • possible duplicate of [Get JNI Signature for methods of nested classes](http://stackoverflow.com/questions/11444407/get-jni-signature-for-methods-of-nested-classes) – Paul Sweatte Sep 18 '14 at 20:31
  • Why? You know what methods you're going to call at complle time. Otherwise you can't compile your code. You don't need to generate this information at runtime. Or are you looking for the Reflection API? – user207421 Oct 24 '16 at 04:01

5 Answers5

4

http://asm.ow2.org/asm31/javadoc/user/org/objectweb/asm/Type.html#getMethodDescriptor%28java.lang.reflect.Method%29 provides exactly the result what you expect.

Offtopic note for the sake of completeness: In my use case I needed also conversion vice-versa. This can be achieved by methods Type.getArgumentTypes(sig) and Type.getReturnType(sig). Resulting array elements of type Type provide method getClassName() from which you obtain reference class via Class.forName or primitive class via simple if statement or map.

Tomáš Záluský
  • 10,735
  • 2
  • 36
  • 64
2
I generate like this:

private static String calculateMethodSignature(Method method){
        String signature = "";
        if(method != null){
            signature += "(";
            for(Class<?> c:method.getParameterTypes()){
                String Lsig = Array.newInstance(c,1).getClass().getName();
                signature += Lsig.substring(1);
            }
            signature += ")";

            Class<?> returnType = method.getReturnType();
            if(returnType == void.class){
                signature += "V";
            }else{
                signature += Array.newInstance(returnType,1).getClass().getName();
            }

            signature = signature.replace('.','/');
        }

        return signature;
    }
Summer
  • 91
  • 1
  • 5
0

I was once trying to create this long back about the generating the method signature, I remember doing this by following style, but i am not sure its a quiet long time

1) I have written my own class to generate the method signature 2) I have used the reflection class to get the method Names dynamically.

I hope this may help you to get an idea, if not the full solution for your problem

gmhk
  • 15,598
  • 27
  • 89
  • 112
0

User ASM Library of Objectweb. Its not only fast but you can have choice about traversing through class

Ratna Dinakar
  • 1,573
  • 13
  • 16
0

In Android Studio, the IDE itself create those signature automatically. Just declare your function and press Alt+Ctl then choose create JNI function for functionName.

Mohsents
  • 691
  • 11
  • 9