You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Michael Rasmussen <ra...@gmail.com> on 2005/02/16 01:24:48 UTC

Binding to a generic class at runtime?

I realize this is most likely a fairly basic question, but my searches
for the answer have led me nowhere.  I am attempting a fairly
straightforward binding of data to a compiled object using a factory. 
The problem is that I do not know until runtime what that class is. 
My present solution is to do essentially (sorry the real code is at
work)

Class c = Class.forName(stringWithMyClassname);
Class factory = c.getClasses()[0];
Method parse = factory.getMethod("parse", new Object[] { String.class });
myObject = parse.invoke(factory, new Object[] {myXml};
myObject.validate();

The rub is in the Class factory = c.getClasses()[0];

Is there a better way to get this factory and thus the object?  My way
has obvious seccurity concerns and is not very typesafe, even for
reflection.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: Binding to a generic class at runtime?

Posted by Michael Rasmussen <ra...@gmail.com>.
I guess I'm confised, how does this avoid me using the Factory?  Don't
I still need an instance of this loaded class to get its schema type? 
And won't I still have to get that instance by reflecting to the
factory?  I unfortuantely do not have the luxury of a known set of
shcemas.


On Tue, 15 Feb 2005 17:03:31 -0800, David Jencks <da...@yahoo.com> wrote:
> You can parse the xml into an "untyped" XmlObject using the XmlObject
> parse methods, then change the schema type to get the object you
> expect.  You'd be loading the schema type by reflection.
> 
> I use this (with a known set of schema types) to figure out what the
> xml is and possibly modify it slightly before claiming it is any
> particular schema type or validating it.
> 
> david jencks
> 
> On Feb 15, 2005, at 4:24 PM, Michael Rasmussen wrote:
> 
> > I realize this is most likely a fairly basic question, but my searches
> > for the answer have led me nowhere.  I am attempting a fairly
> > straightforward binding of data to a compiled object using a factory.
> > The problem is that I do not know until runtime what that class is.
> > My present solution is to do essentially (sorry the real code is at
> > work)
> >
> > Class c = Class.forName(stringWithMyClassname);
> > Class factory = c.getClasses()[0];
> > Method parse = factory.getMethod("parse", new Object[] { String.class
> > });
> > myObject = parse.invoke(factory, new Object[] {myXml};
> > myObject.validate();
> >
> > The rub is in the Class factory = c.getClasses()[0];
> >
> > Is there a better way to get this factory and thus the object?  My way
> > has obvious seccurity concerns and is not very typesafe, even for
> > reflection.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> > For additional commands, e-mail: user-help@xmlbeans.apache.org
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org


Re: Binding to a generic class at runtime?

Posted by David Jencks <da...@yahoo.com>.
You can parse the xml into an "untyped" XmlObject using the XmlObject 
parse methods, then change the schema type to get the object you 
expect.  You'd be loading the schema type by reflection.

I use this (with a known set of schema types) to figure out what the 
xml is and possibly modify it slightly before claiming it is any 
particular schema type or validating it.

david jencks

On Feb 15, 2005, at 4:24 PM, Michael Rasmussen wrote:

> I realize this is most likely a fairly basic question, but my searches
> for the answer have led me nowhere.  I am attempting a fairly
> straightforward binding of data to a compiled object using a factory.
> The problem is that I do not know until runtime what that class is.
> My present solution is to do essentially (sorry the real code is at
> work)
>
> Class c = Class.forName(stringWithMyClassname);
> Class factory = c.getClasses()[0];
> Method parse = factory.getMethod("parse", new Object[] { String.class 
> });
> myObject = parse.invoke(factory, new Object[] {myXml};
> myObject.validate();
>
> The rub is in the Class factory = c.getClasses()[0];
>
> Is there a better way to get this factory and thus the object?  My way
> has obvious seccurity concerns and is not very typesafe, even for
> reflection.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org