elem, writer);
final Map<String, String> requiredValues = new HashMap<String, String>();
final Map<String, JType> unfilledRequiredParams = new HashMap<String, JType>();
final OwnerFieldClass ownerFieldClass = OwnerFieldClass.getFieldClass(type,
writer.getLogger(), context);
/*
* Handle @UiFactory and @UiConstructor, but only if the user
* hasn't provided an instance via @UiField(provided = true)
*/
JAbstractMethod creator = null;
OwnerField uiField = writer.getOwnerClass().getUiField(fieldName);
if ((uiField == null) || (!uiField.isProvided())) {
// See if there's a factory method
creator = writer.getOwnerClass().getUiFactoryMethod(type);
if (creator == null) {
// If not, see if there's a @UiConstructor
creator = ownerFieldClass.getUiConstructor();
}
if (creator != null) {
for (JParameter param : creator.getParameters()) {
unfilledRequiredParams.put(param.getName(), param.getType());
}
}
}
// Work through the localized attribute values and assign them
// to appropriate constructor params or setters (which had better be
// ready to accept strings)
for (Entry<String, String> property : localizedValues.entrySet()) {
String key = property.getKey();
String value = property.getValue();
JType paramType = unfilledRequiredParams.get(key);
if (paramType != null) {
if (!isString(writer, paramType)) {
writer.die(elem,
"In %s, cannot apply message attribute to non-string "
+ "constructor argument %s.",
paramType.getSimpleSourceName(), key);
}
requiredValues.put(key, value);
unfilledRequiredParams.remove(key);
} else {
JMethod setter = ownerFieldClass.getSetter(key);
JParameter[] params = setter == null ? null : setter.getParameters();
if (setter == null || !(params.length == 1)
|| !isString(writer, params[0].getType())) {
writer.die(elem, "No method found to apply message attribute %s", key);
} else {
setterValues.put(key, value);
}
}
}
// Now go through the element and dispatch its attributes, remembering
// that constructor arguments get first dibs
for (int i = elem.getAttributeCount() - 1; i >= 0; i--) {
// Backward traversal b/c we're deleting attributes from the xml element
XMLAttribute attribute = elem.getAttribute(i);
// Ignore xmlns attributes
if (attribute.getName().startsWith("xmlns:")) {
continue;
}
String propertyName = attribute.getLocalName();
if (setterValues.keySet().contains(propertyName)
|| requiredValues.containsKey(propertyName)) {
writer.die(elem, "Duplicate attribute name: %s", propertyName);
}
if (unfilledRequiredParams.keySet().contains(propertyName)) {
JType paramType = unfilledRequiredParams.get(propertyName);
String value = elem.consumeAttributeWithDefault(attribute.getName(),
null, paramType);
if (value == null) {
writer.die(elem, "Unable to parse %s as constructor argument "
+ "of type %s", attribute, paramType.getSimpleSourceName());
}
requiredValues.put(propertyName, value);
unfilledRequiredParams.remove(propertyName);
} else {
JMethod setter = ownerFieldClass.getSetter(propertyName);
if (setter == null) {
writer.die(elem, "Class %s has no appropriate set%s() method",
elem.getLocalName(), initialCap(propertyName));
}
String n = attribute.getName();