• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

findAncestorWithClass() Versus getAdaptee()

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am exercising to see how Classic tag embeded within Simple works.

<x:simpleTag company="NHK" address="Bank St.">
<x:classicTag age="19"/>
</x:simpleTag>

simpleTag points MySimple which extends SimpleTagSupport; classicTag points MyClassic which extends TagSupport

Inside MyClassic.java, I can get parent without problem using
MySimple parent = (MySimple)((TagAdapter)getParent()).getAdaptee();

According to lots of study materials saything that -- "An alternative way to find a specific parent tag is with the findAncestorWithClass() method provided by the SimpleTagSupport and TagSupport classes",
so I tried to use following
MySimple parent = (MySimple)findAncestorWithClass(this, MySimple.class);

However, I got parent null

What's wrong?

Thanks
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiffiny,

That is really interesting. I tried your example and I got the same results.

But after modifying MyClassic.java to :

...it worked fine (yes, findAncestorWithClass is static in both SimpleTagSupport and TagSupport).

I think the reason comes from the fact that TagSupport's findAncestorWithClass method returns an object of Tag klass, and it would be illegal for that method to return an object of MySimple klass (which only implements JspTag, but does not implement Tag).
So, it seems that findAncestorWithClass is intelligent enough to avoid producing a ClassCastException (at least tomcat's implementation, I have not read the specs yet to confirm this is the defined behaviour).
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sergio !!

Yes, you're right, this one works, and thanks for explaination
MySimple t = (MySimple)SimpleTagSupport.findAncestorWithClass(this, MySimple.class);

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic