*/
protected String extractFieldName(TreeLogger logger, JMethod m,
boolean imported) throws UnableToCompleteException {
logger = logger.branch(TreeLogger.DEBUG, "Extracting field name", null);
FieldName fieldNameAnnotation = JSWrapperGenerator.hasTag(logger, m,
FieldName.class);
if (fieldNameAnnotation == null) {
// If the method is imported and there's no overriding annotation,
// just return the methods original name. This ensures that native
// JS methods that look like bean getter/setters don't get munged.
if (imported) {
return m.getName();
}
// If no gwt.fieldName is specified, see if there's a naming policy
// defined on the enclosing class.
JClassType enclosing = m.getEnclosingType();
com.google.gwt.search.jsio.client.NamePolicy namePolicyAnnotation = JSWrapperGenerator.hasTag(
logger, enclosing, com.google.gwt.search.jsio.client.NamePolicy.class);
NamePolicy policy;
// If there is no namePolicy or it's not of the desired form, default
// to the JavaBean-style naming policy.
if (namePolicyAnnotation == null) {
policy = NamePolicy.BEAN;
logger.log(TreeLogger.DEBUG, "No useful policy metadata for class",
null);
} else {
// Use the provided name to access fields within NamePolicy
String policyName = namePolicyAnnotation.value();
try {
Field f = NamePolicy.class.getDeclaredField(policyName.toUpperCase());
policy = (NamePolicy) f.get(null);
} catch (IllegalAccessException e) {
logger.log(TreeLogger.ERROR, "Bad gwt.namePolicy " + policyName, e);
throw new UnableToCompleteException();
} catch (NoSuchFieldException e) {
// This means that the value specified is not a field, but likely
// a class name. Try instantiating one and seeing if it's a
// subclass of NamePolicy.
try {
Class<? extends NamePolicy> clazz = Class.forName(policyName).asSubclass(
NamePolicy.class);
policy = clazz.newInstance();
} catch (ClassCastException ee) {
logger.log(TreeLogger.ERROR,
"@gwt.namePolicy is not an implementation of NamePolicy", null);
throw new UnableToCompleteException();
} catch (ClassNotFoundException ee) {
logger.log(TreeLogger.ERROR, "Bad gwt.namePolicy " + policyName, ee);
throw new UnableToCompleteException();
} catch (IllegalAccessException ee) {
logger.log(TreeLogger.ERROR, "Bad gwt.namePolicy " + policyName, ee);
throw new UnableToCompleteException();
} catch (InstantiationException ee) {
logger.log(TreeLogger.ERROR, "Bad gwt.namePolicy " + policyName, ee);
throw new UnableToCompleteException();
}
}
}
// Execute the conversion
String propertyName = TaskFactory.getPropertyNameFromMethod(m);
return policy.convert(propertyName);
} else {
// Use the field name specified on the method
logger.log(TreeLogger.DEBUG, "Overriding field name based on annotation",
null);
return fieldNameAnnotation.value();
}
}