I've created a patch for this.
Attachments:
Added_Objenesis_to_allow_for_no-default_constructor_instantiation.patch
2.1 KB
Johan, thanks for the patch but adding this dependency is not going to
happen.
You can do this yourself by replacing the DefaultCreator with your own
ObjectFactory:
http://code.google.com/p/morphia/source/browse/trunk/morphia/src/main/java/com/google/code/morphia/ObjectFactory.java
If you would like to make this an "extension" it would make more sense.
To make it an extension was my first intention as well but unfortunately
there are hard references to DefaultCreator#createInst(..) which is a
static method. Until this is resolved it's not possible to create an
extension (or am I wrong?)
However the dependency is relatively small and it would be possible to
embed it in the Morphia code.
There's another way though that we're using in PowerMock by using a
ReflectionFactory. Is not guaranteed to work on all JVM's but no one has
ever complained about it failing in PowerMock. Many JVM vendors does indeed
implement this class for compliance. Even if it shouldn't work on all JVM's
the new patch just throws an MappingException if fails so it shouldn't be
worse than what we get from Morphia today. XStream also uses this approach
to avoid requiring a default ctor.
Attachments:
Use_ReflectionFactory_to_allow_for_no-default_constructor_instantiation.patch
2.5 KB
DefaultCreator is just one implementation, the default one, and you don't
need to use any of that code in your extension; you can completely replace
it.
Can you please provide sample code of what this is fixing, or better yet,
what doesn't work now.
If you look at line 28 in
com.google.code.morphia.mapping.validation.fieldrules.VersionMisuse it uses
the createInst method statically from the DefaultCreator and this is what
concerns me.
I'm attaching a simple example of why I'd like to see this patch accepted.
If you remove the private ctor Morphia will not be able to map the
persisted entity back to the Account instance. I think this should be
possible without (in my view) cluttering the model with annotations. I want
the state in the Account to be immutable and thus I don't want setters and
having a non-default constructor is a good way of achieving this.
Sorry, here's the attachment.
Attachments:
Account.java 570 bytes
Btw, I was thinking more about s.th. like jacksons @JsonCreator:
http://wiki.fasterxml.com/JacksonFeatureCreators
Yes, it's another annotation, but it would integrate well with the existing
morphia annotations.
For technical reasons (if you're using a JVM that doesn't include the
ReflectionFactory class) I agree that having such an annotation would be
beneficial. But I believe that you should try to be as non-intrusive to
your domain/entity model as possible (of course it's always a trade-off),
especially when the framework _can_ fix the issues for us. To me this issue
is exclusively a technical issue that can be resolved by the framework
itself without user intervention. I think that XStream is a good example of
a mapping framework that is non-intrusive. It (almost) doesn't force you to
follow some predefined coding model such as using getters and setters or
using a default constructor.
I'm new to Morpia so I don't really know the philosophy behind it yet but I
could very well imagine having both the ReflectionFactory to instantiate
classes without using a constructor _and_ having annotations as fallback.
Although I think the ReflectionFactory approach will be sufficient for the
vast majority of users.