@SupportedAnnotationTypes("com.gwtplatform.dispatch.annotation.GenEvent")
public class GenEventProcessor extends GenProcessor {
@Override
public void process(Element eventElement) {
BuilderGenerationHelper writer = null;
try {
ReflectionHelper reflection = new ReflectionHelper(getEnvironment(), (TypeElement) eventElement);
String eventElementSimpleName = reflection.getSimpleClassName();
String eventSimpleName = eventElementSimpleName + "Event";
String eventClassName = reflection.getClassName() + "Event";
@SuppressWarnings("resource")
Writer sourceWriter = getEnvironment().getFiler().createSourceFile(eventClassName,
eventElement).openWriter();
writer = new BuilderGenerationHelper(sourceWriter);
Collection<VariableElement> orderedElementFields = reflection.getOrderedFields();
Collection<VariableElement> allFields = reflection.getNonConstantFields();
Collection<VariableElement> optionalFields = reflection.getOptionalFields();
Collection<VariableElement> requiredFields = reflection.getNonConstantFields();
requiredFields.removeAll(optionalFields);
writer.generatePackageDeclaration(reflection.getPackageName());
writer.generateImports(
"com.google.gwt.event.shared.EventHandler",
"com.google.gwt.event.shared.GwtEvent",
"com.google.web.bindery.event.shared.HandlerRegistration",
null,
"com.google.gwt.event.shared.HasHandlers"
);
writer.generateClassHeader(eventSimpleName,
"GwtEvent<" + eventSimpleName + "." + eventElementSimpleName + "Handler>",
reflection.getClassRepresenter().getModifiers()
);
writer.generateFieldDeclarations(orderedElementFields);
if (!optionalFields.isEmpty()) { // has optional fields.
writer.setWhitespaces(2);
writer.generateBuilderClass(eventSimpleName, requiredFields, optionalFields);
writer.resetWhitespaces();
if (!requiredFields.isEmpty()) { // and required fields
writer.generateConstructorUsingFields(eventSimpleName, requiredFields, Modifier.PUBLIC);
}
writer.generateCustomBuilderConstructor(eventSimpleName, allFields);
generateFireSelfMethod(writer);
} else if (!requiredFields.isEmpty()) { // has only required fields
writer.generateEmptyConstructor(eventSimpleName, Modifier.PROTECTED);
writer.generateConstructorUsingFields(eventSimpleName, requiredFields, Modifier.PUBLIC);
generateFireFieldsStaticMethod(writer, requiredFields, eventSimpleName);
} else { // has no non-static fields
writer.generateEmptyConstructor(eventSimpleName, Modifier.PUBLIC);
generateFireFieldsStaticMethod(writer, requiredFields, eventSimpleName);
}
generateFireInstanceStaticMethod(writer, eventSimpleName);
generateHasHandlerInterface(writer, eventElementSimpleName);
generateHandlerInterface(writer, eventElementSimpleName);
generateStaticTypeField(writer, eventElementSimpleName);
generateTypeAccessorMethod(writer, eventElementSimpleName);
generateAssociatedTypeMethod(writer, eventElementSimpleName);
generateDispatchMethod(writer, eventElementSimpleName);
writer.generateFieldAccessors(orderedElementFields);
writer.generateEquals(eventSimpleName, orderedElementFields);
writer.generateHashCode(orderedElementFields);
writer.generateToString(eventSimpleName, orderedElementFields);
writer.generateFooter();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (writer != null) {
writer.close();
}
}
}