• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inherited parameter classes in method invocation (reflection API)

 
Ismael Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

A custom TLD tag i'm developing has the attributes attrib and parser. The latter specifies a static method so that the first one will be its only parameter. What this method does is to parser it into a String.

What i'm trying to do is to output some beans with Calendar objects as attributes:



By Invoking getter methods of the Calendar objects, the API returns an GregorianCalendar one. So, by calling:


throws an NoSuchMethodException.

The question is, how can i handle with derived classes in Reflection?

Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THat's a hard one. Sure, you could try calling getSuperclass() on the class until it finds the method. But what if the method's parameter is an interface? You would have to check all interfaces, including those of getSuperclass() etc.

Can't you add the parameter classes as another attribute in a comma separated list?
 
Ismael Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that interesting reply and sorry for being my response so delayed.

Adding paramateres separated by commas is a good solution, but the complexity of something which should be really simple increases a lot.

A workaround i have made is to set a requisite for methods which will be used in these tags: They have only one parameter of type Object.

You do not need to do that by specifying in the tags the parameter types with commas, though.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of another possible solution.

- Use Class.getMethods() to find all public methods
- Iterate over the result
- If the current method has a matching name, use Method.getParameterTypes
- If the array has only one element, and that element's isInstance() method accepts the object, use that method
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!