Hi,

Comments below.

On Wednesday, February 12, 2003, at 09:48  AM, Paul Christmann wrote:
I'm interested in serializing a bunch of existing java objects that all
follow the same idiom:

class Foo {
      final int arg;
      private Foo (int arg) {
          this.arg = arg;
      }

      public static Foo newFoo (int arg) {
          return new Foo(arg);
      }

      public int getArg () {
          return arg;
      }
}

Is it possible to define a mapping file that can work with this class?
Not with the current version of castor.

It doesn't have a public constructor, so I immediately ran into that
MappingException. I read (and reread and reread) the tips-and-tricks
paragraphs on use of the "create-method" in the mapping, but couldn't
figure out how to make it apply -- its not a property of my Foo that I
need created, but the Foo itself. I found posts in the archives dealing
with use of FieldHandlers to provide flexibility for constructing
objects; but again -- those seem to apply to creating objects on a
class. In some of my cases, the Foo will be the root element in the mapping.

Is this possible? If so, any pointers to where I can learn how? If
not, is there a particular reason why not (meaning, would developers be
open to support of this if it was possible to define a mapping that
described it and I could provide such a patch (which is not to say I have such a patch yet, but I would be willing to dig into it))?
As you have found, none of the current mapping options can handle a static factory method that is contained in the class it creates. I have been working on adding generic typesafe enumeration support to castor (bug 1157; http://bugzilla.exolab.org/show_bug.cgi?id=1157). The interface for a typesafe enumeration is exactly what you outlined above -- only the semantics are different (you can look at the RFC that is posted with that bug for details of what I mean).

(In point of fact, the enumeration support is done -- I'm just having trouble integrating it into JDO, which is what I really need.)

So that could possibly get you a bit closer. The way I'm doing enums may still be incompatible with what you want. For one thing, the implementation is through a field handler. As you note, you can't use a field handler for a top-level class. What you can do, though, is define a wrapper class with one field that marshals out the way you want it. I haven't ever done this personally, but it comes up on the list all the time. Check the archives.

Second, my current enum implementation assumes that the enum instance has no children. This is a perfectly reasonable assumption for enumerations, but not for your case. This could be a substantial stumbling block, or not -- I'll have to think about whether it is reasonably surmountable.

Anyway, that enumeration code has not been integrated into castor (or even submitted for review) yet. If it is worthwhile, I could try to package the generic enumeration modifications in a way that would work with castor XML only while I'm sorting out the remaining JDO problems.

Rhett
--
Rhett Sutphin
Research Assistant (Software)
Coordinated Laboratory for Computational Genomics
and the Center for Macular Degeneration
University of Iowa - Iowa City, IA 52242 - USA
4111 MEBRF - email: [EMAIL PROTECTED]

----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-dev

Reply via email to