Hi All,

I've been doing some research into Class.forName and realize that it's
not supported for some very good reasons. But in my google-fuing I
came across a fellow that seemed to, in a previous version of GWT have
come up with a possible solution using a Generator. I wanted to run
the code by the people here to see what they thought as to it's
viability in the current GWT landscape as I've never used Generators
and this solution was purportedly for pre 1.5. This was originally
taken from (http://programmatica.blogspot.com/2007/10/
classfornamefullyqualifiedclassnamenewi.html) and all credit or flames
of the code should be directed that way ;-)

It starts with a few Interfaces:

Factory:

    package com.test2.shared;

    import com.test2.client.Instantiable;

    public interface Factory {
             Instantiable newInstance(String className);
    }

ReflectiveFactory

  package com.test2.client;

  import com.test2.shared.FactoryWrapper;

  public class ReflectiveFactory implements FactoryWrapper{

  }

Both FactoryWrapper and Instantiable & just marker interfaces.

The accompanying XML from the .gwt.xml is as follows:
    <generate-with class="com.test2.gwt.FactoryGenerator" >
        <when-type-assignable class="com.test2.shared.FactoryWrapper" />
    </generate-with>

And the class that actually get's things done, FactoryGenerator:

package com.test2.gwt;

 import java.io.PrintWriter;

import com.google.gwt.core.ext.Generator;
 import com.google.gwt.core.ext.GeneratorContext;
 import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.google.gwt.core.ext.typeinfo.NotFoundException;
 import com.google.gwt.core.ext.typeinfo.TypeOracle;
 import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;


public class FactoryGenerator extends Generator{

        @Override
        public String generate(TreeLogger logger, GeneratorContext
context,String typeName)  throws UnableToCompleteException{
                // Begin with a starting log line.
                logger.log(TreeLogger.INFO, "Generatoring source for " + 
typeName,
null);

                //Retrieve the Type Oracle
                TypeOracle typeOracle = context.getTypeOracle();

                // Find the Type
                JClassType clazz = typeOracle.findType(typeName);

                // Catch if the class isn't found
                if (clazz == null){
                        logger.log(TreeLogger.ERROR, "Unable to find metadata 
for type'" +
typeName + "'",null);
                        throw new UnableToCompleteException();
                }

                try{
                        logger.log(TreeLogger.INFO, "Generating source for " +
clazz.getQualifiedSourceName(),null);

                        JClassType reflectableType =
typeOracle.getType("com.test2.client.Instantiable");

                        SourceWriter sourceWriter = getSourceWriter(clazz, 
context,
logger);

                        if (sourceWriter != null){
                                sourceWriter.println("public "
                                                                        + 
reflectableType.getQualifiedSourceName()
                                                                        + " 
newInstance(String className) {");

                                JClassType[] types = typeOracle.getTypes();
                                int count = 0;
                                for (int i = 0; i < types.length; i++){
                                        if (types[i].isInterface() == null &&
types[i].isAssignableTo(reflectableType)){
                                                if (count == 0){
                                                        sourceWriter.println("  
  if (\""
                                                                                
                + types[i].getQualifiedSourceName()
                                                                                
                + "\".equals(className)) { "
                                                                                
                + " return new "
                                                                                
                + types[i].getQualifiedSourceName() + "();"
                                                                                
                + "}");
                                                }else{
                                                        sourceWriter.println("  
  else if (\""
                                                                                
                + types[i].getQualifiedSourceName()
                                                                                
                + "\".equals(className)) {"
                                                                                
                + " return new "
                                                                                
                + types[i].getQualifiedSourceName() + "();"
                                                                                
                + "}");
                                                }
                                                count++;
                                        }
                                }
                                sourceWriter.println("return null;");
                                sourceWriter.println("}");
                                sourceWriter.commit(logger);
                                logger.log(TreeLogger.INFO, "Done Generating 
source for " +
clazz.getName(), null);

                                return clazz.getQualifiedSourceName() + 
"Wrapper";
                        }


                }catch(NotFoundException nfe){
                        nfe.printStackTrace();
                }

                return clazz.getQualifiedSourceName() + "Wrapper";
        }

        public SourceWriter getSourceWriter(JClassType classType,
GeneratorContext context, TreeLogger logger){

                String packageName = classType.getPackage().getName();

                String simpleName = classType.getSimpleSourceName() + "Wrapper";

                ClassSourceFileComposerFactory composer = new
ClassSourceFileComposerFactory(packageName, simpleName);

                composer.addImplementedInterface("com.test2.shared.Factory");

                PrintWriter printWriter = context.tryCreate(logger, packageName,
simpleName);

                if (printWriter == null) {

                        return null;

                } else {

                        SourceWriter sw = composer.createSourceWriter(context,
printWriter);

                        return sw;

                }

        }

}


For usage it's supposed to go something like this:

Factory factory = (Factory) GWT.create(ReflectiveFactory.class);
factory.newInstance("fully.qualified.Classname");

Thoughts, comments? I've been trying to get this up and running for a
bit now and while cool and interesting if it's an ultimately flawed
approach in the current GWT environment I'd rather not spend any more
time on it.

Thanks much in advance,

Ryan

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to