Skip to main content

Generating value constructors from XJC

18 replies [Last post]
skaffman
Offline
Joined: 2004-03-14
Points: 0

Is there a way of making XJC generate full value constructors?

For example, rather than generating this source:

public class XYZ {
private String s;
}

it would generate:

public class XYZ {
private String s;

public XYZ() {}

public XYZ(String s) {
this.s = s;
}
}

A default constructor would still be generated to keep the JAXB runtime happy, but the value constructor would greatly improve the generated code's useability as a java object model.

Of course, there's nothing to stop me from adding these constructors after XJC has finished, but I'd rather not make modifications to the generated code.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
skaffman
Offline
Joined: 2004-03-14
Points: 0

OK, plugin src and www stuff checked in, now visible on https://jaxb2-commons.dev.java.net/value-constructor/

I'd tried to use the same "snapshot/XXXXXX/blah.jar" download link as the fluent-api plugin, but i can't get the link to work. Do you need t do something special to gran the snapshot sub-directory under www/calue-constructor?

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Cool!

The broken link is simply because Unix file systems are case sensitive. It has to be "SNAPSHOT", not "snapshot".

skaffman
Offline
Joined: 2004-03-14
Points: 0

Doh! Well spotted, that man.

Fixed.

dennisbyrne
Offline
Joined: 2006-02-03
Points: 0

Has anyone figured out how to do this via command line yet?

collarwhiteblue
Offline
Joined: 2006-03-30
Points: 0

I'm trying your plugin but such an error occurs:

"build.xml": D:\jbprojects\Swing\testJAXB\build.xml:42: unrecognized parameter -Xvalue-constructor at line 42
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:393)
at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:381)
at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:314)
at com.sun.tools.xjc.XJCTask.execute(XJCTask.java:65)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeTarget(Project.java:1214)
at org.apache.tools.ant.Project.executeTargets(Project.java:1062)
at org.apache.tools.ant.Main.runBuild(Main.java:673)
at org.apache.tools.ant.Main.startAnt(Main.java:188)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)
Caused by: com.sun.tools.xjc.BadCommandLineException: unrecognized parameter -Xvalue-constructor
at com.sun.tools.xjc.Options.parseArguments(Options.java:483)
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:391)
... 13 more
--- Nested Exception ---
com.sun.tools.xjc.BadCommandLineException: unrecognized parameter -Xvalue-constructor
at com.sun.tools.xjc.Options.parseArguments(Options.java:483)
at com.sun.tools.xjc.XJC2Task._doXJC(XJC2Task.java:391)
at com.sun.tools.xjc.XJC2Task.doXJC(XJC2Task.java:381)
at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:314)
at com.sun.tools.xjc.XJCTask.execute(XJCTask.java:65)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeTarget(Project.java:1214)
at org.apache.tools.ant.Project.executeTargets(Project.java:1062)
at org.apache.tools.ant.Main.runBuild(Main.java:673)
at org.apache.tools.ant.Main.startAnt(Main.java:188)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)

skaffman
Offline
Joined: 2004-03-14
Points: 0

I'm guessing that the plugin JAR isn't on the classpath. I think (correct me if I'm wrong) that XJC will scan the manifests of each JAR it can find, loading plugins as it finds them.

collarwhiteblue
Offline
Joined: 2006-03-30
Points: 0

Sorry, that error has been fixed.

lyin
Offline
Joined: 2007-01-16
Points: 0

I am also trying to use the value-constructor plugin, but got the error "unrecognized parameter -Xvalue-constructor" as follows:

BUILD FAILED
/scratch/lyin/view_storage/lyin_j2ee/oc4j/build.xml:2145: The following error oc
curred while executing this line:
/scratch/lyin/view_storage/lyin_j2ee/oc4j/build.xml:2185: unrecognized parameter
-Xvalue-constructor
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(Projec
tHelper.java:539)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:384)
at org.apache.tools.ant.taskdefs.CallTarget.execute(CallTarget.java:107)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExe
cutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

Here's what I have in my ant build file:











I've included the plug-in jar in classpath, what else could be wrong?

Thanks for your help in advance.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

You might want to contact the author directly. I don't think he's monitoring the forum.

dma_k
Offline
Joined: 2006-11-03
Points: 0

Hi!

Where can I get the sources of this plugin?

davidhoffer2
Offline
Joined: 2006-11-17
Points: 0

I notice the value-constructor plugin creates constructors for all parameters irregardless if they are required. Is this customizable? What I would like are over-loaded constructors that take all the required parameters and then optionally the others.

Also I have one other request that I don’t know fits into this plugin or not. Here goes…for all attributes with type of xs:ID I want to auto set this to be _[GUID] where [GUID] is an actual auto generated GUID value. I have the code to generate the GUID I just need a way to feed this to the value-constructor (or something) such that in all constructors, default no-arg and fully-initializing, these member variables are set with this value. In this case I would prefer the element not be included in the fully-initializing constructors.

The reason for this is that I want to use ID/IDREF in the schema to limit duplication but I do not want the Java object users to have to ‘know’ to set the xs:ID value since it is not part of the object model, rather it is just part of the XML implementation details that I want to fully isolate Java users from.

Are there ways other than using the value-constructor to get this effect?

ozgwei
Offline
Joined: 2005-10-11
Points: 0

The same here. I'd like the value-constructor plugin creates a constructor for all required elements that don't have a default value (the default value being set by the default-value-plugin), instead of a constructor that takes all elements regardless.

The problem I have is that we have just added a required element with a default value, which causes all codes that use the full element constructor to fail compilation. I'd like to enforce mandatory no-default elements to be set in constructor but leave the optional elements or elements with a default value to be set using the fluent api.

ozgwei
Offline
Joined: 2005-10-11
Points: 0

Hi all,
I have modify the value-constructor plugin to create constructors for any combination of the following scenarios:
1) only required fields with no default value
2) only required fields
3) the original implementation for all parameters. A @deprecated annotation can also be generated if specified in the option

The constructors are created in the above order, so if the signature of a constructor is already used, it won't be created.

Since I don't have write privilege, I'm attaching the source code and the JAR file here.

Hope this will help.

Alex

andrewshilliday
Offline
Joined: 2014-01-30
Points: 0

I realize that it's been a long time since you made this post, but I am interested in seeing your modification to the value-constructor plugin that creates constructors for only required fields (in addition to the other scenarios).

Your post said that you attached it, but I don't see any attachments.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

I agree that this would be useful.

There's a fluent-api plugin at https://jaxb2-commons.dev.java.net/ that can mitigate the pain.

Beyond that, would you be interested in writing a plugin that generates these code?

skaffman
Offline
Joined: 2004-03-14
Points: 0

Right, I've written a plugin, and requested developer role on the jaxb2-commons project.

I'll knock up a web page for it and check it in as soon as ai have access.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Cool!

Sorry for the delay. I just approved it.
Please see https://jaxb2-commons.dev.java.net/contribute.html

We need to decide the name of your plugin (and consequently which part of the CVS repository you'll store your stuff.)

skaffman
Offline
Joined: 2004-03-14
Points: 0

I see you've created a value-constructor directory, thanks. I'll get it all checked in as soon as I can get somewhere where pserver will work :)

I was considering writing another plugin which generates equals(), hashCode() and toString() methods using commons-lang's Builder utilities.