• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on jdk Proxy using InvocationHandler

 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?

-P
 
Istvan Kovacs
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful Thakare wrote:therez... therez

Please UseRealWords: "there's" or "there is".
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing it out Rob , did that mistake after almost 9 years on JR :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic