Instead of checking the type of class by doing getClass().getName() I would
recommend using
If (myclass instanceof com.name.webapp.entities.MetaDataEntity){
...
} else {
...
}
This is better because you can check the presence of a class at compile time
instead of runtime when you do things like Class.forName(className)
--
Dov Rosenberg
Conviveon Corporation
http://www.conviveon.com
On 10/21/04 7:49 PM, "Michael Wille" <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I have a strange question. I need to load a class that is inside a packkage
> of a web application from within a taglib that the web application is using
> within its context.
>
> Let me try and explain this:
>
> I have a bunch of entities in the package: com.name.webapp.entities. In this
> package, there are two base classes: Entity and MetaDataEntity. There are
> quite a few entity classes that are derived from these two clases.
>
> I've setup a taglib to take an array of either Entity or MetaDataEntity from
> the request's attribute map and loop through the array do some processing.
> The problem I have is that different processing needs to be done if the array
> is of a type derived from Entity or derived from MetaDataEntity. To do this,
> I get the first element in the array and get that object's superclass.
>
> So the code I have in the taglib to discover the super class is:
>
> Object entities[] = (Object []) pageContext.getRequest().getAttribute("list");
> Object entity = entities[0];
> className = entity.getClass().getSuperclass().getName();
> if(className.startsWith("com.name.webapp.entities.Entity"))
> // do one thing...
> else if(className.startsWith("com.name.webapp.entities.MetaDataEntity"))
> // do another...
> else
> // throw exception.
>
> (realizing that this will only work with direct descendants of the 2 base
> classes)
>
> Which works fine if the array is not empty. However, when entities.length ==
> 0, I have to use a different method:
>
> Object entities[] = (Object []) pageContext.getRequest().getAttribute("list");
> // class name from the array is [Lcom.name.webapp.entities.entityname, get rid
> of the [L...
> className = entities.getClass().getName().substring(2);
> className = Class.forName(className).getSuperclass().getName();
>
> However, I get a ClassNotFoundException. I believe that is because the taglib
> is using a different class loader than my webapp, which makes sense. But is
> there any other way (short of creating my own class loader for the webapp and
> accessing that loader from the taglib) to do this? I'd rather avoid a custom
> classloader at all costs.
>
> Thanks for any advice.
>
> -Mike
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]