java - How to convert method.getGenericReturnType() to a jvm type signature -


how convert instance of java.lang.reflect.type jvm generic type signature?

type type = method.getgenericreturntype(); string signature = totypesig(type); 

for instance type:

map<string, map<?, ? super integer>> 

must become string:

"ljava/util/map<ljava/lang/string;ljava/util/map<*-ljava/lang/integer;>;>;" 

how people using orb.objectweb.asm.classwriter solve this?

i've done iterating on type information. i'd prefer more robust. (i've posted own solution answer because it's bit long)

i guess want jvm signature class file (or did mean source file?)

from class following solution

public class getsignature {      // method want retrieve signature     map<string, map<?, ? super integer>> somemethod() {         return null;     }      public static void main(string[] args) throws exception {         // make private method in class method accessible         method methodgenericsignature = method.class.getdeclaredmethod(                 "getgenericsignature",                 (class<?>[]) null         );         methodgenericsignature.setaccessible(true);          // signature method         method method = getsignature.class.getdeclaredmethod("somemethod",                 (class<?>[]) null         );         string returnvalue = (string) methodgenericsignature.invoke(method,                 (object[]) null         );         system.out.println("signature: " + returnvalue);     } } 

output

signature: ()ljava/util/map<ljava/lang/string;ljava/util/map<*-ljava/lang/integer;>;>; 

edit small snipped demonstrate how signature using asm.

public class getsignaturedemo {      public static void main(string[] args) throws ioexception {         inputstream = new fileinputstream("/tmp/getsignature.class");         classreader classreader = new classreader(is);         classreader.accept(getclassvisitor(), 0);     }      private static classvisitor getclassvisitor() {         return new classvisitor(opcodes.asm4) {             @override             public methodvisitor visitmethod(int access, string name,                     string descriptor, string signature, string[] exceptions) {                 system.out.printf(                         "method: %s  descriptor: %s  signature: %s%n",                         name,                         descriptor,                         signature                 );                 return super.visitmethod(access, name, descriptor, signature, exceptions);             }         };     } } 

sample output

method: somemethod  descriptor: ()ljava/util/map;  signature: ()ljava/util/map<ljava/lang/string;ljava/util/map<*-ljava/lang/integer;>;>; 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -