title: Dynamically typed objects created with dependency injection
body: <p>I have a piece of existing code that works using reflection, but I'd like to start creating the objects using dependency injection and Guice, if possible.</p>
  
  <p>Here's how it currently works:</p>
  
  <ol>
  <li>Configuration (<code>.properties</code>) file is loaded, with a string like
  <ul>
  <li><code>objects=Foo,^ab..$;Bar,^.bc.$;Baz,i*</code></li>
  <li>Note: <code>Foo</code>, <code>Bar</code>, and <code>Baz</code> are classes that implement <code>MyInterface</code></li>
  <li>Each pair has a regular expression paired with it.</li>
  </ul></li>
  <li>Input data is fed in from another source. Imagine for this example, the data is:
  <ul>
  <li><code>String[]{ "abab", "abcd", "dbca", "fghi", "jklm" }</code></li>
  </ul></li>
  <li>I then want to create new instances of <code>Foo</code>, <code>Bar</code> and <code>Baz</code> that are created by Guice.
  <ul>
  <li>The instances created, in this case, would be:
  <ul>
  <li><code>new Foo("abab");</code></li>
  <li><code>new Foo("abcd");</code></li>
  <li><code>new Bar("abcd");</code></li>
  <li><code>new Bar("dbca");</code></li>
  <li><code>new Baz("fghi");</code></li>
  <li><code>"jklm"</code> would not create any new instances, as it has no matching pattern.</li>
  </ul></li>
  </ul></li>
  </ol>
  
  <p>Here's how it works currently (this is the best I could do <a href="http://sscce.org/" rel="nofollow">sscce</a>-wise), using reflection:</p>
  
  <pre><code>public class MyInterfaceBuilder {
      private Classloader tcl = Thread.currentThread().getContextClassLoader();
  
      private Pattern p;
      private Class&lt;? extends MyInterface&gt; klass;
  
      public InterfaceBuilder(String className, String pattern) {
          this.pattern = Pattern.compile(pattern);
          this.klass = makeClass(className);
      }
  
      private static Class&lt;? extends Interface&gt; makeClass(String className) {
          String fullClassName = classPrefix + className;
          Class&lt;?&gt; myClass;
          try {
              myClass = tcl.loadClass(fullClassName);
          } catch (ClassNotFoundException e) {
              throw new IllegalArgumentException("Class not found: " + fullClassName, e);
          } 
  
          if(MyInterface.class.isAssignableFrom(myClass)) {
              return (Class&lt;? extends MyInterface&gt;) myClass; 
          } else {
              throw new IllegalArgumentException(fullClassName + " is not a MyInterface!");
          }
      }
  
      public MyInterface makeInstance(String type) {
          if (pattern == null || pattern.matcher(type).find()) {
              MyInterface newInstance = null;
              try {
                  newInstance = klass.getConstructor(String.class).newInstance(type);
              } catch (Exception e) {
                  // Handle exceptions
              }
              return newInstance;
          } else {
              return null;
          }
      }
  }
  </code></pre>
  
  <p>How can I duplicate this functionality (loading the classes dynamically at run-time, and creating exactly the matching instances) having using Guice?</p>