users@jaxb.java.net

Re: Réf. : Re: Plugin problem

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 29 Aug 2005 12:47:46 -0700

Patrick BRUNETON wrote:
> I have been using JAXB 2 since two weeks. It works very fine ! I have
> developped my own plugin that modifies the generated code.

Cool. Would you be interested in hosting that on java.net?


> - My plugin has to modify the get/set methods generated by xjc. The
> existing API allow to add statments at the end of body of method but I did
> not find how to add statments at the begining. I need to transform the code
>
> getXXX()
> {
> return xxx;
> }
>
> into
>
> getXXX()
> {
> <my code>
> return xxx;
> }
>
> I managed to do it but I used reflection on the codemodel classes to
> access private fields.

I modified JBlock so that it has a notion of the "current position" to
which new code is inserted. It's kind of like a file pointer.

With this change, you can do:

        int oldPos = block.pos(0);
        // insert your code here...
        block.decl(...);
        block._for(...);
        
        // restore the old position
        block.pos(oldPos);

> - In the same way, I have to change the type of a generated field. It seems
> not possible with the current API. Again I used reflection to achieve my
> goals. Also it is not possible to remove a field from a JDefinedClass.
>
> Is it possible to add some methods in the codmodel to avoid these tricks ?

I added the setter method Jar.type(JType) and
JDefinedClass.removeField(JFieldVar)

CodeModel is intially developed as write-once API, so it makes me
nervous to make changes like this, but we'll see where it takes us...

All changes are already in the repository, so you can build it by
yourself (or otherwise wait for next Monday's weekly.)

> I had also another problem, but I don't expect a change for this because
> it's probably quite unusual : I need to be able to unmarshall into an
> existing object, rather than in a new object. I managed to do it but with a
> (little but ugly) patch to the source code.

In one of my hobby projects I had the same use case.

Are you just trying to reuse the top-level object, or are you trying to
reuse as many objects in a tree as possible? The former is easier, the
latter is harder. Mine was the former.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com