title: Emulating Java Reflection with GWT deferred binding
body: <p>Here's some of my code: 
  My module contains: </p>
  
  <pre><code>&lt;generate-with class="com.bilancio.ReflectionGenerator"&gt; 
    &lt;when-type-assignable class="com.bilancio.client.Reflection" /&gt; 
  &lt;/generate-with&gt; 
  
  public interface Constructable {} 
  
  
  public interface Reflection { 
      public &lt;T&gt; T instantiate( String className ); 
  }
  
  
  public class ReflectionGenerator extends Generator 
  { 
      @Override 
      public String generate( TreeLogger logger, GeneratorContext 
  context, String typeName ) throws UnableToCompleteException 
      { 
          TypeOracle oracle = context.getTypeOracle( ); 
          JClassType markerInterface = 
  oracle.findType( Constructable.class.getName( ) ); 
          List&lt;JClassType&gt; clazzes = new ArrayList&lt;JClassType&gt;( ); 
          PropertyOracle propertyOracle = context.getPropertyOracle( ); 
          for ( JClassType classType : oracle.getTypes( ) ) 
          { 
              if ( !classType.equals( markerInterface ) &amp;&amp; 
  classType.isAssignableTo( markerInterface ) ) 
                  clazzes.add( classType ); 
          } 
          final String genPackageName = "com.bilancio.client"; 
          final String genClassName = "ReflectionImpl"; 
          ClassSourceFileComposerFactory composer = new 
  ClassSourceFileComposerFactory( genPackageName, genClassName ); 
  composer.addImplementedInterface( Reflection.class.getCanonicalName( ) ); 
          composer.addImport( "com.bilancio.client.*" ); 
          PrintWriter printWriter = context.tryCreate( logger, 
  genPackageName, genClassName ); 
          if ( printWriter != null ) 
          { 
              SourceWriter sourceWriter = 
  composer.createSourceWriter( context, printWriter ); 
              sourceWriter.println( "ReflectionImpl( ) {" ); 
              sourceWriter.println( "}" ); 
              printFactoryMethod( clazzes, sourceWriter ); 
              sourceWriter.commit( logger ); 
          } 
          return composer.getCreatedClassName( ); 
      } 
      private void printFactoryMethod( List&lt;JClassType&gt; clazzes, 
  SourceWriter sourceWriter ) 
      { 
          sourceWriter.println( ); 
          sourceWriter.println( "public &lt;T&gt; T instantiate( String 
  className ) {" ); 
          for ( JClassType classType : clazzes ) 
          { 
              if ( classType.isAbstract( ) ) 
                  continue; 
              sourceWriter.println( ); 
              sourceWriter.indent( ); 
              sourceWriter.println( "if( className.equals(\"" + 
  classType.getName( ) + "\")) {" ); 
              sourceWriter.indent( ); 
              sourceWriter.println( "return new " + 
  classType.getQualifiedSourceName( ) + "();" ); 
              sourceWriter.outdent( ); 
              sourceWriter.println( "}" ); 
              sourceWriter.outdent( ); 
              sourceWriter.println( ); 
          } 
          sourceWriter.println( ); 
          sourceWriter.outdent( ); 
          sourceWriter.println( "}" ); 
          sourceWriter.outdent( ); 
          sourceWriter.println( ); 
      } 
  } 
  </code></pre>
  
  <p>I want to use it like that: </p>
  
  <p>GWT.create(Reflection.class)).instantiate("MyPanel") </p>
  
  <p>MyPanel class obviously implement 'Constructable' interface. </p>
  
  <p>I keep getting: </p>
  
  <p><strong>Type mismatch: cannot convert from MyPanel to T</strong> </p>
  
  <p>What do you think?</p>
  
  <p><em><strong>edited:</em></strong></p>
  
  <p>I modified this line, trying to do an unchecked cast, but still the error:</p>
  
  <pre><code>sourceWriter.println( "return (T) new " + classType.getQualifiedSourceName( ) + "();"); 
  </code></pre>
  
  <p>And here's my trace:</p>
  
  <pre><code>[DEBUG] [main] - Rebinding com.bilancio.client.Reflection
      [DEBUG] [main] - Adding '1' new generated units
          [DEBUG] [main] - Validating newly compiled units
              [ERROR] [main] - Errors in 'generated://670356D9DA0D417BB8171499B4C4D57B/com/bilancio/client/ReflectionImpl.java'
                  [ERROR] [main] - Line 9: This method must return a result of type T
                  [INFO] [main] - See snapshot: /tmp/com.bilancio.client.ReflectionImpl6488844579312624283.java
  </code></pre>