aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes findAncestorWithClass() Versus getAdaptee() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "findAncestorWithClass() Versus getAdaptee()" Watch "findAncestorWithClass() Versus getAdaptee()" New topic
Author

findAncestorWithClass() Versus getAdaptee()

Tiffiny Yang
Ranch Hand

Joined: Mar 29, 2006
Posts: 124
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

Joined: Mar 22, 2007
Posts: 329

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).


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Tiffiny Yang
Ranch Hand

Joined: Mar 29, 2006
Posts: 124
Thanks Sergio !!

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

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: findAncestorWithClass() Versus getAdaptee()