wood burning stoves 2.0*
The moose likes Java in General and the fly likes Question on jdk Proxy using InvocationHandler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question on jdk Proxy using InvocationHandler" Watch "Question on jdk Proxy using InvocationHandler" New topic

Question on jdk Proxy using InvocationHandler

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614

I have two questions (problems) on java proxy

I am using following code for generating proxy object

1) If i pass target (of type Class) as a class that extends one class that in turns implements one interface i.e the target indirectly implements the interface thru extended class
then i get class cast exception

where as if i directly implement the interface in target class then every one is happy..

what could be the problem?

2) is there a way to tell proxy object to monitor selected methods? example if i have foo and boo method in proxy object , i have to monitor only foo,currently i am doing it using if method==foo in invoke method, but is there any cleaner way?


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
1: Class.getInterfaces() returns only directly implemented interfaces, not ones implemented via inheritance. If you check its javadocs, you'll see 'The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object.'.

2: I know of no 'cleaner' way - isn't this how proxies and invocation handlers are supposed to work according to the documentation?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
1. :- hmmm I may be only option is to mention the interface directly instead of using getInterfaces
2. :- Yeha I agree they are supposed to work that way but i was wondering if there is some way like get proxy and now add filter.. but seems there is no way..

Thanks for your reply.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

Praful Thakare wrote:therez... therez

Please UseRealWords: "there's" or "there is".

How To Ask Questions How To Answer Questions
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Thanks for pointing it out Rob , did that mistake after almost 9 years on JR :-)
I agree. Here's the link: http://aspose.com/file-tools
subject: Question on jdk Proxy using InvocationHandler
Similar Threads
synthetic classes
AOP - Control Flow Pointcut
Dynamic proxies
JVM copies an object when it is exported to RMI registry?
What is the interface?