Here's some of my code:
My module contains:
<generate-with class="com.bilancio.ReflectionGenerator">
<when-type-assignable class="com.bilancio.client.Reflection" />
</generate-with>
public interface Constructable {}
public interface Reflection {
public <T> 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<JClassType> clazzes = new ArrayList<JClassType>( );
PropertyOracle propertyOracle = context.getPropertyOracle( );
for ( JClassType classType : oracle.getTypes( ) )
{
if ( !classType.equals( markerInterface ) &&
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<JClassType> clazzes,
SourceWriter sourceWriter )
{
sourceWriter.println( );
sourceWriter.println( "public <T> 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( );
}
}
I want to use it like that:
GWT.create(Reflection.class)).instantiate("MyPanel")
MyPanel class obviously implement 'Constructable' interface.
I keep getting:
Type mismatch: cannot convert from MyPanel to T
What do you think?
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.