PageRenderTime 182ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/assistedinject/test/com/google/inject/assistedinject/FactoryProvider2Test.java

https://gitlab.com/metamorphiccode/guice
Java | 1220 lines | 1011 code | 183 blank | 26 comment | 0 complexity | a85bcbba75742ccbf8dfcb12cf99fbaa MD5 | raw file
  1. /**
  2. * Copyright (C) 2008 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.assistedinject;
  17. import static com.google.inject.Asserts.assertContains;
  18. import static com.google.inject.Asserts.assertEqualsBothWays;
  19. import com.google.inject.AbstractModule;
  20. import com.google.inject.ConfigurationException;
  21. import com.google.inject.CreationException;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Inject;
  24. import com.google.inject.Injector;
  25. import com.google.inject.Key;
  26. import com.google.inject.Provider;
  27. import com.google.inject.Stage;
  28. import com.google.inject.TypeLiteral;
  29. import com.google.inject.assistedinject.FactoryProvider2Test.Equals.ComparisonMethod;
  30. import com.google.inject.assistedinject.FactoryProvider2Test.Equals.Impl;
  31. import com.google.inject.matcher.Matchers;
  32. import com.google.inject.name.Named;
  33. import com.google.inject.name.Names;
  34. import junit.framework.TestCase;
  35. import java.util.Collection;
  36. import java.util.Collections;
  37. import java.util.Set;
  38. import java.util.concurrent.atomic.AtomicInteger;
  39. @SuppressWarnings("deprecation")
  40. public class FactoryProvider2Test extends TestCase {
  41. private enum Color { BLUE, GREEN, RED, GRAY, BLACK, ORANGE, PINK }
  42. public void testAssistedFactory() {
  43. Injector injector = Guice.createInjector(new AbstractModule() {
  44. @Override
  45. protected void configure() {
  46. bind(Double.class).toInstance(5.0d);
  47. bind(ColoredCarFactory.class).toProvider(
  48. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  49. }
  50. });
  51. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  52. Mustang blueMustang = (Mustang) carFactory.create(Color.BLUE);
  53. assertEquals(Color.BLUE, blueMustang.color);
  54. assertEquals(5.0d, blueMustang.engineSize);
  55. Mustang redMustang = (Mustang) carFactory.create(Color.RED);
  56. assertEquals(Color.RED, redMustang.color);
  57. assertEquals(5.0d, redMustang.engineSize);
  58. }
  59. public void testAssistedFactoryWithAnnotations() {
  60. Injector injector = Guice.createInjector(new AbstractModule() {
  61. @Override
  62. protected void configure() {
  63. bind(int.class).annotatedWith(Names.named("horsePower")).toInstance(250);
  64. bind(int.class).annotatedWith(Names.named("modelYear")).toInstance(1984);
  65. bind(ColoredCarFactory.class).toProvider(
  66. FactoryProvider.newFactory(ColoredCarFactory.class, Camaro.class));
  67. }
  68. });
  69. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  70. Camaro blueCamaro = (Camaro) carFactory.create(Color.BLUE);
  71. assertEquals(Color.BLUE, blueCamaro.color);
  72. assertEquals(1984, blueCamaro.modelYear);
  73. assertEquals(250, blueCamaro.horsePower);
  74. Camaro redCamaro = (Camaro) carFactory.create(Color.RED);
  75. assertEquals(Color.RED, redCamaro.color);
  76. assertEquals(1984, redCamaro.modelYear);
  77. assertEquals(250, redCamaro.horsePower);
  78. }
  79. public interface Car {}
  80. interface ColoredCarFactory {
  81. Car create(Color color);
  82. }
  83. public static class Mustang implements Car {
  84. private final double engineSize;
  85. private final Color color;
  86. @Inject
  87. public Mustang(double engineSize, @Assisted Color color) {
  88. this.engineSize = engineSize;
  89. this.color = color;
  90. }
  91. public void drive() {}
  92. }
  93. public static class Camaro implements Car {
  94. private final int horsePower;
  95. private final int modelYear;
  96. private final Color color;
  97. @Inject
  98. public Camaro(
  99. @Named("horsePower") int horsePower,
  100. @Named("modelYear") int modelYear,
  101. @Assisted Color color) {
  102. this.horsePower = horsePower;
  103. this.modelYear = modelYear;
  104. this.color = color;
  105. }
  106. }
  107. interface SummerCarFactory {
  108. Car create(Color color, boolean convertable);
  109. }
  110. public void testFactoryUsesInjectedConstructor() {
  111. Injector injector = Guice.createInjector(new AbstractModule() {
  112. @Override
  113. protected void configure() {
  114. bind(float.class).toInstance(140f);
  115. bind(SummerCarFactory.class).toProvider(
  116. FactoryProvider.newFactory(SummerCarFactory.class, Corvette.class));
  117. }
  118. });
  119. SummerCarFactory carFactory = injector.getInstance(SummerCarFactory.class);
  120. Corvette redCorvette = (Corvette) carFactory.create(Color.RED, false);
  121. assertEquals(Color.RED, redCorvette.color);
  122. assertEquals(140f, redCorvette.maxMph);
  123. assertFalse(redCorvette.isConvertable);
  124. }
  125. public static class Corvette implements Car {
  126. private boolean isConvertable;
  127. private Color color;
  128. private float maxMph;
  129. @SuppressWarnings("unused")
  130. public Corvette(Color color, boolean isConvertable) {
  131. throw new IllegalStateException("Not an @AssistedInject constructor");
  132. }
  133. @Inject
  134. public Corvette(@Assisted Color color, Float maxMph, @Assisted boolean isConvertable) {
  135. this.isConvertable = isConvertable;
  136. this.color = color;
  137. this.maxMph = maxMph;
  138. }
  139. }
  140. public void testConstructorDoesntNeedAllFactoryMethodArguments() {
  141. Injector injector = Guice.createInjector(new AbstractModule() {
  142. @Override
  143. protected void configure() {
  144. bind(SummerCarFactory.class).toProvider(
  145. FactoryProvider.newFactory(SummerCarFactory.class, Beetle.class));
  146. }
  147. });
  148. SummerCarFactory factory = injector.getInstance(SummerCarFactory.class);
  149. Beetle beetle = (Beetle) factory.create(Color.RED, true);
  150. assertSame(Color.RED, beetle.color);
  151. }
  152. public static class Beetle implements Car {
  153. private final Color color;
  154. @Inject
  155. public Beetle(@Assisted Color color) {
  156. this.color = color;
  157. }
  158. }
  159. public void testMethodsAndFieldsGetInjected() {
  160. Injector injector = Guice.createInjector(new AbstractModule() {
  161. @Override
  162. protected void configure() {
  163. bind(String.class).toInstance("turbo");
  164. bind(int.class).toInstance(911);
  165. bind(double.class).toInstance(50000d);
  166. bind(ColoredCarFactory.class).toProvider(
  167. FactoryProvider.newFactory(ColoredCarFactory.class, Porsche.class));
  168. }
  169. });
  170. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  171. Porsche grayPorsche = (Porsche) carFactory.create(Color.GRAY);
  172. assertEquals(Color.GRAY, grayPorsche.color);
  173. assertEquals(50000d, grayPorsche.price);
  174. assertEquals(911, grayPorsche.model);
  175. assertEquals("turbo", grayPorsche.name);
  176. }
  177. public static class Porsche implements Car {
  178. private final Color color;
  179. private final double price;
  180. private @Inject String name;
  181. private int model;
  182. @Inject
  183. public Porsche(@Assisted Color color, double price) {
  184. this.color = color;
  185. this.price = price;
  186. }
  187. @Inject void setModel(int model) {
  188. this.model = model;
  189. }
  190. }
  191. public void testProviderInjection() {
  192. Injector injector = Guice.createInjector(new AbstractModule() {
  193. @Override
  194. protected void configure() {
  195. bind(String.class).toInstance("trans am");
  196. bind(ColoredCarFactory.class).toProvider(
  197. FactoryProvider.newFactory(ColoredCarFactory.class, Firebird.class));
  198. }
  199. });
  200. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  201. Firebird blackFirebird = (Firebird) carFactory.create(Color.BLACK);
  202. assertEquals(Color.BLACK, blackFirebird.color);
  203. assertEquals("trans am", blackFirebird.modifiersProvider.get());
  204. }
  205. public static class Firebird implements Car {
  206. private final Provider<String> modifiersProvider;
  207. private final Color color;
  208. @Inject
  209. public Firebird(Provider<String> modifiersProvider, @Assisted Color color) {
  210. this.modifiersProvider = modifiersProvider;
  211. this.color = color;
  212. }
  213. }
  214. public void testAssistedProviderInjection() {
  215. Injector injector = Guice.createInjector(new AbstractModule() {
  216. @Override
  217. protected void configure() {
  218. bind(String.class).toInstance("trans am");
  219. bind(ColoredCarFactory.class).toProvider(
  220. FactoryProvider.newFactory(ColoredCarFactory.class, Flamingbird.class));
  221. }
  222. });
  223. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  224. Flamingbird flamingbird = (Flamingbird) carFactory.create(Color.BLACK);
  225. assertEquals(Color.BLACK, flamingbird.colorProvider.get());
  226. assertEquals("trans am", flamingbird.modifiersProvider.get());
  227. Flamingbird flamingbird2 = (Flamingbird) carFactory.create(Color.RED);
  228. assertEquals(Color.RED, flamingbird2.colorProvider.get());
  229. assertEquals("trans am", flamingbird2.modifiersProvider.get());
  230. // Make sure the original flamingbird is black still.
  231. assertEquals(Color.BLACK, flamingbird.colorProvider.get());
  232. }
  233. public static class Flamingbird implements Car {
  234. private final Provider<String> modifiersProvider;
  235. private final Provider<Color> colorProvider;
  236. @Inject
  237. public Flamingbird(Provider<String> modifiersProvider, @Assisted Provider<Color> colorProvider) {
  238. this.modifiersProvider = modifiersProvider;
  239. this.colorProvider = colorProvider;
  240. }
  241. }
  242. public void testTypeTokenInjection() {
  243. Injector injector = Guice.createInjector(new AbstractModule() {
  244. @Override
  245. protected void configure() {
  246. bind(new TypeLiteral<Set<String>>() {}).toInstance(Collections.singleton("Flux Capacitor"));
  247. bind(new TypeLiteral<Set<Integer>>() {}).toInstance(Collections.singleton(88));
  248. bind(ColoredCarFactory.class).toProvider(
  249. FactoryProvider.newFactory(ColoredCarFactory.class, DeLorean.class));
  250. }
  251. });
  252. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  253. DeLorean deLorean = (DeLorean) carFactory.create(Color.GRAY);
  254. assertEquals(Color.GRAY, deLorean.color);
  255. assertEquals("Flux Capacitor", deLorean.features.iterator().next());
  256. assertEquals(new Integer(88), deLorean.featureActivationSpeeds.iterator().next());
  257. }
  258. public static class DeLorean implements Car {
  259. private final Set<String> features;
  260. private final Set<Integer> featureActivationSpeeds;
  261. private final Color color;
  262. @Inject
  263. public DeLorean(
  264. Set<String> extraFeatures, Set<Integer> featureActivationSpeeds, @Assisted Color color) {
  265. this.features = extraFeatures;
  266. this.featureActivationSpeeds = featureActivationSpeeds;
  267. this.color = color;
  268. }
  269. }
  270. public void testTypeTokenProviderInjection() {
  271. Injector injector = Guice.createInjector(new AbstractModule() {
  272. @Override
  273. protected void configure() {
  274. bind(new TypeLiteral<Set<String>>() { }).toInstance(Collections.singleton("Datsun"));
  275. bind(ColoredCarFactory.class).toProvider(
  276. FactoryProvider.newFactory(ColoredCarFactory.class, Z.class));
  277. }
  278. });
  279. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  280. Z orangeZ = (Z) carFactory.create(Color.ORANGE);
  281. assertEquals(Color.ORANGE, orangeZ.color);
  282. assertEquals("Datsun", orangeZ.manufacturersProvider.get().iterator().next());
  283. }
  284. public static class Z implements Car {
  285. private final Provider<Set<String>> manufacturersProvider;
  286. private final Color color;
  287. @Inject
  288. public Z(Provider<Set<String>> manufacturersProvider, @Assisted Color color) {
  289. this.manufacturersProvider = manufacturersProvider;
  290. this.color = color;
  291. }
  292. }
  293. public static class Prius implements Car {
  294. final Color color;
  295. @Inject
  296. private Prius(@Assisted Color color) {
  297. this.color = color;
  298. }
  299. }
  300. public void testAssistInjectionInNonPublicConstructor() {
  301. Injector injector = Guice.createInjector(new AbstractModule() {
  302. @Override
  303. protected void configure() {
  304. bind(ColoredCarFactory.class).toProvider(
  305. FactoryProvider.newFactory(ColoredCarFactory.class, Prius.class));
  306. }
  307. });
  308. Prius prius = (Prius) injector.getInstance(ColoredCarFactory.class).create(Color.ORANGE);
  309. assertEquals(prius.color, Color.ORANGE);
  310. }
  311. public static class ExplodingCar implements Car {
  312. @Inject
  313. public ExplodingCar(@SuppressWarnings("unused") @Assisted Color color) {
  314. throw new IllegalStateException("kaboom!");
  315. }
  316. }
  317. public void testExceptionDuringConstruction() {
  318. Injector injector = Guice.createInjector(new AbstractModule() {
  319. @Override
  320. protected void configure() {
  321. bind(ColoredCarFactory.class).toProvider(
  322. FactoryProvider.newFactory(ColoredCarFactory.class, ExplodingCar.class));
  323. }
  324. });
  325. try {
  326. injector.getInstance(ColoredCarFactory.class).create(Color.ORANGE);
  327. fail();
  328. } catch (IllegalStateException e) {
  329. assertEquals("kaboom!", e.getMessage());
  330. }
  331. }
  332. public static class DefectiveCar implements Car {
  333. @Inject
  334. public DefectiveCar() throws ExplosionException {
  335. throw new ExplosionException();
  336. }
  337. }
  338. public static class ExplosionException extends Exception { }
  339. public static class FireException extends Exception { }
  340. public interface DefectiveCarFactoryWithNoExceptions {
  341. Car createCar();
  342. }
  343. public interface DefectiveCarFactory {
  344. Car createCar() throws FireException;
  345. }
  346. public interface CorrectDefectiveCarFactory {
  347. Car createCar() throws FireException, ExplosionException;
  348. }
  349. public void testConstructorExceptionsAreThrownByFactory() {
  350. Injector injector = Guice.createInjector(new AbstractModule() {
  351. @Override
  352. protected void configure() {
  353. bind(CorrectDefectiveCarFactory.class).toProvider(
  354. FactoryProvider.newFactory(CorrectDefectiveCarFactory.class, DefectiveCar.class));
  355. }
  356. });
  357. try {
  358. injector.getInstance(CorrectDefectiveCarFactory.class).createCar();
  359. fail();
  360. } catch (FireException e) {
  361. fail();
  362. } catch (ExplosionException expected) {
  363. }
  364. }
  365. public static class WildcardCollection {
  366. public interface Factory {
  367. WildcardCollection create(Collection<?> items);
  368. }
  369. @Inject
  370. public WildcardCollection(@SuppressWarnings("unused") @Assisted Collection<?> items) { }
  371. }
  372. public void testWildcardGenerics() {
  373. Injector injector = Guice.createInjector(new AbstractModule() {
  374. @Override
  375. protected void configure() {
  376. bind(WildcardCollection.Factory.class).toProvider(
  377. FactoryProvider.newFactory(WildcardCollection.Factory.class, WildcardCollection.class));
  378. }
  379. });
  380. WildcardCollection.Factory factory = injector.getInstance(WildcardCollection.Factory.class);
  381. factory.create(Collections.emptyList());
  382. }
  383. public static class SteeringWheel {}
  384. public static class Fiat implements Car {
  385. private final SteeringWheel steeringWheel;
  386. private final Color color;
  387. @Inject
  388. public Fiat(SteeringWheel steeringWheel, @Assisted Color color) {
  389. this.steeringWheel = steeringWheel;
  390. this.color = color;
  391. }
  392. }
  393. public void testFactoryWithImplicitBindings() {
  394. Injector injector = Guice.createInjector(new AbstractModule() {
  395. @Override
  396. protected void configure() {
  397. bind(ColoredCarFactory.class).toProvider(
  398. FactoryProvider.newFactory(ColoredCarFactory.class, Fiat.class));
  399. }
  400. });
  401. ColoredCarFactory coloredCarFactory = injector.getInstance(ColoredCarFactory.class);
  402. Fiat fiat = (Fiat) coloredCarFactory.create(Color.GREEN);
  403. assertEquals(Color.GREEN, fiat.color);
  404. assertNotNull(fiat.steeringWheel);
  405. }
  406. public void testFactoryFailsWithMissingBinding() {
  407. try {
  408. Guice.createInjector(new AbstractModule() {
  409. @Override protected void configure() {
  410. bind(ColoredCarFactory.class).toProvider(
  411. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  412. }
  413. });
  414. fail();
  415. } catch (CreationException expected) {
  416. assertContains(expected.getMessage(),
  417. "Could not find a suitable constructor in java.lang.Double.",
  418. "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java");
  419. }
  420. }
  421. public void testFactoryFailsWithMissingBindingInToolStage() {
  422. try {
  423. Guice.createInjector(Stage.TOOL, new AbstractModule() {
  424. @Override protected void configure() {
  425. bind(ColoredCarFactory.class).toProvider(
  426. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  427. }
  428. });
  429. fail();
  430. } catch (CreationException expected) {
  431. assertContains(expected.getMessage(),
  432. "Could not find a suitable constructor in java.lang.Double.",
  433. "at " + ColoredCarFactory.class.getName() + ".create(FactoryProvider2Test.java");
  434. }
  435. }
  436. public void testMethodsDeclaredInObject() {
  437. Injector injector = Guice.createInjector(new AbstractModule() {
  438. @Override protected void configure() {
  439. bind(Double.class).toInstance(5.0d);
  440. bind(ColoredCarFactory.class).toProvider(
  441. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  442. }
  443. });
  444. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  445. assertEqualsBothWays(carFactory, carFactory);
  446. }
  447. static class Subaru implements Car {
  448. @Inject @Assisted Provider<Color> colorProvider;
  449. }
  450. public void testInjectingProviderOfParameter() {
  451. Injector injector = Guice.createInjector(new AbstractModule() {
  452. @Override protected void configure() {
  453. bind(ColoredCarFactory.class).toProvider(
  454. FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class));
  455. }
  456. });
  457. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  458. Subaru subaru = (Subaru) carFactory.create(Color.RED);
  459. assertSame(Color.RED, subaru.colorProvider.get());
  460. assertSame(Color.RED, subaru.colorProvider.get());
  461. Subaru sedan = (Subaru) carFactory.create(Color.BLUE);
  462. assertSame(Color.BLUE, sedan.colorProvider.get());
  463. assertSame(Color.BLUE, sedan.colorProvider.get());
  464. // and make sure the subaru is still red
  465. assertSame(Color.RED, subaru.colorProvider.get());
  466. }
  467. public void testInjectingNullParameter() {
  468. Injector injector = Guice.createInjector(new AbstractModule() {
  469. @Override protected void configure() {
  470. bind(ColoredCarFactory.class).toProvider(
  471. FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class));
  472. }
  473. });
  474. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  475. Subaru subaru = (Subaru) carFactory.create(null);
  476. assertNull(subaru.colorProvider.get());
  477. assertNull(subaru.colorProvider.get());
  478. }
  479. interface ProviderBasedColoredCarFactory {
  480. Car createCar(Provider<Color> colorProvider, Provider<String> stringProvider);
  481. Mustang createMustang(@Assisted("color") Provider<Color> colorProvider);
  482. }
  483. public void testAssistedProviderIsDisallowed() {
  484. try {
  485. Guice.createInjector(new AbstractModule() {
  486. @Override protected void configure() {
  487. bind(ProviderBasedColoredCarFactory.class).toProvider(
  488. FactoryProvider.newFactory(ProviderBasedColoredCarFactory.class, Subaru.class));
  489. }
  490. });
  491. fail();
  492. } catch (CreationException expected) {
  493. assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
  494. // Assert each method individually, because JDK7 doesn't guarantee method ordering.
  495. assertContains(expected.getMessage(),
  496. ") A Provider may not be a type in a factory method of an AssistedInject."
  497. + "\n Offending instance is parameter [1] with key"
  498. + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method ["
  499. + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
  500. assertContains(expected.getMessage(),
  501. ") A Provider may not be a type in a factory method of an AssistedInject."
  502. + "\n Offending instance is parameter [2] with key"
  503. + " [com.google.inject.Provider<java.lang.String>] on method ["
  504. + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
  505. assertContains(expected.getMessage(),
  506. ") A Provider may not be a type in a factory method of an AssistedInject."
  507. + "\n Offending instance is parameter [1] with key"
  508. + " [com.google.inject.Provider<" + Color.class.getName() + ">"
  509. + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]"
  510. + " on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createMustang()]"
  511. );
  512. assertContains(expected.getMessage(),
  513. ") No implementation for com.google.inject.assistedinject."
  514. + "FactoryProvider2Test$ProviderBasedColoredCarFactory was bound.");
  515. }
  516. }
  517. interface JavaxProviderBasedColoredCarFactory {
  518. Car createCar(javax.inject.Provider<Color> colorProvider, javax.inject.Provider<String> stringProvider);
  519. Mustang createMustang(@Assisted("color") javax.inject.Provider<Color> colorProvider);
  520. }
  521. public void testAssistedJavaxProviderIsDisallowed() {
  522. try {
  523. Guice.createInjector(new AbstractModule() {
  524. @Override protected void configure() {
  525. bind(JavaxProviderBasedColoredCarFactory.class).toProvider(
  526. FactoryProvider.newFactory(JavaxProviderBasedColoredCarFactory.class, Subaru.class));
  527. }
  528. });
  529. fail();
  530. } catch (CreationException expected) {
  531. assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
  532. assertContains(expected.getMessage(),
  533. ") A Provider may not be a type in a factory method of an AssistedInject."
  534. + "\n Offending instance is parameter [1] with key"
  535. + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method ["
  536. + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
  537. assertContains(expected.getMessage(),
  538. ") A Provider may not be a type in a factory method of an AssistedInject."
  539. + "\n Offending instance is parameter [2] with key"
  540. + " [com.google.inject.Provider<java.lang.String>] on method ["
  541. + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
  542. assertContains(expected.getMessage(),
  543. ") A Provider may not be a type in a factory method of an AssistedInject."
  544. + "\n Offending instance is parameter [1] with key"
  545. + " [com.google.inject.Provider<" + Color.class.getName() + ">"
  546. + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]"
  547. + " on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createMustang()]"
  548. );
  549. assertContains(expected.getMessage(),
  550. ") No implementation for com.google.inject.assistedinject."
  551. + "FactoryProvider2Test$JavaxProviderBasedColoredCarFactory was bound.");
  552. }
  553. }
  554. public void testFactoryUseBeforeInitialization() {
  555. ColoredCarFactory carFactory = FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class)
  556. .get();
  557. try {
  558. carFactory.create(Color.RED);
  559. fail();
  560. } catch (IllegalStateException expected) {
  561. assertContains(expected.getMessage(),
  562. "Factories.create() factories cannot be used until they're initialized by Guice.");
  563. }
  564. }
  565. interface MustangFactory {
  566. Mustang create(Color color);
  567. }
  568. public void testFactoryBuildingConcreteTypes() {
  569. Injector injector = Guice.createInjector(new AbstractModule() {
  570. @Override
  571. protected void configure() {
  572. bind(double.class).toInstance(5.0d);
  573. // note there is no 'thatMakes()' call here:
  574. bind(MustangFactory.class).toProvider(
  575. FactoryProvider.newFactory(MustangFactory.class, Mustang.class));
  576. }
  577. });
  578. MustangFactory factory = injector.getInstance(MustangFactory.class);
  579. Mustang mustang = factory.create(Color.RED);
  580. assertSame(Color.RED, mustang.color);
  581. assertEquals(5.0d, mustang.engineSize);
  582. }
  583. static class Fleet {
  584. @Inject Mustang mustang;
  585. @Inject Camaro camaro;
  586. }
  587. interface FleetFactory {
  588. Fleet createFleet(Color color);
  589. }
  590. public void testInjectDeepIntoConstructedObjects() {
  591. Injector injector = Guice.createInjector(new AbstractModule() {
  592. @Override
  593. protected void configure() {
  594. bind(double.class).toInstance(5.0d);
  595. bind(int.class).annotatedWith(Names.named("horsePower")).toInstance(250);
  596. bind(int.class).annotatedWith(Names.named("modelYear")).toInstance(1984);
  597. bind(FleetFactory.class).toProvider(FactoryProvider.newFactory(FleetFactory.class,
  598. Fleet.class));
  599. }
  600. });
  601. FleetFactory fleetFactory = injector.getInstance(FleetFactory.class);
  602. Fleet fleet = fleetFactory.createFleet(Color.RED);
  603. assertSame(Color.RED, fleet.mustang.color);
  604. assertEquals(5.0d, fleet.mustang.engineSize);
  605. assertSame(Color.RED, fleet.camaro.color);
  606. assertEquals(250, fleet.camaro.horsePower);
  607. assertEquals(1984, fleet.camaro.modelYear);
  608. }
  609. interface TwoToneCarFactory {
  610. Car create(@Assisted("paint") Color paint, @Assisted("fabric") Color fabric);
  611. }
  612. static class Maxima implements Car {
  613. @Inject @Assisted("paint") Color paint;
  614. @Inject @Assisted("fabric") Color fabric;
  615. }
  616. public void testDistinctKeys() {
  617. Injector injector = Guice.createInjector(new AbstractModule() {
  618. @Override
  619. protected void configure() {
  620. bind(TwoToneCarFactory.class).toProvider(
  621. FactoryProvider.newFactory(TwoToneCarFactory.class, Maxima.class));
  622. }
  623. });
  624. TwoToneCarFactory factory = injector.getInstance(TwoToneCarFactory.class);
  625. Maxima maxima = (Maxima) factory.create(Color.BLACK, Color.GRAY);
  626. assertSame(Color.BLACK, maxima.paint);
  627. assertSame(Color.GRAY, maxima.fabric);
  628. }
  629. interface DoubleToneCarFactory {
  630. Car create(@Assisted("paint") Color paint, @Assisted("paint") Color morePaint);
  631. }
  632. public void testDuplicateKeys() {
  633. try {
  634. Guice.createInjector(new AbstractModule() {
  635. @Override protected void configure() {
  636. bind(DoubleToneCarFactory.class).toProvider(
  637. FactoryProvider.newFactory(DoubleToneCarFactory.class, Maxima.class));
  638. }
  639. });
  640. fail();
  641. } catch (CreationException expected) {
  642. assertContains(expected.getMessage(), "A binding to " + Color.class.getName() + " annotated with @"
  643. + Assisted.class.getName() + "(value=paint) was already configured at");
  644. }
  645. }
  646. /*if[AOP]*/
  647. public void testMethodInterceptorsOnAssistedTypes() {
  648. final AtomicInteger invocationCount = new AtomicInteger();
  649. final org.aopalliance.intercept.MethodInterceptor interceptor
  650. = new org.aopalliance.intercept.MethodInterceptor() {
  651. public Object invoke(org.aopalliance.intercept.MethodInvocation methodInvocation)
  652. throws Throwable {
  653. invocationCount.incrementAndGet();
  654. return methodInvocation.proceed();
  655. }
  656. };
  657. Injector injector = Guice.createInjector(new AbstractModule() {
  658. @Override
  659. protected void configure() {
  660. bindInterceptor(Matchers.any(), Matchers.any(), interceptor);
  661. bind(Double.class).toInstance(5.0d);
  662. bind(ColoredCarFactory.class).toProvider(
  663. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  664. }
  665. });
  666. ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
  667. Mustang mustang = (Mustang) factory.create(Color.GREEN);
  668. assertEquals(0, invocationCount.get());
  669. mustang.drive();
  670. assertEquals(1, invocationCount.get());
  671. }
  672. /*end[AOP]*/
  673. /**
  674. * Our factories aren't reusable across injectors. Although this behaviour isn't something we
  675. * like, I have a test case to make sure the error message is pretty.
  676. */
  677. public void testFactoryReuseErrorMessageIsPretty() {
  678. final Provider<ColoredCarFactory> factoryProvider
  679. = FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class);
  680. Guice.createInjector(new AbstractModule() {
  681. @Override protected void configure() {
  682. bind(Double.class).toInstance(5.0d);
  683. bind(ColoredCarFactory.class).toProvider(factoryProvider);
  684. }
  685. });
  686. try {
  687. Guice.createInjector(new AbstractModule() {
  688. @Override protected void configure() {
  689. bind(Double.class).toInstance(5.0d);
  690. bind(ColoredCarFactory.class).toProvider(factoryProvider);
  691. }
  692. });
  693. fail();
  694. } catch(CreationException expected) {
  695. assertContains(expected.getMessage(),
  696. "Factories.create() factories may only be used in one Injector!");
  697. }
  698. }
  699. public void testNonAssistedFactoryMethodParameter() {
  700. try {
  701. FactoryProvider.newFactory(NamedParameterFactory.class, Mustang.class);
  702. fail();
  703. } catch(ConfigurationException expected) {
  704. assertContains(expected.getMessage(),
  705. "Only @Assisted is allowed for factory parameters, but found @" + Named.class.getName());
  706. }
  707. }
  708. interface NamedParameterFactory {
  709. Car create(@Named("seats") int seats, double engineSize);
  710. }
  711. public void testDefaultAssistedAnnotation() throws NoSuchFieldException {
  712. Assisted plainAssisted
  713. = Subaru.class.getDeclaredField("colorProvider").getAnnotation(Assisted.class);
  714. assertEqualsBothWays(FactoryProvider2.DEFAULT_ANNOTATION, plainAssisted);
  715. assertEquals(FactoryProvider2.DEFAULT_ANNOTATION.toString(), plainAssisted.toString());
  716. }
  717. interface GenericColoredCarFactory<T extends Car> {
  718. T create(Color color);
  719. }
  720. public void testGenericAssistedFactory() {
  721. final TypeLiteral<GenericColoredCarFactory<Mustang>> mustangTypeLiteral
  722. = new TypeLiteral<GenericColoredCarFactory<Mustang>>() {};
  723. final TypeLiteral<GenericColoredCarFactory<Camaro>> camaroTypeLiteral
  724. = new TypeLiteral<GenericColoredCarFactory<Camaro>>() {};
  725. Injector injector = Guice.createInjector(new AbstractModule() {
  726. @Override
  727. protected void configure() {
  728. bind(Double.class).toInstance(5.0d);
  729. bind(int.class).annotatedWith(Names.named("horsePower")).toInstance(250);
  730. bind(int.class).annotatedWith(Names.named("modelYear")).toInstance(1984);
  731. bind(mustangTypeLiteral)
  732. .toProvider(FactoryProvider.newFactory(mustangTypeLiteral, TypeLiteral.get(Mustang.class)));
  733. bind(camaroTypeLiteral)
  734. .toProvider(FactoryProvider.newFactory(camaroTypeLiteral, TypeLiteral.get(Camaro.class)));
  735. }
  736. });
  737. GenericColoredCarFactory<Mustang> mustangFactory
  738. = injector.getInstance(Key.get(mustangTypeLiteral));
  739. GenericColoredCarFactory<Camaro> camaroFactory
  740. = injector.getInstance(Key.get(camaroTypeLiteral));
  741. Mustang blueMustang = mustangFactory.create(Color.BLUE);
  742. assertEquals(Color.BLUE, blueMustang.color);
  743. assertEquals(5.0d, blueMustang.engineSize);
  744. Camaro redCamaro = camaroFactory.create(Color.RED);
  745. assertEquals(Color.RED, redCamaro.color);
  746. assertEquals(1984, redCamaro.modelYear);
  747. assertEquals(250, redCamaro.horsePower);
  748. }
  749. @SuppressWarnings("unused")
  750. public interface Insurance<T extends Car> {
  751. }
  752. public static class MustangInsurance implements Insurance<Mustang> {
  753. private final double premium;
  754. private final double limit;
  755. @SuppressWarnings("unused") private Mustang car;
  756. @Inject
  757. public MustangInsurance(@Named("lowLimit") double limit, @Assisted Mustang car,
  758. @Assisted double premium) {
  759. this.premium = premium;
  760. this.limit = limit;
  761. this.car = car;
  762. }
  763. public void sell() {}
  764. }
  765. public static class CamaroInsurance implements Insurance<Camaro> {
  766. private final double premium;
  767. private final double limit;
  768. @SuppressWarnings("unused") private Camaro car;
  769. @Inject
  770. public CamaroInsurance(@Named("highLimit") double limit, @Assisted Camaro car,
  771. @Assisted double premium) {
  772. this.premium = premium;
  773. this.limit = limit;
  774. this.car = car;
  775. }
  776. public void sell() {}
  777. }
  778. public interface MustangInsuranceFactory {
  779. public Insurance<Mustang> create(Mustang car, double premium);
  780. }
  781. public interface CamaroInsuranceFactory {
  782. public Insurance<Camaro> create(Camaro car, double premium);
  783. }
  784. public void testAssistedFactoryForConcreteType() {
  785. Injector injector = Guice.createInjector(new AbstractModule() {
  786. @Override
  787. protected void configure() {
  788. bind(Double.class).annotatedWith(Names.named("lowLimit")).toInstance(50000.0d);
  789. bind(Double.class).annotatedWith(Names.named("highLimit")).toInstance(100000.0d);
  790. bind(MustangInsuranceFactory.class).toProvider(
  791. FactoryProvider.newFactory(MustangInsuranceFactory.class, MustangInsurance.class));
  792. bind(CamaroInsuranceFactory.class).toProvider(
  793. FactoryProvider.newFactory(CamaroInsuranceFactory.class, CamaroInsurance.class));
  794. }
  795. });
  796. MustangInsuranceFactory mustangInsuranceFactory =
  797. injector.getInstance(MustangInsuranceFactory.class);
  798. CamaroInsuranceFactory camaroInsuranceFactory =
  799. injector.getInstance(CamaroInsuranceFactory.class);
  800. Mustang mustang = new Mustang(5000d, Color.BLACK);
  801. MustangInsurance mustangPolicy =
  802. (MustangInsurance) mustangInsuranceFactory.create(mustang, 800.0d);
  803. assertEquals(800.0d, mustangPolicy.premium);
  804. assertEquals(50000.0d, mustangPolicy.limit);
  805. Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
  806. CamaroInsurance camaroPolicy = (CamaroInsurance) camaroInsuranceFactory.create(camaro, 800.0d);
  807. assertEquals(800.0d, camaroPolicy.premium);
  808. assertEquals(100000.0d, camaroPolicy.limit);
  809. }
  810. public interface InsuranceFactory<T extends Car> {
  811. public Insurance<T> create(T car, double premium);
  812. }
  813. public void testAssistedFactoryForParameterizedType() {
  814. final TypeLiteral<InsuranceFactory<Mustang>> mustangInsuranceFactoryType =
  815. new TypeLiteral<InsuranceFactory<Mustang>>() {};
  816. final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType =
  817. new TypeLiteral<InsuranceFactory<Camaro>>() {};
  818. Injector injector = Guice.createInjector(new AbstractModule() {
  819. @Override
  820. protected void configure() {
  821. bind(Double.class).annotatedWith(Names.named("lowLimit")).toInstance(50000.0d);
  822. bind(Double.class).annotatedWith(Names.named("highLimit")).toInstance(100000.0d);
  823. bind(mustangInsuranceFactoryType).toProvider(FactoryProvider.newFactory(
  824. mustangInsuranceFactoryType, TypeLiteral.get(MustangInsurance.class)));
  825. bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(
  826. camaroInsuranceFactoryType, TypeLiteral.get(CamaroInsurance.class)));
  827. }
  828. });
  829. InsuranceFactory<Mustang> mustangInsuranceFactory =
  830. injector.getInstance(Key.get(mustangInsuranceFactoryType));
  831. InsuranceFactory<Camaro> camaroInsuranceFactory =
  832. injector.getInstance(Key.get(camaroInsuranceFactoryType));
  833. Mustang mustang = new Mustang(5000d, Color.BLACK);
  834. MustangInsurance mustangPolicy =
  835. (MustangInsurance) mustangInsuranceFactory.create(mustang, 800.0d);
  836. assertEquals(800.0d, mustangPolicy.premium);
  837. assertEquals(50000.0d, mustangPolicy.limit);
  838. Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
  839. CamaroInsurance camaroPolicy = (CamaroInsurance) camaroInsuranceFactory.create(camaro, 800.0d);
  840. assertEquals(800.0d, camaroPolicy.premium);
  841. assertEquals(100000.0d, camaroPolicy.limit);
  842. }
  843. public static class AutoInsurance<T extends Car> implements Insurance<T> {
  844. private final double premium;
  845. private final double limit;
  846. private final T car;
  847. @Inject
  848. public AutoInsurance(double limit, @Assisted T car, @Assisted double premium) {
  849. this.limit = limit;
  850. this.car = car;
  851. this.premium = premium;
  852. }
  853. public void sell() {}
  854. }
  855. public void testAssistedFactoryForTypeVariableParameters() {
  856. final TypeLiteral<InsuranceFactory<Camaro>> camaroInsuranceFactoryType =
  857. new TypeLiteral<InsuranceFactory<Camaro>>() {};
  858. Injector injector = Guice.createInjector(new AbstractModule() {
  859. @Override
  860. protected void configure() {
  861. bind(Double.class).toInstance(50000.0d);
  862. bind(camaroInsuranceFactoryType).toProvider(FactoryProvider.newFactory(
  863. camaroInsuranceFactoryType, new TypeLiteral<AutoInsurance<Camaro>>() {}));
  864. }
  865. });
  866. InsuranceFactory<Camaro> camaroInsuranceFactory =
  867. injector.getInstance(Key.get(camaroInsuranceFactoryType));
  868. Camaro camaro = new Camaro(3000, 1967, Color.BLUE);
  869. AutoInsurance<?> camaroPolicy =
  870. (AutoInsurance<?>) camaroInsuranceFactory.create(camaro, 800.0d);
  871. assertEquals(800.0d, camaroPolicy.premium);
  872. assertEquals(50000.0d, camaroPolicy.limit);
  873. assertEquals(camaro, camaroPolicy.car);
  874. }
  875. public void testInjectingAndUsingInjector() {
  876. Injector injector = Guice.createInjector(new AbstractModule() {
  877. @Override protected void configure() {
  878. bind(ColoredCarFactory.class).toProvider(
  879. FactoryProvider.newFactory(ColoredCarFactory.class, Segway.class));
  880. }
  881. });
  882. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  883. Segway green = (Segway)carFactory.create(Color.GREEN);
  884. assertSame(Color.GREEN, green.getColor());
  885. assertSame(Color.GREEN, green.getColor());
  886. Segway pink = (Segway)carFactory.create(Color.PINK);
  887. assertSame(Color.PINK, pink.getColor());
  888. assertSame(Color.PINK, pink.getColor());
  889. assertSame(Color.GREEN, green.getColor());
  890. }
  891. public void testDuplicateAssistedFactoryBinding() {
  892. Injector injector = Guice.createInjector(new AbstractModule() {
  893. @Override
  894. protected void configure() {
  895. bind(Double.class).toInstance(5.0d);
  896. bind(ColoredCarFactory.class).toProvider(
  897. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  898. bind(ColoredCarFactory.class).toProvider(
  899. FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
  900. }
  901. });
  902. ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
  903. Mustang blueMustang = (Mustang) carFactory.create(Color.BLUE);
  904. assertEquals(Color.BLUE, blueMustang.color);
  905. assertEquals(5.0d, blueMustang.engineSize);
  906. Mustang redMustang = (Mustang) carFactory.create(Color.RED);
  907. assertEquals(Color.RED, redMustang.color);
  908. assertEquals(5.0d, redMustang.engineSize);
  909. }
  910. public interface Equals {
  911. enum ComparisonMethod { SHALLOW, DEEP; }
  912. interface Factory {
  913. Equals equals(Equals.ComparisonMethod comparisonMethod);
  914. }
  915. public static class Impl implements Equals {
  916. private final double sigma;
  917. private final ComparisonMethod comparisonMethod;
  918. @AssistedInject
  919. public Impl(double sigma, @Assisted ComparisonMethod comparisonMethod) {
  920. this.sigma = sigma;
  921. this.comparisonMethod = comparisonMethod;
  922. }
  923. }
  924. }
  925. public void testFactoryMethodCalledEquals() {
  926. Injector injector = Guice.createInjector(new AbstractModule() {
  927. @Override
  928. protected void configure() {
  929. bind(Double.class).toInstance(0.01d);
  930. bind(Equals.Factory.class).toProvider(
  931. FactoryProvider.newFactory(Equals.Factory.class, Equals.Impl.class));
  932. }
  933. });
  934. Equals.Factory equalsFactory = injector.getInstance(Equals.Factory.class);
  935. Equals.Impl shallowEquals = (Impl) equalsFactory.equals(ComparisonMethod.SHALLOW);
  936. assertEquals(ComparisonMethod.SHALLOW, shallowEquals.comparisonMethod);
  937. assertEquals(0.01d, shallowEquals.sigma);
  938. }
  939. static class Segway implements Car {
  940. @Inject Injector injector;
  941. Color getColor() { return injector.getInstance(Key.get(Color.class, FactoryProvider2.DEFAULT_ANNOTATION)); }
  942. }
  943. public void testReturnValueMatchesParamValue() {
  944. Injector injector = Guice.createInjector(new AbstractModule() {
  945. @Override
  946. public void configure() {
  947. install(new FactoryModuleBuilder().build(Delegater.Factory.class));
  948. }
  949. });
  950. Delegater delegate = new Delegater();
  951. Delegater user = injector.getInstance(Delegater.Factory.class).create(delegate);
  952. assertSame(delegate, user.delegate);
  953. }
  954. static class Delegater {
  955. interface Factory {
  956. Delegater create(Delegater delegate);
  957. }
  958. private final Delegater delegate;
  959. @Inject Delegater(@Assisted Delegater delegater) {
  960. this.delegate = delegater;
  961. }
  962. Delegater() {
  963. this.delegate = null;
  964. }
  965. }
  966. public static abstract class AbstractAssisted {
  967. interface Factory<O extends AbstractAssisted, I extends CharSequence> {
  968. O create(I string);
  969. }
  970. }
  971. static class ConcreteAssisted extends AbstractAssisted {
  972. @Inject ConcreteAssisted(@SuppressWarnings("unused") @Assisted String string) {}
  973. }
  974. static class ConcreteAssistedWithOverride extends AbstractAssisted {
  975. @AssistedInject
  976. ConcreteAssistedWithOverride(@SuppressWarnings("unused") @Assisted String string) {}
  977. @AssistedInject
  978. ConcreteAssistedWithOverride(@SuppressWarnings("unused") @Assisted StringBuilder sb) {}
  979. interface Factory extends AbstractAssisted.Factory<ConcreteAssistedWithOverride, String> {
  980. @Override ConcreteAssistedWithOverride create(String string);
  981. }
  982. interface Factory2 extends AbstractAssisted.Factory<ConcreteAssistedWithOverride, String> {
  983. @Override ConcreteAssistedWithOverride create(String string);
  984. ConcreteAssistedWithOverride create(StringBuilder sb);
  985. }
  986. }
  987. static class ConcreteAssistedWithoutOverride extends AbstractAssisted {
  988. @Inject ConcreteAssistedWithoutOverride(@SuppressWarnings("unused") @Assisted String string) {}
  989. interface Factory extends AbstractAssisted.Factory<ConcreteAssistedWithoutOverride, String> {}
  990. }
  991. public static class Public extends AbstractAssisted {
  992. @AssistedInject Public(@SuppressWarnings("unused") @Assisted String string) {}
  993. @AssistedInject Public(@SuppressWarnings("unused") @Assisted StringBuilder sb) {}
  994. public interface Factory extends AbstractAssisted.Factory<Public, String> {
  995. @Override Public create(String string);
  996. Public create(StringBuilder sb);
  997. }
  998. }
  999. // See https://github.com/google/guice/issues/904
  1000. public void testGeneratedDefaultMethodsForwardCorrectly() {
  1001. final Key<AbstractAssisted.Factory<ConcreteAssisted, String>> concreteKey =
  1002. new Key<AbstractAssisted.Factory<ConcreteAssisted, String>>() {};
  1003. Injector injector = Guice.createInjector(new AbstractModule() {
  1004. @Override protected void configure() {
  1005. install(new FactoryModuleBuilder().build(ConcreteAssistedWithOverride.Factory.class));
  1006. install(new FactoryModuleBuilder().build(ConcreteAssistedWithOverride.Factory2.class));
  1007. install(new FactoryModuleBuilder().build(ConcreteAssistedWithoutOverride.Factory.class));
  1008. install(new FactoryModuleBuilder().build(Public.Factory.class));
  1009. install(new FactoryModuleBuilder().build(concreteKey));
  1010. }
  1011. });
  1012. ConcreteAssistedWithOverride.Factory factory1 =
  1013. injector.getInstance(ConcreteAssistedWithOverride.Factory.class);
  1014. factory1.create("foo");
  1015. AbstractAssisted.Factory<ConcreteAssistedWithOverride, String> factory1Abstract = factory1;
  1016. factory1Abstract.create("foo");
  1017. ConcreteAssistedWithOverride.Factory2 factory2 =
  1018. injector.getInstance(ConcreteAssistedWithOverride.Factory2.class);
  1019. factory2.create("foo");
  1020. factory2.create(new StringBuilder("foo"));
  1021. AbstractAssisted.Factory<ConcreteAssistedWithOverride, String> factory2Abstract = factory2;
  1022. factory2Abstract.create("foo");
  1023. ConcreteAssistedWithoutOverride.Factory factory3 =
  1024. injector.getInstance(ConcreteAssistedWithoutOverride.Factory.class);
  1025. factory3.create("foo");
  1026. AbstractAssisted.Factory<ConcreteAssistedWithoutOverride, String> factory3Abstract = factory3;
  1027. factory3Abstract.create("foo");
  1028. Public.Factory factory4 = injector.getInstance(Public.Factory.class);
  1029. factory4.create("foo");
  1030. factory4.create(new StringBuilder("foo"));
  1031. AbstractAssisted.Factory<Public, String> factory4Abstract = factory4;
  1032. factory4Abstract.create("foo");
  1033. AbstractAssisted.Factory<ConcreteAssisted, String> factory5 =
  1034. injector.getInstance(concreteKey);
  1035. factory5.create("foo");
  1036. }
  1037. }