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.
The signature of a method consists of the name of the method and the description (i.e., type, number, and position) of its parameters. Example: toUpperCase() println(String s)
A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type. exceptions that might be thrown or passed back.
9. Which three are valid method signatures in an interface? private int getArea(); public float getVol(float x);
The only required elements of a method signature are the method's return type, the method name, parentheses for the parameter list (which can be empty), and the method body inside curly braces (which can also be empty).
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With