// 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("initializeFeatures();");
sw.println("init((" + userPrefsType.getQualifiedSourceName()
+ ")GWT.create(" + userPrefsType.getQualifiedSourceName()
+ ".class));");
sw.outdent();
sw.println("}");
generateFeatureInitializers(logger, typeOracle, sw, sourceType);
// Write out the manifest
String manifestName = typeName;
if (!GadgetUtils.useLongManifestName(logger, typeOracle, sourceType)) {
int lastIndex = manifestName.lastIndexOf('.');
if (lastIndex != -1) {
manifestName = manifestName.substring(lastIndex + 1);
}
}
OutputStream manifestOut = context.tryCreateResource(logger, manifestName
+ ".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);
sw.commit(logger);
}
return f.getCreatedClassName();
}