Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] How to resolve method by name and arguments

Hi Holger, 
   AspectJ Devs,

The example was deliberately chosen to depict the complexity of the problem.

That's what I want to find a solution for:
"You will still need to figure out which one to call depending on the value types in your arguments array"

interface MethodResolver {
   /** 
    * Resolve the most specific method in a manner similar to Java compiler. 
    * Should handle stuff such as overloading, generics, boxing, conversion, varargs, etc.
    */
   Method resolve(Class target, String methodName, Object... arguments) throws Exception; }

An implementation following as much as possible Java compiler resolution. See JSL reference: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12. Of course, there will be some corner cases and tradeoffs we could live with.

Hope this makes the requirements much more clear.

Once again thanks for your collaboration and feedback!

-Alex

P.S. Attached is the original request.
P.S. Replaying to aspectj-dev since I have no access to aspectj-users.

-----Original Message-----
From: Holger Hoffstätte [mailto:holger.hoffstaette@xxxxxxxxxxxxxx]
Sent: Thursday, April 07, 2011 12:37 PM
To: Alexandar Savov; aspectj-users
Subject: Re: [aspectj-dev] How to resolve method by name and arguments

Hi,

please keep emails on the list. Thanks.

>     * public Object getSomeProp(int primitiveInt, int primitiveInt2);
>     * public Object getSomeProp(int primitiveInt, Number number);
>     * public Object getSomeProp(Integer. intVarargs);
>  
> In order to call getSomeProp through reflection one should pass its 
> arguments as Object[]:
>  
>     Object[] arguments = new Object[] { Integer.valueOf(3),
> Integer.valueOf(7) };

The short answer is a) you cannot and b) even the compiler can get this wrong when your methods are "too overloaded"; you sometimes need to supply type casts as hints even in regular code. Whether this consequence of autoboxing is a bug, a feature or a problem with the API you are trying to call (why are there methods taking both ints and Numbers in the first place?) is debatable.
Though AFAIK ClassMate does allow you to find all methods matching certain types (incl. generics, which is why I posted the links), you will still need to figure out which one to call depending on the value types in your arguments array and a selection strategy that knows which method to prefer. Maybe always choosing a matching method that takes scalar values is correct; mabe it is not.
Regular reflection & autoboxing just do not mix very well.

-h
--- Begin Message ---
  • From: Alexandar Savov <asavov@xxxxxxxxxx>
  • Date: Wed, 6 Apr 2011 09:59:07 -0700
  • Thread-index: Acv0e/FdGgIypppFRmaIMTaH+Ysr4g==
  • Thread-topic: How to resolve method by name and arguments

Hi AspectJ Gurus,

 

I’m contacting you w.r.t. how to resolve a Java method provided its name and arguments.

 

My ultimate goal is to find out a ready-to-use class that implements this task as _closer_ as possible to the way Java compiler does (JLS ref: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12). I’m saying _closer_ cause such a class should consider all the tricky stuff, such as overloading, varargs, generics, boxing, conversions, etc.

 

Your help, thoughts and comments are highly appreciated. It will be great if you share your professional opinion or just point me to the right person/team to contact.

 

Thanks in advance!

 

- Alex

 

Aleksandar Savov

Sr. Member of Technical Staff

asavov@xxxxxxxxxx

16A "G.M. Dimitrov" Blvd, 8th floor, 1797 Sofia, BULGARIA

+359 2 80-41004  Office

+359 2 943-3337 Fax

+359 88 8664291 Mobile

 

vmware

 


--- End Message ---

Back to the top