Bytecode manipulation - converting Bytecode representation to Java code representation

StackOverflow https://stackoverflow.com/questions/19805599

  •  04-07-2022
  •  | 
  •  

Pergunta

For my bytecode analysis project, I am using ASM library to manipulate bytecodes. At bytecode level this method declaration in source code:

void m(int i, String s)

is represented as a String:

(ILjava/lang/String;)[I
     |__ Parameters   |__ Return type 

here I am using string manipulation techniques to extract parameters out of that String. I need to get output like this in another String array: (So that I can convert byte code representation to corresponding Java representation):

{I,Ljava/lang/String;}

for this I have tried following regex to extract all the matches which Start with L and end with ; (to get Strings which are in the form of Ljava/lang/String;, others I can manage):

/\L([^L;]+)\;/

But it is not returning me any matches. My question is:

  • Can anyone help me to correct the above regex?
  • Or better if anyone has worked on these type of manipulations can you point me if any API is available to convert Bytecode representations --> Java code representations?
Foi útil?

Solução

You can read method desc using org.objectweb.asm.Type

    String desc = "(ILjava/lang/String;)[I";

    // params
    for(Type type : Type.getArgumentTypes(desc)){
        System.out.println(type.getClassName());
    }

    //return type
    System.out.println(Type.getReturnType(desc).getClassName());

output

int
java.lang.String
int[]

Outras dicas

As for the regexp, this should do the trick (the parameter type is in the first capture group afterwards, the whole match does match the L and the ;, too)
/L([^;]+);/

And here is one that should match the return type (if the string ends after the return type... if you have omitted something, tell me):
/\)\[(.+)$/

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top