Reflection problem (parameter subtypes)

D

Daniel Hagen

Hi All,

I believe this has been answered before but I didn't find an answer to
my question, so here we are:

I am using reflection (Class.getMethod()) to find a method of a class
and execute it.

Let's assume I have the following class:

class A
{
public void v(B b)
}

I want to get (and later invoke) v .

This works fine when I am using an instance of B as parameter:

A.class.getMethod("v",new Class[]{B.class});


Whoever, when I try to user a Subclass of B, getMethod fails.

So, let's assume

class C extends B
{
[...]
}

The call

A.class.getMethod("v",new Class[]{C.class});

will fail with a NoSuchMethodException.

Am I doing something wrong, or, if this is just the way reflection
works, any suggestions for workarounds?

Thank you

Daniel
 
S

Sebastian Scheid

Daniel Hagen said:
Hi All,

I believe this has been answered before but I didn't find an answer to
my question, so here we are:

I am using reflection (Class.getMethod()) to find a method of a class
and execute it.

Let's assume I have the following class:

class A
{
public void v(B b)
}

I want to get (and later invoke) v .

This works fine when I am using an instance of B as parameter:

A.class.getMethod("v",new Class[]{B.class});


Whoever, when I try to user a Subclass of B, getMethod fails.

So, let's assume

class C extends B
{
[...]
}

The call

A.class.getMethod("v",new Class[]{C.class});

will fail with a NoSuchMethodException.

I think that's ok because you don't have the method A.v(C).
Am I doing something wrong, or, if this is just the way reflection
works, any suggestions for workarounds?

Get the method with parameter B and pass an object of type C to it when
calling Method#invoke().

Sebastian
 
J

John C. Bollinger

Daniel said:
Hi All,

I believe this has been answered before but I didn't find an answer to
my question, so here we are:

I am using reflection (Class.getMethod()) to find a method of a class
and execute it.

Let's assume I have the following class:

class A
{
public void v(B b)
}

I want to get (and later invoke) v .

This works fine when I am using an instance of B as parameter:

A.class.getMethod("v",new Class[]{B.class});


Whoever, when I try to user a Subclass of B, getMethod fails.

So, let's assume

class C extends B
{
[...]
}

The call

A.class.getMethod("v",new Class[]{C.class});

will fail with a NoSuchMethodException.

Am I doing something wrong, or, if this is just the way reflection
works, any suggestions for workarounds?

By using the reflection APIs you are taking on some of the work that the
Java compiler otherwise performs for you. The JLS has the full
description of how method resolution is performed; see

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#20448

The section (15.12) is quite long, and for the general case you need to
support the whole specification. It will involve getting all the
methods of the object, determining which (if any) are applicable, and
which among those is the most specific. Chances are that you would be
better off avoiding this mess altogether if you can.

How to avoid the mess? It depends on what you're trying to do. The
best approaches don't rely on reflection for method invocation. I don't
have time to go into details at the moment, but there are various
techniques, typically involving use of known interfaces and/or
superclasses for method parameters.


John Bollinger
(e-mail address removed)
 
O

Oscar kind

Daniel Hagen said:
I am using reflection (Class.getMethod()) to find a method of a class
and execute it.

Let's assume I have the following class:

class A
{
public void v(B b)
}

I want to get (and later invoke) v .

This works fine when I am using an instance of B as parameter:

A.class.getMethod("v",new Class[]{B.class});

Whoever, when I try to user a Subclass of B, getMethod fails.

That's because the method v doesn't exist with a subclass of B as a
parameter. You'll need to search for it yourself. This code could nudge
you in the right direction:

Method[] methods = A.class.getMethods();
for (int i=0; i<methods.length; i++)
{
Method method = methods;
Class[] parametersTypes = method.getParametersTypes();
boolean usable = true;
for (int j=0; j<parametersTypes.length; j++)
{
Class parameterType = parameterTypes;
usable &= parameterType.isAssignableFrom(C.class);
}
// Use usable.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top