How know if a method is part of an interface?

I have a class named MyClass implementing an interface named MyInterface.

I have also an instance of java.lang.reflect.Method describing a method
of MyClass (is the parameter of the invoke method in an
java.lang.reflect.InvocationHandler).
I need to know if Method is part of MyInterface or not.
Is there a way other than checking the method name and parameters type?

Advertisements

Andrea Polci <> wrote:
> I have a class named MyClass implementing an interface named MyInterface.
>
> I have also an instance of java.lang.reflect.Method describing a method
> of MyClass (is the parameter of the invoke method in an
> java.lang.reflect.InvocationHandler).
> I need to know if Method is part of MyInterface or not.
> Is there a way other than checking the method name and parameters type?

See Method.getDeclaringClass(), which gives you the class (or interface)
that declared (as opposed to implemented) the method.

Advertisements

Oscar kind wrote:
> Andrea Polci <> wrote:
>
>>I have a class named MyClass implementing an interface named MyInterface.
>>
>>I have also an instance of java.lang.reflect.Method describing a method
>>of MyClass (is the parameter of the invoke method in an
>>java.lang.reflect.InvocationHandler).
>>I need to know if Method is part of MyInterface or not.
>>Is there a way other than checking the method name and parameters type?
>
>
> See Method.getDeclaringClass(), which gives you the class (or interface)
> that declared (as opposed to implemented) the method.

No, aMethod.getDeclaringClass() give me MyClass.class even if aMethod is
declared into MyInterface.

Andrea Polci wrote:
>>> I have also an instance of java.lang.reflect.Method describing a
>>> method of MyClass (is the parameter of the invoke method in an
>>> java.lang.reflect.InvocationHandler).
>>> I need to know if Method is part of MyInterface or not.
>>> Is there a way other than checking the method name and parameters type?

Can you tell us why? People do the wierdest things with reflection
and I'm wondering why you'd want to know. As part of some IDE plugin or ANT
tool it sounds great, but otherwise it sounds a bit odd.

Sudsy wrote:
> Andrea Polci wrote:
> <snip>
>
>>> See Method.getDeclaringClass(), which gives you the class (or interface)
>>> that declared (as opposed to implemented) the method.
>>
>>
>> No, aMethod.getDeclaringClass() give me MyClass.class even if aMethod
>> is declared into MyInterface.
>
>
> So did you check out the Class#isAssignableFrom( Class ) method? I don't
> have time to try it myself...but then I don't have the problem! ;-)

P.Hill wrote:
> Andrea Polci wrote:
>
>>>> I have also an instance of java.lang.reflect.Method describing a
>>>> method of MyClass (is the parameter of the invoke method in an
>>>> java.lang.reflect.InvocationHandler).
>>>> I need to know if Method is part of MyInterface or not.
>>>> Is there a way other than checking the method name and parameters type?
>
>
> Can you tell us why? People do the wierdest things with reflection
> and I'm wondering why you'd want to know. As part of some IDE plugin or
> ANT
> tool it sounds great, but otherwise it sounds a bit odd.

I'll try but it isn't easy in english for me.

I have many classes each exposing some properties (getter/setter
methods) and other methods, some of wich defined in a common interface.
Now I want to attach some common functionality to those properties and
common methods (for example user's permission checking, but not only) so
I have defined a class like this:

I don't know if this is a good solution, but it is the solution we
adopted and I can't change it easily. Consider that usually we accessed
the properties throw reflection and there were few places were we do this.

The problem is that in some code I need to get the functionality of the
PermissionChecker without knowing of it. In that code I access the
object throw some interfaces (implemented by the original class) that
define the properties and the other methods.

I think I can do this with the java.lang.Proxy class.
Consider this code:

Actually the whole reason of interfaces in Java is to support
these compile time checks in order to avoid failure during the
execution. Otherwise, the situation would be more like in
Smalltalk, where AFAIK such interfaces do not exist.

(If you want to check it at run time: This should not be
neccessary. But if you dynamically compile code at run time
without human intervention, then you might need this, if the
Java-Code was also generated at run time. In this case the
error messages from the compilation might be inspected.)

Andrea Polci wrote:
> I have a class named MyClass implementing an interface named MyInterface.
>
> I have also an instance of java.lang.reflect.Method describing a method
> of MyClass (is the parameter of the invoke method in an
> java.lang.reflect.InvocationHandler).
> I need to know if Method is part of MyInterface or not.
> Is there a way other than checking the method name and parameters type?

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!