Commit Graph
27 Commits
Author SHA1 Message Date
Guice TeamandGuice Team 5caa549395 Enable the new Guice error messages.
Changes include:

 - update Guice tests that asserts error messages
 - export new errors tests
 - update Guice compiler flag so parameter names are available at runtime, which is required for the errors tests

PiperOrigin-RevId: 337602195
2020-10-16 16:55:03 -07:00
Guice TeamandGuice Team 9a8e46716e Revert previous change because it broke some tests when running with openjdk8.
PiperOrigin-RevId: 337517262
2020-10-16 09:13:55 -07:00
Guice TeamandGuice Team 2c8c7fed12 Enable the new Guice error messages.
Changes include:
- update Guice tests that asserts error messages
- export new errors tests
- update Guice compiler flag so parameter names are available at runtime, which is required for the errors tests

PiperOrigin-RevId: 337401912
2020-10-15 16:06:22 -07:00
samebandColin Decker c83404b2fb use the correct suppression name, oops. also add some other suppressions.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=312555246
2020-05-21 17:06:13 -04:00
samebandKurt Alfred Kluever 39c2ab9eda Suppress warnings for the scope/qualifier annotations intentionally used w/o RUNTIME retention.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311573006
2020-05-15 09:39:51 -04:00
higheggandRon Shapiro f8cc1718a3 Fix a subtle bug in Guice eager singleton evaluation. Add a test.
A linked binding should only be treated as an eager singleton if the target binding is eager singleton *and* the source binding is unscoped. If the source binding is scoped, it may be a scope with context constraints (such as @RequestScoped) that shouldn't be evaluated during injector creation.

Bug repro:
bind(Key1).toInstance(<something>)
bind(Key2).to(Key1).in(RequestScoped.class);

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=249417146
2019-05-27 12:17:55 -04:00
samebandRon Shapiro 37255a24d9 Refactor code to use java8-isms.
Generated with refaster, with some manual touch-ups afterwards.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=238042288
2019-04-08 09:56:03 -04:00
lukesandRon Shapiro 4c2c276cdc Remove empty overrides of AbstractModule.configure() from guice modules
In 41f307e21b this AbstractModule.configure() became non-abstract in the base class, so it is now allowed for subclasses to not override the method at all when they don’t need to access the binding dsl.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=148043119
2017-03-21 15:29:08 -04:00
lukesandRon Shapiro dcf7bdacc2 remove unnecessary explicit type parameters now that we are on java7
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=145716288
2017-03-21 15:28:23 -04:00
josephkwonandSam Berlin 11e30dc82c NO_SCOPE should be same as unscoped in IS_UNSCOPED_VISITOR
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137039636
2016-12-12 16:18:49 -05:00
lukesandSam Berlin cc0f72131a Remove the dependency on InternalContext.getDependencyChain() from SingletonScope
This method is going away and this is the only internal use of it in guice (outside of tests).  It is used to construct error messages by listing the dep chains from other
threads involved in singleton based lock cycles.

There is no direct replacement, so instead we are using a completely different
error message formatting using thread stacks.

For each thread in the cycle we will print something like:

Thread['foo',5,main] is holding locks the following singletons in the cycle:
com.google.Foo
com.google.Bar annotated with com.google.inject.name.Named(hello)
at com.google.inject.internal.SingletonScope.provision
at ....
at com.google.application.package.Foo.createInjector

and then for the thread who detected the cycle we will report the current
provisioning stack (as normal).

While this loses some detail from the other threads, it does add the full stack trace which should be useful for identifying the top level call that triggered the failure.

To do this i had to slightly modify the contract on CycleDetectingLock to make sure that the final lock in the cycle was reported

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134445852
2016-10-20 12:43:22 -04:00
lukesandSam Berlin 34e7c5d715 Run google-java-format on all Guice code.
configure a presubmit to ensure that it stays formatted.

Highlights include:
* simplified import order
* method annotations are now consistently defined on the preceding line
* javadoc reformatted to 100 chars column width

One test that contained line numbers in error messages had to be modified and
the formatter didn't like some of the more complicated preprocessor directives
(MOE and AOP).

To avoid formatting the copyright notices as javadoc i did a preprocessing step to rewrite the initial '/**' to '/*' using perl

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132718493
2016-09-13 11:47:18 -04:00
lukesandSam Berlin 713c06cce9 Fix issues reported by errorprone
99% of this is adding missing @Override annotations, in the long tail we also have

* insert missing calls to Assert.fail() in tests that are testing exception behavior
* rewrite a few cases of Foo.class.instanceof(c) to c instanceof Foo
* rewrite Class.newInstance - > Class.getConstructor().newInstance() which doesn't break checked exception checking.
* adding @javax.inject.Inject annotations to methods that override methods annotated with @javax.inject.Inject

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131839622
2016-09-07 17:45:54 -04:00
lukesandSam Berlin d82f0610c2 Fix https://github.com/google/guice/issues/1032 by making SingletonScope handle
the Injector thread local not being set.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129671032
2016-08-29 16:40:45 -04:00
mackrazandSam Berlin 113684b9d9 This CL adds @SuppressWarnings("multi-scope") to classes that are annotated with multiple scoping-annotations.
Reasoning:
I am rolling out an Annotation Processor check that fails at compile-time if you annotated a class with multiple scoping-annotations as this is not supported by Guice.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=128826347
2016-08-29 16:39:45 -04:00
timofeybandSam Berlin 5e6c93348c Implement more granular locks for a Singleton scope in Guice.
Now when you can create two independent singletons using
the same injector in different threads.
This make it easy to create scopes creating singletons using
thread pools with all the concurrency being done by Guice.
As a nice side effect Singleton scope is no longer treated
specially in Guice codebase.

The obvious problem to solve is potential deadlocks:
A requires B, B requires C, C requires A where all are
singletons and all are created simultaneously.
It's impossible to detect this deadlock using information
within one thread, so we have to have a shared storage.

An idea is to have a map of creators' locks and a map
of which threads are waiting for other singletons to be created.
Using this information circular dependencies are trivially
discovered within O(N) where N is a number of concurrent threads.

Important to not that no other deadlock scenarios within
Guice code is introduced as Guice does not expose any
other scopes that can span several threads.

Now it would be possible for
client code to deadlock on itself with two lazy singletons
calling each other's providers during creation.
This is deemed as a non-issue as it is up to the client
to write a thread-safe code.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=91610630
2015-04-21 09:37:16 -04:00
dweisandSam Berlin f11bf2d26c Make Guice ignore validation of scope annotations on abstract types for types annotated with @Component. This allows one to provide Dagger Components with Guice Injectors.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=84836495
2015-02-03 18:07:07 -05:00
timofeybandSam Berlin d7aa953d08 Making Singleton's creation lock less coarse.
Singleton is defined as a scope which creates no more than one
object per injector. It's highly confusing when you catch a deadlock
between two unrelated injectors due to the same class injection.

Problem is demonstrated using a test that recreates scenario when
one thread injecting class can block other thread to use its own
injector.

Proposed solution is to use Injector-wide locks in a singleton.
ThreadLocal as a way to store current state.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=78469951
2014-10-27 15:08:22 -04:00
Tavian Barnes d1e965c3e0 De-duplicate equivalent scope bindings. 2014-09-15 10:55:11 -04:00
Christian Edward Gruber ba5acdf7cf Allow turning off stack trace collection in Guice.
By setting guice_include_stack_traces flag OFF, Guice does not collect stack traces for identifying the declaring source of a binding. Instead it uses the first non-skipped module class name from the modules stack. As a result, in some cases, error messages can be slightly different with this flag. For example, the file name and line number are not always available.

A sample error message with this flag :

Guice creation errors:
1) Received null converting foo (bound at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest)) to java.util.Date
using CustomConverter which matches only(java.util.Date) (bound at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest)).
while locating java.util.Date annotated with @com.google.inject.TypeConversionTest()
for field at com.google.inject.TypeConversionTest.date(TypeConversionTest.java:478)
at com.google.inject.TypeConversionTest.configure(Unknown Source) (via modules: com.google.inject.TypeConversionTest -> com.google.inject.TypeConversionTest)

This also changes the DEFAULT flag name to ONLY_FOR_DECLARTION_SOURCE.
Adds InternalFlags.getIncludeStackTraceOption() to wrap system property access.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=53610378
2013-10-05 14:12:59 -07:00
Christian Edward Gruber 2e39ef748a Print out the modules that led to a binding during error reporting, but
only if there's >1 module (otherwise it's just noise).  The format is
Some normal messaging about the fact that something terrible happened
at <the normal source> (via modules: A -> B -> C)

So, for example, exposing something that isn't bound would show up as:
1) Could not expose() Foo, it must be explicitly bound.
at FooExposerModule.configure(FooExposerModule.java:211) (via modules: ParentModule -> FooExposerModule)

.. and a duplicate binding would show up as:
A binding to DuplicatedThing was already configured at ModuleA.configure(ModuleA.java:36) (via modules: ParentModule -> FooModule -> ModuleA)
at ModuleA.configure(ModuleA.java:36) (via modules: ParentModule -> BarModule -> ModuleA)

All the tests passed as-is (because this is just new information), so I made a bunch of tests stricter to explicitly look for this new information.

I also cleaned up some other errors regarding scopes & some other stuff.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=53039251
2013-10-05 14:11:20 -07:00
Christian Edward Gruber 33736ecd7a Because error-prone treats more than one scope annotation on a class as a compile-time error, existing builds that upgrade to error prone will fail. To support that, given that these are tests of the run-time version of the failure, add a supporession to the usages.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=51282197
MOE_MIGRATED_REVID=51282207
2013-09-18 15:26:44 -07:00
Sam Berlin 66094fb992 Add isEagerSingleton.
Revision created by MOE tool push_codebase.
MOE_MIGRATION=4085
2012-01-13 18:21:27 -05:00
sberlin b7a02b02d8 * Remove unused imports
* Sort imports
* Convert tabs to spaces
* Fix the ant no_aop build


Revision created by MOE tool push_codebase.
MOE_MIGRATION=2532


git-svn-id: https://google-guice.googlecode.com/svn/trunk@1572 d779f126-a31b-0410-b53b-1d3aecad763e
2011-07-08 00:34:16 +00:00
sberlin d9c913acca switch Guice from manually repackaging Guava to depending on Guava. it's still jarjar'd right now, which is causing a ~400k increase in guice-snapshot.jar. next step is to switch to ProGuard to remove the unnecessary code and cut it back down (even further?!). this will let people build from Guice source and depend directly on Guava code without having to worry about hiding internal/util.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@1558 d779f126-a31b-0410-b53b-1d3aecad763e
2011-06-26 21:02:54 +00:00
sberlin 09ab5f9e6a issue 477 - fix Scopes.isSingleton to navigate through exposed private elements.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@1313 d779f126-a31b-0410-b53b-1d3aecad763e
2010-10-25 00:41:10 +00:00
mcculls 74d714c1e7 Issue 552: (step 1) move core code to its own subdirectory
git-svn-id: https://google-guice.googlecode.com/svn/trunk@1300 d779f126-a31b-0410-b53b-1d3aecad763e
2010-10-21 19:10:16 +00:00