PageRenderTime 82ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/core/test/com/google/inject/JitBindingsTest.java

https://gitlab.com/metamorphiccode/guice
Java | 731 lines | 605 code | 67 blank | 59 comment | 12 complexity | 15d2769da5d35213478efa7cf7375e86 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject;
  17. import static com.google.common.collect.ImmutableSet.of;
  18. import static com.google.inject.Asserts.assertContains;
  19. import static com.google.inject.JitBindingsTest.GetBindingCheck.ALLOW_BINDING;
  20. import static com.google.inject.JitBindingsTest.GetBindingCheck.FAIL_ALL;
  21. import junit.framework.TestCase;
  22. import java.util.Set;
  23. /**
  24. * Some tests for {@link Binder#requireExplicitBindings()}
  25. *
  26. * @author sberlin@gmail.com (Sam Berlin)
  27. */
  28. public class JitBindingsTest extends TestCase {
  29. private String jitFailed(Class<?> clazz) {
  30. return jitFailed(TypeLiteral.get(clazz));
  31. }
  32. private String jitFailed(TypeLiteral<?> clazz) {
  33. return "Explicit bindings are required and " + clazz + " is not explicitly bound.";
  34. }
  35. private String jitInParentFailed(Class<?> clazz) {
  36. return jitInParentFailed(TypeLiteral.get(clazz));
  37. }
  38. private String jitInParentFailed(TypeLiteral<?> clazz) {
  39. return "Explicit bindings are required and " + clazz + " would be bound in a parent injector.";
  40. }
  41. private String inChildMessage(Class<?> clazz) {
  42. return "Unable to create binding for "
  43. + clazz.getName()
  44. + ". It was already configured on one or more child injectors or private modules";
  45. }
  46. public void testLinkedBindingWorks() {
  47. Injector injector = Guice.createInjector(new AbstractModule() {
  48. @Override
  49. protected void configure() {
  50. binder().requireExplicitBindings();
  51. bind(Foo.class).to(FooImpl.class);
  52. }
  53. });
  54. // Foo was explicitly bound
  55. ensureWorks(injector, Foo.class);
  56. // FooImpl was implicitly bound, it is an error to call getInstance or getProvider,
  57. // It is OK to call getBinding for introspection, but an error to get the provider
  58. // of the binding
  59. ensureFails(injector, ALLOW_BINDING, FooImpl.class);
  60. }
  61. public void testMoreBasicsWork() {
  62. Injector injector = Guice.createInjector(new AbstractModule() {
  63. @Override
  64. protected void configure() {
  65. binder().requireExplicitBindings();
  66. bind(Foo.class).to(FooImpl.class);
  67. bind(Bar.class);
  68. bind(FooBar.class);
  69. }
  70. });
  71. // Foo, Bar & FooBar was explicitly bound
  72. ensureWorks(injector, FooBar.class, Bar.class, Foo.class);
  73. // FooImpl was implicitly bound, it is an error to call getInstance or getProvider,
  74. // It is OK to call getBinding for introspection, but an error to get the provider
  75. // of the binding
  76. ensureFails(injector, ALLOW_BINDING, FooImpl.class);
  77. }
  78. public void testLinkedEagerSingleton() {
  79. Injector injector = Guice.createInjector(new AbstractModule() {
  80. @Override
  81. protected void configure() {
  82. binder().requireExplicitBindings();
  83. bind(Foo.class).to(FooImpl.class).asEagerSingleton();
  84. }
  85. });
  86. // Foo was explicitly bound
  87. ensureWorks(injector, Foo.class);
  88. // FooImpl was implicitly bound, it is an error to call getInstance or getProvider,
  89. // It is OK to call getBinding for introspection, but an error to get the provider
  90. // of the binding
  91. ensureFails(injector, ALLOW_BINDING, FooImpl.class);
  92. }
  93. public void testBasicsWithEagerSingleton() {
  94. Injector injector = Guice.createInjector(new AbstractModule() {
  95. @Override
  96. protected void configure() {
  97. binder().requireExplicitBindings();
  98. bind(Foo.class).to(FooImpl.class).asEagerSingleton();
  99. bind(Bar.class);
  100. bind(FooBar.class);
  101. }
  102. });
  103. // Foo, Bar & FooBar was explicitly bound
  104. ensureWorks(injector, FooBar.class, Bar.class, Foo.class);
  105. // FooImpl was implicitly bound, it is an error to call getInstance or getProvider,
  106. // It is OK to call getBinding for introspection, but an error to get the provider
  107. // of the binding
  108. ensureFails(injector, ALLOW_BINDING, FooImpl.class);
  109. }
  110. public void testLinkedToScoped() {
  111. Injector injector = Guice.createInjector(new AbstractModule() {
  112. @Override
  113. protected void configure() {
  114. binder.requireExplicitBindings();
  115. bind(Foo.class).to(ScopedFooImpl.class);
  116. }
  117. });
  118. // Foo was explicitly bound
  119. ensureWorks(injector, Foo.class);
  120. // FooSingletonImpl was implicitly bound, it is an error to call getInstance or getProvider,
  121. // It is OK to call getBinding for introspection, but an error to get the provider
  122. // of the binding
  123. ensureFails(injector, ALLOW_BINDING, ScopedFooImpl.class);
  124. }
  125. public void testBasicsWithScoped() {
  126. Injector injector = Guice.createInjector(new AbstractModule() {
  127. @Override
  128. protected void configure() {
  129. binder().requireExplicitBindings();
  130. bind(Foo.class).to(ScopedFooImpl.class);
  131. bind(Bar.class);
  132. bind(FooBar.class);
  133. }
  134. });
  135. // Foo, Bar & FooBar was explicitly bound
  136. ensureWorks(injector, FooBar.class, Bar.class, Foo.class);
  137. // FooSingletonImpl was implicitly bound, it is an error to call getInstance or getProvider,
  138. // It is OK to call getBinding for introspection, but an error to get the provider
  139. // of the binding
  140. ensureFails(injector, ALLOW_BINDING, ScopedFooImpl.class);
  141. }
  142. public void testFailsIfInjectingScopedDirectlyWhenItIsntBound() {
  143. try {
  144. Guice.createInjector(new AbstractModule() {
  145. @Override
  146. protected void configure() {
  147. binder().requireExplicitBindings();
  148. bind(Foo.class).to(ScopedFooImpl.class);
  149. bind(WantsScopedFooImpl.class);
  150. }
  151. });
  152. fail();
  153. } catch(CreationException expected) {
  154. assertContains(expected.getMessage(), jitFailed(ScopedFooImpl.class));
  155. assertEquals(1, expected.getErrorMessages().size());
  156. }
  157. }
  158. public void testLinkedProviderBindingWorks() {
  159. Injector injector = Guice.createInjector(new AbstractModule() {
  160. @Override
  161. protected void configure() {
  162. binder().requireExplicitBindings();
  163. bind(Foo.class).toProvider(FooProvider.class);
  164. }
  165. });
  166. // Foo was explicitly bound
  167. ensureWorks(injector, Foo.class);
  168. // FooImpl was not bound at all (even implicitly), it is an error
  169. // to call getInstance, getProvider, or getBinding.
  170. ensureFails(injector, FAIL_ALL, FooImpl.class);
  171. }
  172. public void testJitGetFails() {
  173. try {
  174. Guice.createInjector(new AbstractModule() {
  175. @Override
  176. protected void configure() {
  177. binder().requireExplicitBindings();
  178. }
  179. }).getInstance(Bar.class);
  180. fail("should have failed");
  181. } catch(ConfigurationException expected) {
  182. assertContains(expected.getMessage(), jitFailed(Bar.class));
  183. assertEquals(1, expected.getErrorMessages().size());
  184. }
  185. }
  186. public void testJitInjectionFails() {
  187. try {
  188. Guice.createInjector(new AbstractModule() {
  189. @Override
  190. protected void configure() {
  191. binder().requireExplicitBindings();
  192. bind(Foo.class).to(FooImpl.class);
  193. bind(FooBar.class);
  194. }
  195. });
  196. fail("should have failed");
  197. } catch (CreationException expected) {
  198. assertContains(expected.getMessage(), jitFailed(Bar.class));
  199. assertEquals(1, expected.getErrorMessages().size());
  200. }
  201. }
  202. public void testJitProviderGetFails() {
  203. try {
  204. Guice.createInjector(new AbstractModule() {
  205. @Override
  206. protected void configure() {
  207. binder().requireExplicitBindings();
  208. }
  209. }).getProvider(Bar.class);
  210. fail("should have failed");
  211. } catch (ConfigurationException expected) {
  212. assertContains(expected.getMessage(), jitFailed(Bar.class));
  213. assertEquals(1, expected.getErrorMessages().size());
  214. }
  215. }
  216. public void testJitProviderInjectionFails() {
  217. try {
  218. Guice.createInjector(new AbstractModule() {
  219. @Override
  220. protected void configure() {
  221. binder().requireExplicitBindings();
  222. bind(Foo.class).to(FooImpl.class);
  223. bind(ProviderFooBar.class);
  224. }
  225. });
  226. fail("should have failed");
  227. } catch (CreationException expected) {
  228. assertContains(expected.getMessage(), jitFailed(Bar.class));
  229. assertEquals(1, expected.getErrorMessages().size());
  230. }
  231. }
  232. public void testImplementedBy() {
  233. Injector injector = Guice.createInjector(new AbstractModule() {
  234. @Override
  235. protected void configure() {
  236. binder().requireExplicitBindings();
  237. bind(ImplBy.class);
  238. }
  239. });
  240. ensureWorks(injector, ImplBy.class);
  241. ensureFails(injector, ALLOW_BINDING, ImplByImpl.class);
  242. }
  243. public void testImplementedBySomethingThatIsAnnotated() {
  244. Injector injector = Guice.createInjector(new AbstractModule() {
  245. @Override
  246. protected void configure() {
  247. binder().requireExplicitBindings();
  248. bind(ImplByScoped.class);
  249. }
  250. });
  251. ensureWorks(injector, ImplByScoped.class);
  252. ensureFails(injector, ALLOW_BINDING, ImplByScopedImpl.class);
  253. }
  254. public void testProvidedBy() {
  255. Injector injector = Guice.createInjector(new AbstractModule() {
  256. @Override
  257. protected void configure() {
  258. binder().requireExplicitBindings();
  259. bind(ProvBy.class);
  260. }
  261. });
  262. ensureWorks(injector, ProvBy.class);
  263. ensureFails(injector, ALLOW_BINDING, ProvByProvider.class);
  264. }
  265. public void testProviderMethods() {
  266. Injector injector = Guice.createInjector(new AbstractModule() {
  267. @Override protected void configure() {
  268. binder().requireExplicitBindings();
  269. }
  270. @SuppressWarnings("unused") @Provides Foo foo() { return new FooImpl(); }
  271. });
  272. ensureWorks(injector, Foo.class);
  273. }
  274. public void testChildInjectorInheritsOption() {
  275. Injector parent = Guice.createInjector(new AbstractModule() {
  276. @Override
  277. protected void configure() {
  278. binder().requireExplicitBindings();
  279. bind(Bar.class);
  280. }
  281. });
  282. ensureWorks(parent, Bar.class);
  283. ensureFails(parent, FAIL_ALL, FooImpl.class, FooBar.class, Foo.class);
  284. try {
  285. parent.createChildInjector(new AbstractModule() {
  286. @Override
  287. protected void configure() {
  288. bind(FooBar.class);
  289. }
  290. });
  291. fail("should have failed");
  292. } catch(CreationException expected) {
  293. assertContains(expected.getMessage(), jitFailed(Foo.class));
  294. assertEquals(1, expected.getErrorMessages().size());
  295. }
  296. Injector child = parent.createChildInjector(new AbstractModule() {
  297. @Override
  298. protected void configure() {
  299. bind(Foo.class).to(FooImpl.class);
  300. }
  301. });
  302. ensureWorks(child, Foo.class, Bar.class);
  303. ensureFails(child, ALLOW_BINDING, FooImpl.class);
  304. ensureInChild(parent, FooImpl.class, Foo.class);
  305. // TODO(sameb): FooBar may or may not be in a child injector, depending on if GC has run.
  306. // We should fix failed child injectors to remove their contents from the parent blacklist
  307. // immediately, rather than waiting on GC to do it.
  308. // FooBar was succesfully inserted into the child injector (and parent blacklist), but then
  309. // JIT bindings it depended on failed, making the child injector invalid.
  310. Injector grandchild = child.createChildInjector(new AbstractModule() {
  311. @Override
  312. protected void configure() {
  313. bind(FooBar.class);
  314. }
  315. });
  316. ensureWorks(grandchild, FooBar.class, Foo.class, Bar.class);
  317. ensureFails(grandchild, ALLOW_BINDING, FooImpl.class);
  318. ensureFails(child, ALLOW_BINDING, FooImpl.class);
  319. ensureInChild(parent, FooImpl.class, FooBar.class, Foo.class);
  320. }
  321. public void testChildInjectorAddsOption() {
  322. Injector parent = Guice.createInjector(new AbstractModule() {
  323. @Override
  324. protected void configure() {
  325. bind(Bar.class);
  326. }
  327. });
  328. int totalParentBindings = parent.getAllBindings().size();
  329. try {
  330. parent.createChildInjector(new AbstractModule() {
  331. @Override
  332. protected void configure() {
  333. binder().requireExplicitBindings();
  334. bind(FooBar.class);
  335. }
  336. });
  337. fail("should have failed");
  338. } catch(CreationException expected) {
  339. assertContains(expected.getMessage(), jitFailed(Foo.class));
  340. assertEquals(1, expected.getErrorMessages().size());
  341. }
  342. assertEquals(totalParentBindings, parent.getAllBindings().size());
  343. Injector child = parent.createChildInjector(new AbstractModule() {
  344. @Override
  345. protected void configure() {
  346. binder().requireExplicitBindings();
  347. bind(Foo.class).to(FooImpl.class);
  348. bind(FooImpl.class);
  349. }
  350. });
  351. assertEquals(totalParentBindings, parent.getAllBindings().size());
  352. ensureWorks(child, Foo.class, Bar.class);
  353. Injector grandchild = child.createChildInjector(new AbstractModule() {
  354. @Override
  355. protected void configure() {
  356. bind(FooBar.class);
  357. }
  358. });
  359. assertEquals(totalParentBindings, parent.getAllBindings().size());
  360. ensureWorks(grandchild, FooBar.class, Foo.class, Bar.class);
  361. // Make sure siblings of children don't inherit each others settings...
  362. // a new child should be able to get FooImpl.
  363. child = parent.createChildInjector();
  364. ensureWorks(child, FooImpl.class);
  365. }
  366. public void testPrivateModulesInheritOptions() {
  367. try {
  368. Guice.createInjector(new AbstractModule() {
  369. protected void configure() {
  370. binder().requireExplicitBindings();
  371. bind(Foo.class).to(FooImpl.class);
  372. install(new PrivateModule() {
  373. public void configure() {
  374. bind(FooBar.class);
  375. expose(FooBar.class);
  376. }
  377. });
  378. }
  379. });
  380. fail("should have failed");
  381. } catch(CreationException expected) {
  382. assertContains(expected.getMessage(), jitFailed(Bar.class));
  383. assertEquals(1, expected.getErrorMessages().size());
  384. }
  385. Injector injector = Guice.createInjector(new AbstractModule() {
  386. protected void configure() {
  387. binder().requireExplicitBindings();
  388. install(new PrivateModule() {
  389. public void configure() {
  390. bind(Foo.class).to(FooImpl.class);
  391. expose(Foo.class);
  392. }
  393. });
  394. }
  395. });
  396. ensureInChild(injector, FooImpl.class);
  397. }
  398. public void testPrivateModuleAddsOption() {
  399. try {
  400. Guice.createInjector(new AbstractModule() {
  401. protected void configure() {
  402. bind(Foo.class).to(FooImpl.class);
  403. // Fails because FooBar is in the private module,
  404. // and it wants Bar, but Bar would be JIT.
  405. install(new PrivateModule() {
  406. public void configure() {
  407. binder().requireExplicitBindings();
  408. bind(FooBar.class);
  409. expose(FooBar.class);
  410. }
  411. });
  412. }
  413. });
  414. fail("should have failed");
  415. } catch(CreationException expected) {
  416. assertContains(expected.getMessage(), jitFailed(Bar.class));
  417. assertEquals(1, expected.getErrorMessages().size());
  418. }
  419. }
  420. public void testPrivateModuleSiblingsDontShareOption() {
  421. Guice.createInjector(new AbstractModule() {
  422. protected void configure() {
  423. bind(Foo.class).to(FooImpl.class);
  424. install(new PrivateModule() {
  425. public void configure() {
  426. binder().requireExplicitBindings();
  427. }
  428. });
  429. // This works, even though Bar is JIT,
  430. // because the requireExplicitBindings isn't shared
  431. // between sibling private modules.
  432. install(new PrivateModule() {
  433. public void configure() {
  434. bind(FooBar.class);
  435. expose(FooBar.class);
  436. }
  437. });
  438. }
  439. });
  440. }
  441. public void testTypeLiteralsCanBeInjected() {
  442. Injector injector = Guice.createInjector(new AbstractModule() {
  443. @Override protected void configure() {
  444. binder().requireExplicitBindings();
  445. bind(new TypeLiteral<WantsTypeLiterals<String>>() {});
  446. bind(new TypeLiteral<Set<String>>() {}).toInstance(of("bar"));
  447. }
  448. });
  449. WantsTypeLiterals<String> foo = injector.getInstance(new Key<WantsTypeLiterals<String>>() {});
  450. assertEquals(foo.literal.getRawType(), String.class);
  451. assertEquals(of("bar"), foo.set);
  452. }
  453. public void testMembersInjectorsCanBeInjected() {
  454. Injector injector = Guice.createInjector(new AbstractModule() {
  455. @Override protected void configure() {
  456. binder().requireExplicitBindings();
  457. }
  458. @Provides String data(MembersInjector<String> mi) {
  459. String data = "foo";
  460. mi.injectMembers(data);
  461. return data;
  462. }
  463. });
  464. String data = injector.getInstance(String.class);
  465. assertEquals("foo", data);
  466. }
  467. public void testJitLinkedBindingInParentFails() {
  468. try {
  469. Guice.createInjector(new AbstractModule() {
  470. @Override
  471. protected void configure() {
  472. install(new PrivateModule() {
  473. @Override
  474. protected void configure() {
  475. binder().requireExplicitBindings();
  476. bind(Foo.class).to(FooImpl.class);
  477. }
  478. });
  479. }
  480. });
  481. fail("should have failed");
  482. } catch (CreationException expected) {
  483. assertContains(expected.getMessage(), jitInParentFailed(FooImpl.class));
  484. assertEquals(1, expected.getErrorMessages().size());
  485. }
  486. }
  487. public void testJitProviderBindingInParentFails() {
  488. try {
  489. Guice.createInjector(new AbstractModule() {
  490. @Override
  491. protected void configure() {
  492. install(new PrivateModule() {
  493. @Override
  494. protected void configure() {
  495. binder().requireExplicitBindings();
  496. bind(Foo.class).toProvider(FooProvider.class);
  497. }
  498. });
  499. }
  500. });
  501. fail("should have failed");
  502. } catch (CreationException expected) {
  503. assertContains(expected.getMessage(), jitInParentFailed(FooProvider.class));
  504. assertEquals(1, expected.getErrorMessages().size());
  505. }
  506. }
  507. public void testJitImplementedByBindingInParentFails() {
  508. try {
  509. Guice.createInjector(new AbstractModule() {
  510. @Override
  511. protected void configure() {
  512. install(new PrivateModule() {
  513. @Override
  514. protected void configure() {
  515. binder().requireExplicitBindings();
  516. bind(ImplBy.class);
  517. }
  518. });
  519. }
  520. });
  521. fail("should have failed");
  522. } catch (CreationException expected) {
  523. assertContains(expected.getMessage(), jitInParentFailed(ImplByImpl.class));
  524. assertEquals(1, expected.getErrorMessages().size());
  525. }
  526. }
  527. public void testJitProvidedByBindingInParentFails() {
  528. try {
  529. Guice.createInjector(new AbstractModule() {
  530. @Override
  531. protected void configure() {
  532. install(new PrivateModule() {
  533. @Override
  534. protected void configure() {
  535. binder().requireExplicitBindings();
  536. bind(ProvBy.class);
  537. }
  538. });
  539. }
  540. });
  541. fail("should have failed");
  542. } catch (CreationException expected) {
  543. assertContains(expected.getMessage(), jitInParentFailed(ProvByProvider.class));
  544. assertEquals(1, expected.getErrorMessages().size());
  545. }
  546. }
  547. private void ensureWorks(Injector injector, Class<?>... classes) {
  548. for(int i = 0; i < classes.length; i++) {
  549. injector.getInstance(classes[i]);
  550. injector.getProvider(classes[i]).get();
  551. injector.getBinding(classes[i]).getProvider().get();
  552. }
  553. }
  554. enum GetBindingCheck { FAIL_ALL, ALLOW_BINDING, ALLOW_BINDING_PROVIDER }
  555. private void ensureFails(Injector injector, GetBindingCheck getBinding, Class<?>... classes) {
  556. for(int i = 0; i < classes.length; i++) {
  557. try {
  558. injector.getInstance(classes[i]);
  559. fail("should have failed tring to retrieve class: " + classes[i]);
  560. } catch(ConfigurationException expected) {
  561. assertContains(expected.getMessage(), jitFailed(classes[i]));
  562. assertEquals(1, expected.getErrorMessages().size());
  563. }
  564. try {
  565. injector.getProvider(classes[i]);
  566. fail("should have failed tring to retrieve class: " + classes[i]);
  567. } catch(ConfigurationException expected) {
  568. assertContains(expected.getMessage(), jitFailed(classes[i]));
  569. assertEquals(1, expected.getErrorMessages().size());
  570. }
  571. if (getBinding == GetBindingCheck.ALLOW_BINDING
  572. || getBinding == GetBindingCheck.ALLOW_BINDING_PROVIDER) {
  573. Binding<?> binding = injector.getBinding(classes[i]);
  574. try {
  575. binding.getProvider();
  576. if (getBinding != GetBindingCheck.ALLOW_BINDING_PROVIDER) {
  577. fail("should have failed trying to retrieve class: " + classes[i]);
  578. }
  579. } catch(ConfigurationException expected) {
  580. if (getBinding == GetBindingCheck.ALLOW_BINDING_PROVIDER) {
  581. throw expected;
  582. }
  583. assertContains(expected.getMessage(), jitFailed(classes[i]));
  584. assertEquals(1, expected.getErrorMessages().size());
  585. }
  586. } else {
  587. try {
  588. injector.getBinding(classes[i]);
  589. fail("should have failed tring to retrieve class: " + classes[i]);
  590. } catch(ConfigurationException expected) {
  591. assertContains(expected.getMessage(), jitFailed(classes[i]));
  592. assertEquals(1, expected.getErrorMessages().size());
  593. }
  594. }
  595. }
  596. }
  597. private void ensureInChild(Injector injector, Class<?>... classes) {
  598. for(int i = 0; i < classes.length; i++) {
  599. try {
  600. injector.getInstance(classes[i]);
  601. fail("should have failed tring to retrieve class: " + classes[i]);
  602. } catch(ConfigurationException expected) {
  603. assertContains(expected.getMessage(), inChildMessage(classes[i]));
  604. assertEquals(1, expected.getErrorMessages().size());
  605. }
  606. try {
  607. injector.getProvider(classes[i]);
  608. fail("should have failed tring to retrieve class: " + classes[i]);
  609. } catch(ConfigurationException expected) {
  610. assertContains(expected.getMessage(), inChildMessage(classes[i]));
  611. assertEquals(1, expected.getErrorMessages().size());
  612. }
  613. try {
  614. injector.getBinding(classes[i]);
  615. fail("should have failed tring to retrieve class: " + classes[i]);
  616. } catch(ConfigurationException expected) {
  617. assertContains(expected.getMessage(), inChildMessage(classes[i]));
  618. assertEquals(1, expected.getErrorMessages().size());
  619. }
  620. }
  621. }
  622. private static interface Foo {}
  623. private static class FooImpl implements Foo {}
  624. @Singleton private static class ScopedFooImpl implements Foo {}
  625. private static class WantsScopedFooImpl {
  626. @SuppressWarnings("unused") @Inject ScopedFooImpl scopedFoo;
  627. }
  628. private static class Bar {}
  629. private static class FooBar {
  630. @SuppressWarnings("unused") @Inject Foo foo;
  631. @SuppressWarnings("unused") @Inject Bar bar;
  632. }
  633. private static class ProviderFooBar {
  634. @SuppressWarnings("unused") @Inject Provider<Foo> foo;
  635. @SuppressWarnings("unused") @Inject Provider<Bar> bar;
  636. }
  637. private static class FooProvider implements Provider<Foo> {
  638. public Foo get() {
  639. return new FooImpl();
  640. }
  641. }
  642. @ImplementedBy(ImplByImpl.class)
  643. private static interface ImplBy {}
  644. private static class ImplByImpl implements ImplBy {}
  645. @ImplementedBy(ImplByScopedImpl.class)
  646. private static interface ImplByScoped {}
  647. @Singleton
  648. private static class ImplByScopedImpl implements ImplByScoped {}
  649. @ProvidedBy(ProvByProvider.class)
  650. private static interface ProvBy {}
  651. private static class ProvByProvider implements Provider<ProvBy> {
  652. public ProvBy get() {
  653. return new ProvBy() {};
  654. }
  655. }
  656. private static class WantsTypeLiterals<T> {
  657. TypeLiteral<T> literal;
  658. Set<T> set;
  659. @Inject WantsTypeLiterals(TypeLiteral<T> literal, Set<T> set) {
  660. this.literal = literal;
  661. this.set = set;
  662. }
  663. }
  664. }