2011-08-09 1 views
0

est ici une partie de mon code: Mon module contient:Java Émulation Réflexion avec GWT différé de liaison

<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(); 
    } 
} 

Je veux l'utiliser comme ça:

GWT.create (Reflection.class)). instantiate ("MyPanel")

La classe MyPanel implémente évidemment l'interface 'Constructable'.

Je continue à recevoir:

Incompatibilité de type: ne peut pas convertir MyPanel à T

Que pensez-vous?

modifié:

J'ai modifié cette ligne, en essayant de faire un casting sans contrôle, mais toujours l'erreur:

sourceWriter.println("return (T) new " + classType.getQualifiedSourceName() + "();"); 

Et voici ma trace:

[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 
+0

À quel point obtenez-vous cette erreur? Pendant la compilation de la classe générée? Avez-vous la trace d'exception? –

+0

le projet construit correctement, mais sur le chargement du module l'erreur apparaît, je poste la trace dès que possible! –

+0

que pensez-vous du tahir? –

Répondre