Re: Method.getMethod for varargs?

Chris <[email protected]> Thu, 30 Nov 2017 14:12:06 +0000 (UTC)
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
--------------------------------------------
On Thu, 11/30/17, Eliot Moss <[email protected]> wrote:

 Subject: Re: [asm] Method.getMethod for varargs?
 To: [email protected]
 Date: Thursday, November 30, 2017, 2:44 PM
 
 On 11/30/2017 4:20 AM, [email protected]
 wrote:
 > Hi,
 > 
 > I'm new to using ASM and I'm
 trying to look up some method with varargs and I
 > don't understand how to format the
 string input to getMethod to match the
 > actual signature.
 > 
 > I'm calling getMethod like this:
 >         
 method = Method.getMethod(stringArg);
 > 
 > The method I want to inspect is
 declared in the code as:
 >    void log(Foo foo,
 Object... bar);
 > 
 > When I pass in "stringArg" as
 >     void
 log(mypackagage/Foo, [java/lang/Object)
 > I get back a Method where
 getDescriptor() returns this
 >    
 (Lmypackage/Foo;[Ljava/lang/Object;)V
 > 
 > If I make a MethodVisitor and look
 how the "desc" argument in the
 > visitMethodInsn(int opcode, String
 owner, String name, String desc, boolean
 > itf) looks like when visiting the
 "log" method, "desc"  looks like this:
 >    
 (Lmypackage/Foo;L[java/lang/Object;)V
 > 
 > That is, the former has "[L" and
 the latter has "L[" for the varargs; I'm
 > obviously doing this wrong.
 > 
 > I've tried calling getMethod with
 the bracket at the end:
 >     void
 log(mypackagage/Foo, java/lang/Object[)
 > and then my method gets the
 signature
 >   
 (Lmypackage/Foo;Ljava/lang/Object[;)V
 > Which still isn't a match with
 what I see in the visitor.
 > 
 > So what should I pass in to get
 the description with "L["?
 
 The usual Java Method.getMethod takes a
 String name for the name of the
 method, e.g., "log", and zero or more
 Class objects for the types of the
 arguments.  These would be written
 Foo.class and Object[].class.
 
 At the bytecode level, according to
 javap, the descriptor is:
  
 "(Lmypackage/Foo;[Ljava/lang/Object;)V"
 
 That should definitely match some
 method on the ASM side.  Since the ...
 stuff turns into an array type during
 compilation, ASM is not going to
 do anything special with it.  [L
 etc. is the correct form; L[ should
 not occur.  So I don't really have
 an answer.  Either something is
 pretty broken or you're reading that
 descriptor incorrectly ...
 
 Regards - Eliot Moss
 
 ===

Hi Eliot and thanks for the reply.
Seems like I misunderstood how to call getMethod. I didn't realize that I should call it with [] for arrays, so I can get it to match by doing that instead of me using only the [ to indicate the array. So my exact question was based on a misunderstanding on my side, thanks for setting me straight.

That said, this way it handles the varargs exactly as if the method signature was a "regular" array.
That is, I can call getMethod with
    "void log(mypackage/Foo ,java.lang.Object[])"
to get it to match, but in the source code, it is defined as
   "void log(mypackage/Foo ,java.lang.Object...)". 
And that doesn't work with getMethod as it just replaces "..." with "///"
I'm not at all familiar with the JVM instructions, so perhaps those two compile to the same thing and one should not be able differentiate these two cases when on the instruction level?
If so, I think I'm in trouble. What I'm trying to do is to write an analyzer program to count how many arguments the varargs method is called with in different places to make sure they conform to an informal set of rules in this software. And I'm not at all clear on how I can do that if I just see the varargs as an array. But again, I'm a newbie in this area

/Chris


-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws