Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Advanced-java] Another odd question - how to convert String to literal?
- From: philion@xxxxxxxxxxxxxx (Paul Philion)
- Subject: [Advanced-java] Another odd question - how to convert String to literal?
- Date: Fri, 29 Mar 2002 12:15:40 -0500
Alvin - Um... why do you want to? The easy answer is: Cast it. A more useful answer, however, it to ask yourself why you need the class type. If you are going to write literal code against the deserialized object, then the code has to know what type the object is. In similar circumstances, I have made the MyClass implement an interface that all the DB-serialized objects implement. That way, I always cast the deserialized object into the interface and go from there. HOWEVER... If you are building something a little more generic, and you might be storing objects like Customer and Product (that are very different and don't really share an abstract interface) then you need to build some sort of flexible Mediator code that examines the object, reads the class directly form the object (no need for the string class name in the db) and determines what to do with the object based on the class. Typically, I'll do a callback-type structure where different classes will register with the Mediator to be notified when an object of a certain type is deserialized from the db. The interaction would go something like this: - MyClass instance "a" is serialized to the db. - MyClassHandler registers itself with the Mediator, asking to be notified when classes of type "MyClass" are deserialized. - The Mediator stores the reference to the MyClass and to MyClassHandler (in an internal hashmap, for example). - MyClass instance "a" is deserialized into Object "x". - The Mediator gets the class from Object "x" (which is MyClass) and looks it up. - The Mediator discovers that MyClassHandler is interested in MyClass objects. - The Mediator calls a method on MyClassHandler and passes object "x". - MyClassHandler knows (by contract, it only registered to be notified of MyClass instances. An excellent place to but the new 1.4 assertions to work) that the object "x" is really a MyClass, so it just casts it. - MyClassHander then does all the work necessary to make stuff happen correctly with the MyClass instance "a". I hope this doesn't further confuse stuff for you... - Paul Philion ----- Original Message ----- From: "Alvin Wang" <xwang@xxxxxxxxxxxxxxxxx> To: "Advanced-Java" <advanced-java@xxxxxxxxxxxxxxxxxxxxxx> Sent: Friday, March 29, 2002 11:36 AM Subject: [Advanced-java] Another odd question - how to convert String to literal? > Hi! Thanks for those helping me with my odd question. Maybe they are naive. > Here is another one. > > I serialized an MyClass object into byte[], and then put the byte[] into > database, along with the class name "MyClass" as String. Later on, another > program looks up the databaase, and picks up the byte[] and the class name > "MyClass". It will deserialize byte[] into Object obj. However, I do not > know how to convert obj to be a MyClass object. That means I do not know how > to convert String "MyClass" to java language class name literal of MyClass. > I do not think Reflection will help, because I am not building a new MyClass > object. > > Can any guru help me out? Thanks! > > _______________________________________________ > Advanced-java mailing list > Advanced-java@xxxxxxxxxxxxxxxxxxxxxx > http://lists.xcf.berkeley.edu/mailman/listinfo/advanced-java >
- References:
- Prev by Date: [Advanced-java] Another odd question - how to convert String to literal?
- Next by Date: [Advanced-java] [±¤°í]100¸¸¿øÀ¸·Î ¿µÈ°ü»çÀå´ÔÀÌ µÇ¼¼¿ä!!
- Previous by thread: [Advanced-java] Another odd question - how to convert String to literal?
- Next by thread: [Advanced-java] [±¤°í]100¸¸¿øÀ¸·Î ¿µÈ°ü»çÀå´ÔÀÌ µÇ¼¼¿ä!!
- Index(es):