0

I'm trying to extract method and argument names from JAR file using ASM (org.objectweb.asm) library. I use MethodNode.localVariables property to get those argument names.

I know I have to compile java sources using javac -parameters to embed parameter names into the class files, but for some reason MethodNode.localVariables is always null. I can tell that parameter names are available because I'm able to extract them with Java reflection. MethodNode.localVariables is not null only when I use the option -g:vars, which is what I don't want to do.

Is there any reason why ASM can't extract parameter names from classes compiled with the -parameters option?

majakthecoder
  • 177
  • 3
  • 14
  • 2
    As the name strongly suggests, `localVariables` is for local variables. Not parameters. – rzwitserloot Oct 07 '21 at 21:41
  • 2
    Have you tried: https://javadoc.io/doc/org.ow2.asm/asm/5.2/org/objectweb/asm/tree/MethodNode.html#parameters – boneill Oct 07 '21 at 21:50
  • @rzwitserloot, `localVariables ` contains also argument names, including `this` reference for non static methods. – majakthecoder Oct 07 '21 at 21:59
  • 2
    @majakthecoder No, they don't. – rzwitserloot Oct 07 '21 at 22:44
  • @rzwitserloot they do, I tested it. Check out the first answer here: https://stackoverflow.com/questions/2729580/how-to-get-the-parameter-names-of-an-objects-constructors-reflection if you compile java with `javac -g:vars` you will get argument names in `localVariables`. – majakthecoder Oct 07 '21 at 23:57
  • 2
    @majakthecoder and that's not where `-parameters` goes, nor what the relevant reflection methods look at. It's a bit odd to ask a question but then insist you know precisely how it all works. Did you check boneill's link? – rzwitserloot Oct 08 '21 at 00:07
  • @boneill, you're right, `parameters` is what I need. I was confused by some old posts where ppl suggested to use `localVariables` for this. Looks like parameters of a method goes to `MethodNode.localVariables` when a class is compiled with `-g:vars`, and to `MethodNode.parameters` when compiled with `-parameters`. – majakthecoder Oct 08 '21 at 23:34
  • 2
    That's because `-parameters` is much newer than the debug information about local variables. So you might find a lot of recommendations from the time where the `localVariables` was the only option. – Holger Oct 09 '21 at 08:07

0 Answers0