How to generate method signature?
Asked Answered
C

5

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.

Claytor answered 16/12, 2010 at 2:11 Comment(3)
@bemace, @birryre. Sorry to be not clear, added more details.Claytor
possible duplicate of Get JNI Signature for methods of nested classesCompel
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?Wright
A
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.

Asinine answered 4/3, 2012 at 0:37 Comment(0)
F
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;
    }
Fieldfare answered 24/10, 2016 at 3:53 Comment(2)
Not every parameter or return type is an array ... but some are.Wright
great idea! wonder why OP has not accepted this as the answer.N
J
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

Jagir answered 16/12, 2010 at 3:15 Comment(1)
Hard to see how this qualifies as an answer.Wright
T
0

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

Torrefy answered 16/12, 2010 at 5:14 Comment(0)
C
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.

Cephalochordate answered 16/8, 2023 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.