Preventing others from using 'new' when object is created by Guice?

85 views
Skip to first unread message

Charles Roth

unread,
Jan 30, 2018, 11:45:23 AM1/30/18
to google-guice
A bit of a philosophical question, here.  Any thoughts welcome.

So I've adapted a project to use Guice to create, say, object Alpha, which gets @Inject-ed into other objects.  Everything works fine.

In theory, no code in the project (other than unit-tests) should ever use 'new' to create an Alpha.  Suppose I want to enforce that?  What can I do?
  1. I could @Deprecate the constructor (and explain it in a comment) so that anyone who attempts to try to use 'new' on it will at least get warned.
     
  2. I could make the constructor default or protected, and put Alpha in the same package as the Guice module class.
     
  3. I could add some horrible static to Alpha, that keeps track of how (or how many) times it gets constructed, and throw an exception.  (YUCK!!)
     
  4. I could examine the stack trace in the constructor, and throw an exception if Guice is not in the chain.  (Ditto ditto testing class.)  (Also yuck.)
     
  5. I could let go of my need to micro-manage things, accept the philosophy behind IOC, and simply accept that Guice will construct Alpha, and the rest of my team won't do really dumb things.  (OK, maybe I could add a comment to the constructor, too :-) )   See for example, Uncle Bob at http://blog.cleancoder.com/uncle-bob/2015/07/01/TheLittleSingleton.html
     
  6. I could argue with the developers of Java 10 (11, 12...?) and beg them to please put something like "friend" in Java, so I could (in this case) make the Guice module a "friend" of Alpha (or is it vice-versa?).

Did I miss anything?  How would you argue this in your shop?


Cheers!



Tavian Barnes

unread,
Jan 30, 2018, 11:50:00 AM1/30/18
to google-guice
On Tuesday, 30 January 2018 11:45:23 UTC-5, Charles Roth wrote:
A bit of a philosophical question, here.  Any thoughts welcome.

So I've adapted a project to use Guice to create, say, object Alpha, which gets @Inject-ed into other objects.  Everything works fine.

In theory, no code in the project (other than unit-tests) should ever use 'new' to create an Alpha.  Suppose I want to enforce that?  What can I do?
  1. I could @Deprecate the constructor (and explain it in a comment) so that anyone who attempts to try to use 'new' on it will at least get warned.
     
  2. I could make the constructor default or protected, and put Alpha in the same package as the Guice module class.
Do that, but don't necessarily bother putting it in the same package as your Guice module.  Guice is perfectly happy to call a package-private @Inject constructor from any package.

Charles Roth

unread,
Feb 2, 2018, 11:27:28 AM2/2/18
to google-guice
Son of a gun!   Never thought of that (Guice being able to call the constructor w/o actually having traditional Java access to it).

Thanks!

Thomas Broyer

unread,
Feb 2, 2018, 2:36:24 PM2/2/18
to google-guice
Reply all
Reply to author
Forward
0 new messages