Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PY Ricau: DIY - Build Your Own Dependency Injec...

Realm
April 13, 2016

PY Ricau: DIY - Build Your Own Dependency Injection Library

Realm

April 13, 2016
Tweet

More Decks by Realm

Other Decks in Programming

Transcript

  1. public class VisitHandler {
 
 private final Counter counter;
 private

    final Logger logger;
 
 public VisitHandler(Counter counter, Logger logger) {
 this.counter = counter;
 this.logger = logger;
 }
 
 public void visit() {
 counter.increment();
 logger.log("Visits increased: " + counter.getCount());
 }
 }
  2. public class Counter {
 
 private int count;
 
 void

    increment() {
 count++;
 }
 
 int getCount() {
 return count;
 }
 }
  3. public class Counter {
 
 private int count;
 
 void

    increment() {
 count++;
 }
 
 int getCount() {
 return count;
 }
 } public class Logger {
 
 private final PrintStream printStream;
 
 public Logger(PrintStream printStream) {
 this.printStream = printStream;
 }
 
 public void log(String log) {
 printStream.println(log);
 }
 
 }
  4. public static void main(String... args) {
 Counter counter = new

    Counter();
 PrintStream printStream = System.out;
 Logger logger = new Logger(printStream);
 VisitHandler visitHandler = new VisitHandler(counter, logger);
 visitHandler.visit();
 }
  5. public static void main(String... args) {
 Counter counter = new

    Counter();
 PrintStream printStream = System.out;
 Logger logger = new Logger(printStream);
 VisitHandler visitHandler = new VisitHandler(counter, logger);
 visitHandler.visit();
 }
  6. public static void main(String... args) {
 Counter counter = new

    Counter();
 PrintStream printStream = System.out;
 Logger logger = new Logger(printStream);
 VisitHandler visitHandler = new VisitHandler(counter, logger);
 visitHandler.visit();
 }
  7. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();

    VisitHandler visitHandler = objectGraph.get(VisitHandler.class); 
 visitHandler.visit();
 }
  8. public class ObjectGraph {
 
 public <T> T get(Class<T> key)

    {
 throw new UnsupportedOperationException("Not implemented");
 }
 }
  9. public interface Factory<T> {
 T get(Linker linker);
 } public class

    Linker {
 
 private final Map<Class<?>, Factory<?>> factories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 return (Factory<T>) factories.get(key);
 }
 }
  10. public interface Factory<T> {
 T get(Linker linker);
 } public class

    Linker {
 
 private final Map<Class<?>, Factory<?>> factories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 return (Factory<T>) factories.get(key);
 }
 }
  11. public interface Factory<T> {
 T get(Linker linker);
 } public class

    Linker {
 
 private final Map<Class<?>, Factory<?>> factories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 return (Factory<T>) factories.get(key);
 }
 }
  12. public class ObjectGraph {
 
 private final Linker linker;
 


    public ObjectGraph(Linker linker) {
 this.linker = linker;
 }
 
 public <T> T get(Class<T> key) {
 Factory<T> factory = linker.factoryFor(key);
 return factory.get(linker);
 }
 }
  13. public class ObjectGraph {
 
 private final Linker linker;
 


    public ObjectGraph(Linker linker) {
 this.linker = linker;
 }
 
 public <T> T get(Class<T> key) {
 Factory<T> factory = linker.factoryFor(key);
 return factory.get(linker);
 }
 }
  14. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();

    VisitHandler visitHandler = objectGraph.get(VisitHandler.class); 
 visitHandler.visit();
 }
  15. private static ObjectGraph buildObjectGraph() {
 Linker linker = new Linker();


    installFactories(linker);
 return new ObjectGraph(linker);
 }
  16. private static void installFactories(Linker linker) {
 linker.install(VisitHandler.class, new Factory<VisitHandler>() {


    @Override public VisitHandler get(Linker linker) {
 Factory<Counter> counterFactory = linker.factoryFor(Counter.class);
 Factory<Logger> loggerFactory = linker.factoryFor(Logger.class);
 Counter counter = counterFactory.get(linker);
 Logger logger = loggerFactory.get(linker);
 return new VisitHandler(counter, logger);
 }
 });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, new Factory<PrintStream>() {
 @Override public PrintStream get(Linker linker) {
 return System.out;

  17. private static void installFactories(Linker linker) {
 linker.install(VisitHandler.class, new Factory<VisitHandler>() {


    @Override public VisitHandler get(Linker linker) {
 Factory<Counter> counterFactory = linker.factoryFor(Counter.class);
 Factory<Logger> loggerFactory = linker.factoryFor(Logger.class);
 Counter counter = counterFactory.get(linker);
 Logger logger = loggerFactory.get(linker);
 return new VisitHandler(counter, logger);
 }
 });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, new Factory<PrintStream>() {
 @Override public PrintStream get(Linker linker) {
 return System.out;

  18. });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker

    linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, new Factory<PrintStream>() {
 @Override public PrintStream get(Linker linker) {
 return System.out;
 }
 });
 
 linker.install(Counter.class, new Factory<Counter>() {
 @Override public Counter get(Linker linker) {
 return new Counter();
 }
 });
 }
  19. });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker

    linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, new Factory<Counter>() {
 @Override public Counter get(Linker linker) {
 return new Counter();
 }
 });
 }
  20. public final class ValueFactory<T> implements Factory<T> {
 
 public static

    <T> Factory<T> of(T instance) {
 return new ValueFactory<>(instance);
 }
 
 private final T value;
 
 private ValueFactory(T value) {
 this.value = value;
 }
 
 @Override public T get(Linker linker) {
 return value;
 }
 }
  21. public final class ValueFactory<T> implements Factory<T> {
 
 public static

    <T> Factory<T> of(T instance) {
 return new ValueFactory<>(instance);
 }
 
 private final T value;
 
 private ValueFactory(T value) {
 this.value = value;
 }
 
 @Override public T get(Linker linker) {
 return value;
 }
 }
  22. });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker

    linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, new Factory<Counter>() {
 @Override public Counter get(Linker linker) {
 return new Counter();
 }
 });
 }
  23. });
 
 linker.install(Logger.class, new Factory<Logger>() {
 @Override public Logger get(Linker

    linker) {
 Factory<PrintStream> printStreamFactory 
 = linker.factoryFor(PrintStream.class);
 return new Logger(printStreamFactory.get(linker));
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, SingletonFactory.of(new Factory<~>() {
 @Override public Counter get(Linker linker) {
 return new Counter();
 }
 }));
 }
  24. public class SingletonFactory<T> implements Factory<T> {
 
 public static <T>

    Factory<T> of(Factory<T> factory) {
 return new SingletonFactory<>(factory);
 }
 
 private final Factory<T> factory;
 private T instance;
 
 private SingletonFactory(Factory<T> factory) {
 this.factory = factory;
 }
 
 @Override public final T get(Linker linker) {
 if (instance == null) {
 instance = factory.get(linker);
 }
 return instance;
 }
 }
  25. public class SingletonFactory<T> implements Factory<T> {
 
 public static <T>

    Factory<T> of(Factory<T> factory) {
 return new SingletonFactory<>(factory);
 }
 
 private final Factory<T> factory;
 private T instance;
 
 private SingletonFactory(Factory<T> factory) {
 this.factory = factory;
 }
 
 @Override public final T get(Linker linker) {
 if (instance == null) {
 instance = factory.get(linker);
 }
 return instance;
 }
 }
  26. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 return (Factory<T>) factories.get(key);
 }
 }
  27. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 System.out.println("Get factory for " + key);
 return (Factory<T>) factories.get(key);
 }
 }
  28. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();

    VisitHandler visitHandler = objectGraph.get(VisitHandler.class); 
 visitHandler.visit();
 }
  29. Get factory for class v1.example.VisitHandler Get factory for class v1.example.Counter

    Get factory for class v1.example.Logger Get factory for class java.io.PrintStream
  30. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();

    VisitHandler visitHandler = objectGraph.get(VisitHandler.class); VisitHandler visitHandler2 = objectGraph.get(VisitHandler.class);
 }
  31. Get factory for class v1.example.VisitHandler Get factory for class v1.example.Counter

    Get factory for class v1.example.Logger Get factory for class java.io.PrintStream Get factory for class v1.example.VisitHandler Get factory for class v1.example.Counter Get factory for class v1.example.Logger Get factory for class java.io.PrintStream
  32. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 return (Factory<T>) factories.get(key);
 }
 }
  33. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  34. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  35. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  36. linker.install(VisitHandler.class, new Factory<VisitHandler>() {
 @Override public VisitHandler get(Linker linker) {


    Factory<Counter> counterFactory = linker.factoryFor(Counter.class);
 Factory<Logger> loggerFactory = linker.factoryFor(Logger.class);
 Counter counter = counterFactory.get(linker);
 Logger logger = loggerFactory.get(linker);
 return new VisitHandler(counter, logger);
 }
 });
  37. linker.install(VisitHandler.class, new Factory<VisitHandler>() {
 
 Factory<Logger> loggerFactory;
 Factory<Counter> counterFactory;
 


    @Override public void link(Linker linker) {
 counterFactory = linker.factoryFor(Counter.class);
 loggerFactory = linker.factoryFor(Logger.class);
 }
 
 @Override public VisitHandler get() {
 return new VisitHandler(counterFactory.get(), loggerFactory.get());
 }
 });
  38. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) { Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  39. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) {
 System.out.println("Get factory for " + key);
 Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 System.out.println("Link factory for " + key);
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  40. Get factory for class v2.example.VisitHandler Link factory for class v2.example.VisitHandler

    Get factory for class v2.example.Counter Link factory for class v2.example.Counter Get factory for class v2.example.Logger Link factory for class v2.example.Logger Get factory for class java.io.PrintStream Link factory for class java.io.PrintStream Get factory for class v2.example.VisitHandler
  41. private static void installFactories(Linker linker) {
 linker.install(VisitHandler.class, new Factory<VisitHandler>() {


    
 Factory<Logger> loggerFactory;
 Factory<Counter> counterFactory;
 
 @Override public void link(Linker linker) {
 counterFactory = linker.factoryFor(Counter.class);
 loggerFactory = linker.factoryFor(Logger.class);
 }
 
 @Override public VisitHandler get() {
 return new VisitHandler(counterFactory.get(), loggerFactory.get());
 }
 });
 
 linker.install(Logger.class, new Factory<Logger>() {
 
 Factory<PrintStream> printStreamFactory;
 
 @Override public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);

  42. 
 linker.install(Logger.class, new Factory<Logger>() {
 
 Factory<PrintStream> printStreamFactory;
 
 @Override

    public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, SingletonFactory.of(new Factory<Counter>() {
 
 @Override public Counter get() {
 return new Counter();
 }
 }));
 }
  43. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) { Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = factories.get(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  44. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) { Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = loadFactory(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  45. private <T> Factory<?> loadFactory(Class<T> key) {
 Factory<?> factory = factories.get(key);


    
 if (factory != null) {
 return factory;
 }
 throw new IllegalStateException("No factory for " + key);
 }
  46. public class VisitHandler {
 
 private final Counter counter;
 private

    final Logger logger;
 
 public VisitHandler(Counter counter, Logger logger) {
 this.counter = counter;
 this.logger = logger;
 }
 
 public void visit() {
 counter.increment();
 logger.log("Visits increased: " + counter.getCount());
 }
 }
  47. public class VisitHandler {
 
 private final Counter counter;
 private

    final Logger logger;
 
 @Inject public VisitHandler(Counter counter, Logger logger) {
 this.counter = counter;
 this.logger = logger;
 }
 
 public void visit() {
 counter.increment();
 logger.log("Visits increased: " + counter.getCount());
 }
 }
  48. private <T> Factory<?> loadFactory(Class<T> key) {
 Factory<?> factory = factories.get(key);


    
 if (factory != null) {
 return factory;
 }
 throw new IllegalStateException("No factory for " + key);
 }
  49. private <T> Factory<?> loadFactory(Class<T> key) {
 Factory<?> factory = factories.get(key);


    
 if (factory != null) {
 return factory;
 }
 
 Constructor<T> constructor = findAtInjectConstructor(key);
 if (constructor != null) {
 factory = new ReflectiveFactory<>(constructor);
 if (key.isAnnotationPresent(Singleton.class)) {
 factory = SingletonFactory.of(factory);
 }
 return factory;
 }
 throw new IllegalStateException("No factory for " + key);
 }
  50. private <T> Constructor<T> findAtInjectConstructor(Class<T> type) {
 for (Constructor<?> constructor :

    type.getConstructors()) {
 if (constructor.isAnnotationPresent(Inject.class)) {
 return (Constructor<T>) constructor;
 }
 }
 return null;
 }
  51. public class ReflectiveFactory<T> extends Factory<T> {
 
 private final Constructor<T>

    constructor;
 private final ArrayList<Factory<?>> factories = new ArrayList<>();
 
 public ReflectiveFactory(Constructor<T> constructor) {
 this.constructor = constructor;
 }
 
 @Override protected void link(Linker linker) {
 for (Class<?> parameterType : constructor.getParameterTypes()) {
 factories.add(linker.factoryFor(parameterType));
 }
 }
 
 @Override public T get() {
 Object[] dependencies = new Object[factories.size()];
 for (int i = 0; i < dependencies.length; i++) {
 Factory<?> factory = factories.get(i);
 dependencies[i] = factory.get();
 }
 

  52. this.constructor = constructor;
 }
 
 @Override protected void link(Linker linker)

    {
 for (Class<?> parameterType : constructor.getParameterTypes()) {
 factories.add(linker.factoryFor(parameterType));
 }
 }
 
 @Override public T get() {
 Object[] dependencies = new Object[factories.size()];
 for (int i = 0; i < dependencies.length; i++) {
 Factory<?> factory = factories.get(i);
 dependencies[i] = factory.get();
 }
 
 try {
 return constructor.newInstance(dependencies);
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }
 }
  53. private static void installFactories(Linker linker) {
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 } private

    static void installFactories(Linker linker) {
 linker.install(VisitHandler.class, new Factory<VisitHandler>() {
 
 Factory<Logger> loggerFactory;
 Factory<Counter> counterFactory;
 
 @Override public void link(Linker linker) {
 counterFactory = linker.factoryFor(Counter.class);
 loggerFactory = linker.factoryFor(Logger.class);
 }
 
 @Override public VisitHandler get() {
 return new VisitHandler(counterFactory.get(), loggerFactory.get());
 }
 });
 
 linker.install(Logger.class, new Factory<Logger>() {
 
 Factory<PrintStream> printStreamFactory;
 
 @Override public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, SingletonFactory.of(new Factory<Counter>() {
 
 @Override public Counter get() {
 return new Counter();
 }
 }));
 }
  54. linker.install(Logger.class, new Factory<Logger>() {
 
 Factory<PrintStream> printStreamFactory;
 
 @Override public

    void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 });
  55. public class Logger$$Factory extends Factory<Logger> {
 
 Factory<PrintStream> printStreamFactory;
 


    @Override public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 }
  56. public class Logger$$Factory extends GeneratedFactory<Logger> {
 Factory<PrintStream> printStreamFactory;
 protected Logger$$Factory()

    {
 super(false);
 } 
 @Override public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 public abstract class GeneratedFactory<T> extends Factory<T> {
 private final boolean singleton;
 
 protected GeneratedFactory(boolean singleton) {
 this.singleton = singleton;
 }
 }
  57. public abstract class GeneratedFactory<T> extends Factory<T> {
 public static <T>

    Factory<T> loadFactory(Class<T> key) {
 String generatedClass = key.getName() + "$$Factory";
 try {
 Class<GeneratedFactory<T>> factoryClass =
 (Class<GeneratedFactory<T>>) Class.forName(generatedClass);
 GeneratedFactory<T> factory = factoryClass.newInstance();
 if (factory.singleton) {
 return SingletonFactory.of(factory);
 } else {
 return factory;
 }
 } catch (Exception e) {
 return null;
 }
 }
 
 private final boolean singleton;
 
 protected GeneratedFactory(boolean singleton) {
 this.singleton = singleton;
 }

  58. private <T> Factory<?> loadFactory(Class<T> key) {
 Factory<?> factory = factories.get(key);


    
 if (factory != null) {
 return factory;
 }
 throw new IllegalStateException("No factory for " + key);
 }
  59. private <T> Factory<?> loadFactory(Class<T> key) {
 Factory<?> factory = factories.get(key);


    
 if (factory != null) {
 return factory;
 }
 
 factory = GeneratedFactory.loadFactory(key);
 
 if (factory != null) {
 return factory;
 }
 throw new IllegalStateException("No factory for " + key);
 }
  60. public class DatabaseManager {
 
 private final Database database;
 


    @Inject public DatabaseManager(Database database) {
 this.database = database;
 }
 
 public void updateVisits(int count) {
 database.execute("UPDATE count_table SET count = " + count);
 }
 }
  61. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    DatabaseManager databaseManager = objectGraph.get(DatabaseManager.class);
 databaseManager.updateVisits(2);
 }
  62. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    DatabaseManager databaseManager = objectGraph.get(DatabaseManager.class);
 databaseManager.updateVisits(2);
 } Exception in thread "main" java.lang.IllegalStateException: No factory for class v3.example.Database at v3.Linker.loadFactory(Linker.java:50) at v3.Linker.factoryFor(Linker.java:20) at v3.ReflectiveFactory.link(ReflectiveFactory.java:17) at v3.Linker.factoryFor(Linker.java:21) at v3.ObjectGraph.get(ObjectGraph.java:16) at v3.example.Main.main(Main.java:18)
  63. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    objectGraph.validate(DatabaseManager.class);
 // ...
 }
  64. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    objectGraph.validate(DatabaseManager.class);
 // ...
 } public class ObjectGraph {
 
 private final Linker linker;
 
 public ObjectGraph(Linker linker) {
 this.linker = linker;
 }
 
 public <T> void validate(Class<T> key) {
 linker.factoryFor(key);
 }
 
 public <T> T get(Class<T> key) {
 Factory<T> factory = linker.factoryFor(key);
 return factory.get();

  65. private static void installFactories(Linker linker) {
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 } private

    static void installFactories(Linker linker) {
 linker.install(VisitHandler.class, new Factory<VisitHandler>() {
 
 Factory<Logger> loggerFactory;
 Factory<Counter> counterFactory;
 
 @Override public void link(Linker linker) {
 counterFactory = linker.factoryFor(Counter.class);
 loggerFactory = linker.factoryFor(Logger.class);
 }
 
 @Override public VisitHandler get() {
 return new VisitHandler(counterFactory.get(), loggerFactory.get());
 }
 });
 
 linker.install(Logger.class, new Factory<Logger>() {
 
 Factory<PrintStream> printStreamFactory;
 
 @Override public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 });
 
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 
 linker.install(Counter.class, SingletonFactory.of(new Factory<Counter>() {
 
 @Override public Counter get() {
 return new Counter();
 }
 }));
 }
  66. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    objectGraph.validate(DatabaseManager.class);
 // ...
 } private static void installFactories(Linker linker) {
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 }
  67. @Provides ApiClient provideApiClient(ApiClientImpl realApiClient) {
 return realApiClient;
 } private static

    void installFactories(Linker linker) {
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 }
  68. 
 public class MainModule {
 
 @Provides ApiClient provideApiClient(ApiClientImpl realApiClient)

    {
 return realApiClient;
 }
 
 
 @Provides PrintStream providePrintStream() {
 return System.out;
 }
 }
  69. private static ObjectGraph buildObjectGraph() {
 Linker linker = new Linker();


    installFactories(linker);
 return new ObjectGraph(linker);
 } private static void installFactories(Linker linker) {
 linker.install(PrintStream.class, ValueFactory.of(System.out));
 }
  70. private static ObjectGraph buildObjectGraph(List<Class> modulesClasses) {
 Linker linker = new

    Linker();
 linker.loadModuleBindings(modulesClasses);
 return new ObjectGraph(linker);
 }
  71. public static void main(String... args) {
 ObjectGraph objectGraph = buildObjectGraph();


    objectGraph.validate(DatabaseManager.class);
 // ...
 }
  72. 
 public class MainModule {
 
 @Provides ApiClient provideApiClient(ApiClientImpl realApiClient)

    {
 return realApiClient;
 }
 
 
 @Provides PrintStream providePrintStream() {
 return System.out;
 }
 }
  73. @Module(injects = DatabaseManager.class)
 public class MainModule {
 
 @Provides ApiClient

    provideApiClient(ApiClientImpl realApiClient) {
 return realApiClient;
 }
 
 
 @Provides PrintStream providePrintStream() {
 return System.out;
 }
 }
  74. public class TransactionHandler {
 
 private final Analytics analytics;
 private

    final TaxCache taxCache;
 
 private Payment paymentInFlight;
 private Order order;
 
 @Inject TransactionHandler(Analytics analytics, TaxCache taxCache) {
 this.analytics = analytics;
 this.taxCache = taxCache;
 }
 
 // ...
 }
  75. public class TransactionHandler {
 
 private final Analytics analytics;
 private

    final TaxCache taxCache;
 
 private Payment paymentInFlight;
 private Order order;
 
 @Inject TransactionHandler(Analytics analytics, TaxCache taxCache) {
 this.analytics = analytics;
 this.taxCache = taxCache;
 }
 
 // ...
 }
  76. @Singleton public class TransactionHandler {
 
 private final Analytics analytics;


    private final TaxCache taxCache;
 
 private Payment paymentInFlight;
 private Order order;
 
 @Inject TransactionHandler(Analytics analytics, TaxCache taxCache) {
 this.analytics = analytics;
 this.taxCache = taxCache;
 }
 
 // ...
 }
  77. public class ObjectGraph {
 
 Linker linker;
 
 public <T>

    T get(Class<T> key) {
 Factory<T> factory = linker.factoryFor(key);
 return factory.get();
 }
 }
  78. public class ObjectGraph {
 
 Linker linker;
 ObjectGraph parentGraph;
 


    public <T> T get(Class<T> key) {
 if (parentGraph != null) {
 T instance = parentGraph.get(key);
 if (instance != null) {
 return instance;
 }
 }
 Factory<T> factory = linker.factoryFor(key);
 return factory.get();
 }
 }
  79. public class Linker {
 
 private final Map<Class<?>, Factory<?>> factories

    = new HashMap<>();
 private final Map<Class<?>, Factory<?>> linkedFactories = new HashMap<>();
 
 public <T> void install(Class<T> key, Factory<T> factory) {
 factories.put(key, factory);
 }
 
 public <T> Factory<T> factoryFor(Class<T> key) { Factory<?> factory = linkedFactories.get(key);
 
 if (factory == null) {
 factory = loadFactory(key);
 factory.link(this);
 linkedFactories.put(key, factory);
 }
 return (Factory<T>) factory;
 }
 }
  80. public class Logger$$Factory extends Factory<Logger> {
 Factory<PrintStream> printStreamFactory;
 
 @Override

    public void link(Linker linker) {
 printStreamFactory = linker.factoryFor(PrintStream.class);
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 }
  81. public class Logger$$Factory extends Factory<Logger> {
 final Factory<PrintStream> printStreamFactory;
 


    public Logger$$Factory(Factory<PrintStream> printStreamFactory) {
 this.printStreamFactory = printStreamFactory;
 }
 
 @Override public Logger get() {
 return new Logger(printStreamFactory.get());
 }
 }
  82. @Module(injects = DatabaseManager.class)
 public class MainModule {
 
 @Provides ApiClient

    provideApiClient(ApiClientImpl realApiClient) {
 return realApiClient;
 }
 
 
 @Provides PrintStream providePrintStream() {
 return System.out;
 }
 }
  83. @Module
 public class MainModule {
 
 @Provides ApiClient provideApiClient(ApiClientImpl realApiClient)

    {
 return realApiClient;
 }
 
 
 @Provides PrintStream providePrintStream() {
 return System.out;
 }
 } @Component(modules = MainModule.class)
 public interface MainComponent {
 DatabaseManager databaseManager();
 }
  84. @Component(modules = MainModule.class)
 public interface MainComponent {
 DatabaseManager databaseManager();
 }

    public class Dagger_MainComponent implements MainComponent {
 Factory<PrintStream> printStreamFactory;
 Factory<Logger> loggerFactory;
 Factory<DatabaseManager> databaseManagerFactory;
 
 Dagger_MainComponent() {
 final MainModule mainModule = new MainModule();
 printStreamFactory = new Factory<PrintStream>() {
 @Override public PrintStream get() {
 return mainModule.providePrintStream();
 }
 };
 loggerFactory = new Logger$$Factory(printStreamFactory);
 databaseManagerFactory = new DatabaseManager$$Factory(loggerFactory);
 }
 
 @Override public DatabaseManager databaseManager() {

  85. public class Dagger_MainComponent implements MainComponent {
 Factory<PrintStream> printStreamFactory;
 Factory<Logger> loggerFactory;


    Factory<DatabaseManager> databaseManagerFactory;
 
 Dagger_MainComponent() {
 final MainModule mainModule = new MainModule();
 printStreamFactory = new Factory<PrintStream>() {
 @Override public PrintStream get() {
 return mainModule.providePrintStream();
 }
 };
 loggerFactory = new Logger$$Factory(printStreamFactory);
 databaseManagerFactory = new DatabaseManager$$Factory(loggerFactory);
 }
 
 @Override public DatabaseManager databaseManager() {
 return databaseManagerFactory.get();
 }
 }
  86. public class Dagger_MainComponent implements MainComponent {
 Factory<PrintStream> printStreamFactory;
 Factory<Logger> loggerFactory;


    Factory<DatabaseManager> databaseManagerFactory;
 
 Dagger_MainComponent() {
 final MainModule mainModule = new MainModule();
 printStreamFactory = new Factory<PrintStream>() {
 @Override public PrintStream get() {
 return mainModule.providePrintStream();
 }
 };
 loggerFactory = new Logger$$Factory(printStreamFactory);
 databaseManagerFactory = new DatabaseManager$$Factory(loggerFactory);
 }
 
 @Override public DatabaseManager databaseManager() {
 return databaseManagerFactory.get();
 }
 }