JClassType sourceType = typeOracle.findType(typeName);
// Ensure that the requested type exists
if (sourceType == null) {
logger.log(TreeLogger.ERROR, "Could not find requested typeName", null);
throw new UnableToCompleteException();
}
// Make sure the Gadget type is correctly defined
validateType(logger, sourceType);
// Pick a name for the generated class to not conflict.
String generatedSimpleSourceName = sourceType.getSimpleSourceName()
+ "GadgetImpl";
// Begin writing the generated source.
ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(
sourceType.getPackage().getName(), generatedSimpleSourceName);
f.addImport(GWT.class.getName());
f.setSuperclass(typeName);
// All source gets written through this Writer
PrintWriter out = context.tryCreate(logger,
sourceType.getPackage().getName(), generatedSimpleSourceName);
// If an implementation already exists, we don't need to do any work
if (out != null) {
JClassType userPrefsType = GadgetUtils.getUserPrefsType(logger,
typeOracle, sourceType);
// We really use a SourceWriter since it's convenient
SourceWriter sw = f.createSourceWriter(context, out);
sw.println("public " + generatedSimpleSourceName + "() {");
sw.indent();
sw.println("nativeInit();");
sw.println("init((" + userPrefsType.getQualifiedSourceName()
+ ")GWT.create(" + userPrefsType.getQualifiedSourceName()
+ ".class));");
sw.outdent();
sw.println("}");
sw.println("private native void nativeInit() /*-{");
sw.indent();
for (JClassType interfaceType : sourceType.getImplementedInterfaces()) {
generateFeatureInitializer(logger, typeOracle, sw, sourceType,
interfaceType);
}
sw.outdent();
sw.println("}-*/;");
// Write out the manifest
OutputStream manifestOut = context.tryCreateResource(logger, typeName
+ ".gadget.xml");
if (manifestOut == null) {
logger.log(TreeLogger.ERROR, "Gadget manifest was already created",
null);
throw new UnableToCompleteException();
}
generateGadgetManifest(logger, typeOracle, sourceType, new PrintWriter(
new OutputStreamWriter(manifestOut)));
context.commitResource(logger, manifestOut);