Discussion:
Preventing others from using 'new' when object is created by Guice?
Charles Roth
2018-01-30 16:45:23 UTC
Permalink
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!
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/7365593c-2d6d-4651-80da-cd3cede48855%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tavian Barnes
2018-01-30 16:50:00 UTC
Permalink
Post by Charles Roth
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
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?
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.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/a0055909-434f-43da-bdbb-e10cd827d32f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Charles Roth
2018-02-02 16:27:28 UTC
Permalink
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!
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/e56830df-c103-4c0c-a370-0880fc53d99b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thomas Broyer
2018-02-02 19:36:23 UTC
Permalink
FYI https://github.com/google/guice/wiki/KeepConstructorsHidden
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6f335a07-2a74-49bd-a38a-f22b8a6c6c6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...