Is there a (better) way to dynamically create Objects?
Right now I'm using a simple 'factory pattern' solution as following:
String classType = generalObject.getClass().toString();
if(classType.equals("class be.testApp.UserObject")) {
return UserObject.fromByteArray(data);
//return new UserObject();
}
else if(classType.equals("class.be.testApp.NewsObject")) {
return NewsObject.fromByteArray(data);
//return new NewsObject();
}
toStringto get your class as a String... usegetPackageandgetNameinstead - or use theclassattribute.getPackage().getName()returns the fully qualified namegetClass().getName()instead ofgetClass().toString(). At least you'll get rid of the unreadable"class "part of the string. Better: useClass type = generalObject.getClass();andif (type.equals(UserObject.class)).