users@jaxb.java.net

Re: restricting access to constructors of generated classes

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Thu, 05 Oct 2006 21:04:54 -0700

Lowell Kirsh wrote:
> Both of these seem to do the exact same thing. I'm curious, if there's
> a factory, why are the constructors public?

Because it's more convenient and terse if you can just create a new
instance via "new".

In 1.0, there was only a factory and no public constructor. People
didn't like that at all.

> My problem is that I want
> to only call the factory creation methods, and to not call the
> constructors directly. That way I can sometimes return a subclass. But
> sometimes I forget, and call the constructors directly, which in my
> case is a bug. So I'd like to prevent myself from shooting myself in
> the foot by making the constructors private. Is there a way to set all
> generated classes to have private constructors?

You should consider writing a plugin. You can either put @deprecated on
all constructors, and that way you can relatively quickly tell when
someone in your team makes that mistake.

I believe you can also change constructor access modifiers (if you
can't, let me know.)

Also, take a look at https://jaxb2-commons.dev.java.net/contribute.html
and consider hosting such a plugin on jaxb2-commons.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com