Re: FYI: A Simple GWT Generator Example

2010-04-12 Thread Olivier Monaco
Hi Francis,

Using your generator can lead to NPE: your getSourceWriter can returns
null (line 68).

A generator have two goals: returns the name of class to instantiate
and generate the code for that class. The first goal is mandatory, the
second not. If your generator returns the name of a class that already
exists, you don't have to generate the code. This can happen when the
implementation is hand written (like for replacement) or when your
generator is already call for the same type (same call to GWT.create
at different places). To know if the class already exist, check to
returned value of GeneratorContext.tryCreate. A null value indicates
that the class already exists.

Your getSourceWriter do the check and return null if that the case.
But you don't check the returned value of getSourceWriter... Next, you
can try to create the PrintWriter first (before creating the
ClassSourceFileComposerFactory) because you don't it the second if the
first is null...

Here is a better implementation:

@Override
public String generate(TreeLogger logger, GeneratorContext
context,
String typeName)
throws UnableToCompleteException
{
TypeOracle typeOracle = context.getTypeOracle();

// Find JClassType for this interface
JClassType intf = typeOracle.findType(typeName);
if (intf == null) {
logger.log(TreeLogger.ERROR, "Class not found and it's
not"
+ " possible, oups.");
throw new UnableToCompleteException();
}
if (intf.isInterface() == null) {
logger.log(TreeLogger.ERROR, "Not an interface.");
throw new UnableToCompleteException();
}

// Computer package name and class name for the resulting
implementation
String packageName = intf.getPackage().getName();
String className = intf.getSimpleSourceName() + "_TikrayImpl";

// Create the printer
PrintWriter printer = context.tryCreate(logger, packageName,
className);

// If a printer is created, we must write the code
if (printer != null) {
ClassSourceFileComposerFactory composerFactory =
new ClassSourceFileComposerFactory(packageName,
className);
...
}

return packageName + "." + className;
}

Olivier

On 10 avr, 01:56, Francis Shanahan  wrote:
> I had a lot of trouble getting a simple GWT Generator to work so
> here's an example, maybe this will help someone like me in future.
>
> http://francisshanahan.com/index.php/2010/a-simple-gwt-generator-exam...
>
> regards,
> -fs

-- 
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-tool...@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.



FYI: A Simple GWT Generator Example

2010-04-10 Thread Francis Shanahan
I had a lot of trouble getting a simple GWT Generator to work so
here's an example, maybe this will help someone like me in future.

http://francisshanahan.com/index.php/2010/a-simple-gwt-generator-example/

regards,
-fs

-- 
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-tool...@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.