Abhishek
Abhishek

Reputation: 1031

Retrieve Interface Class of a Proxy Invocation Object in Java

I have an Interface whose implementation is deciding at Runtime and is given a Proxy Object as its dynamic implementation. I want to retrieve the Interface which this proxy object implements to know the methods in the interface. Is there any way I can do so in Java.

Upvotes: 1

Views: 109

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340873

Try this (using plain reflection):

Class<?>[] interfaces = proxyInstance.getClass().getInterfaces();

For the following code:

Object proxyInstance = Proxy.newProxyInstance(
    getClass().getClassLoader(), 
    new Class<?>[] {Serializable.class},
    new InvocationHandler() /**/);

It correctly return java.io.Serializable interface.

Upvotes: 3

Related Questions