users@jaxb.java.net

Re: (JAXB Plugin) Customize the body of a setter method to fire bound properties

From: Chris Campbell <Christopher.Campbell_at_Sun.COM>
Date: Fri, 12 Jan 2007 09:51:07 -0800

Thanks, Kohsuke and Jerome. I asked the following on Kohsuke's blog,
but probably would be better to move the discussion here.

1) I just tried it with the xjc that comes with JDK 6, and the option
wasn't recognized (says "unrecognized parameter -Xinject-listener-
code"):
% xjc -classpath property-listener-injector-1.0.jar -Xinject-listener-
code catalog.xsd

However, the plugin works fine if I run it using the xjc that comes
with JAXB 2.1 RI. So is this plugin dependent on JAXB 2.1? Is there
a reason why it wouldn't work with the JAXB 2.0 that comes with JDK 6?

2) (From Mikael Grev...) The property name fired has the first
character upper case (should be lowercase).

3) Why does it only generate "constrained" properties?
http://java.sun.com/docs/books/tutorial/javabeans/properties/
constrained.html

These are arguably more complex than the usual "bound" properties:
http://java.sun.com/docs/books/tutorial/javabeans/properties/bound.html

Even the way the plugin generates "constrained" properties is not
quite right, as Mikael pointed out already on Kohsuke's blog. For
true "constrained" support, it would need to follow the steps listed
on that tutorial page.

Personally, I would prefer that the plugin generate only "bound"
properties by default. For example:

     @XmlTransient
     private final PropertyChangeSupport pcs = new
PropertyChangeSupport(this);

     public String getTitle() {
         return this.title;
     }

     public void setTitle(String title) {
         String old = this.title;
         this.title = title;
         this.pcs.firePropertyChange("title", old, title);
     }

Then if the user really wants "constrained" support (usually in
addition to, but I suppose could be in place of) "bound" support,
they could specify another argument to get that behavior. For example:
% xjc -Xinject-bound-properties ...
% xjc -Xinject-bound-properties -Xinject-constrained-properties ...
% xjc -Xinject-constrained-properties ...

Jerome or Marcos, any opinions? Would either of you be willing to
take this on?

Thanks,
Chris


On Jan 11, 2007, at 7:03 PM, Kohsuke Kawaguchi wrote:
> I moved the plugin from Glassfish workspace to jaxb2-commons.
>
> See https://jaxb2-commons.dev.java.net/property-listener-injector/
>
> This is mostly the same code that Jerome had.
>
> Marcos, it looks like you found a bug in it. If you are interested
> in looking at the code, I'd be happy to make you a committer.
> Otherwise, if you can send me a test case, I'd be happy to take a
> look.
>
>
> Kohsuke Kawaguchi wrote:
>> Marcos wrote:
>>> Hi all,
>>> I'm trying to write a plugin based on the one developed by Mr.
>>> Jerome Dochez, found at.:
>>> http://fisheye5.cenqua.com/browse/glassfish/admin-core/xjc-plugin/
>>> src/com/sun/tools/xjc/addon/property_listener_injector/
>>> PluginImpl.java?r1=1.1&r2=1.1.2.1
>> We are just talking about moving this to jaxb2-commons.
>>> JAXB's binding compiler generate setter methods for all elements
>>> found in the .xsd file
>>> but I would like to replace some parts to suit my needs .... as
>>> follows.:
>>> Before.: (Without the plugin)
>>> /**
>>> * Sets the value of the returnCode property.
>>> *
>>> * @param value
>>> * allowed object is
>>> * {_at_link String }
>>> * */
>>> public void setReturnCode(String value) {
>>> this.returnCode = value;
>>> }
>>> After.: (Using the plugin)
>>> /* PropertyChangeSupport inserted by my own plugin.*/
>>> private PropertyChangeSupport changes = new
>>> PropertyChangeSupport(this);
>>> /**
>>> * Sets the value of the returnCode property.
>>> *
>>> * @param value
>>> * allowed object is
>>> * {_at_link String }
>>> * */
>>> public void setReturnCode(String value) {
>>> String oldReturnCode = this.returnCode; // This must be
>>> inserted by my plugin
>>> this.returnCode = value; //
>>> I'm not sure if I can invoke the default implementation of JAXB
>>> to retrieve this line of code ...
>>> changes.firePropertyChange ("returnCode", oldReturnCode,
>>> returnCode); // This must be inserted by my plugin
>>> }
>>> I know that each plugin has 02 interesting hook methods.:
>>> onActivated (Options opt)
>>> run (Outline model, Options opt, ErrorHandler)
>>> How can I use these methods and JAXB RI API to inject the piece
>>> of code that I need ?
>> It really depends on what kind of code you want to inject. Why
>> don't you start by looking at Jerome's code and modify it to fit
>> your needs?
>
>
> --
> Kohsuke Kawaguchi
> Sun Microsystems kohsuke.kawaguchi_at_sun.com