title: Using Reflections to create new class instances
body: <p>I am experimenting with reflections library found at:<a href="https://code.google.com/p/reflections/" rel="nofollow">https://code.google.com/p/reflections/</a> </p>
  
  <p>What I am trying to achieve, is scan a package within my project, and then create instances of all the subclasses of a given type, found within that package. 
  The way I am using the library is correct, as subTypes returns the following:</p>
  
  <blockquote>
    <p>[class identifiers.DNSLookup, class identifiers.AliasChecker, class
    identifiers.GoogleSafeBrowsing]</p>
  </blockquote>
  
  <p>Although my problem is how I can create new instances of the classes found within that set. All of them have no-argument constructors.</p>
  
  <pre><code>private void getDetectors(){
          Reflections reflections = new Reflections("identifiers"); //name of package to scan
  
          Set&lt;Class&lt;? extends DetectorSub&gt;&gt; subTypes =
                  reflections.getSubTypesOf(DetectorSub.class);
          System.out.println(subTypes); // correct classes included here.
          for(Class&lt;? extends DetectorSub&gt; detector:subTypes){
              try {
                  DetectorSub d =(DetectorSub)detector.getClass().newInstance().cast(DetectorSub.class); //returns exceptions at runtime.
              } catch (InstantiationException e) {
                  e.printStackTrace();  
              } catch (IllegalAccessException e) {
                  e.printStackTrace();  
              }
          }
      }
  </code></pre>
  
  <p>The above code returns the following exception: </p>
  
  <pre><code>java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class
      at java.lang.Class.newInstance0(Class.java:339)
      at java.lang.Class.newInstance(Class.java:327)
      at core.PhishingScanner.getDetectors(PhishingScanner.java:40)
      at core.PhishingScanner.&lt;init&gt;(PhishingScanner.java:28)
      at core.Main.main(Main.java:13)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  </code></pre>
  
  <p>As a final note, Is it possible to use the above functionality to scan classes based on their interface, rather than superclass? Thanks.</p>