* Creates an ExternalResourceDependency for a field annotated with
* {@link org.apache.uima.fit.descriptor.ExternalResource}.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static ExternalResourceDependency createExternalResourceDependency(Field field) {
ExternalResource era = ReflectionUtil.getAnnotation(field, ExternalResource.class);
// Get the binding key for the specified field. If no key is set, use the field name as key.
String key = era.key();
if (key.length() == 0) {
key = field.getName();
}
// Get the type of class/interface a resource has to implement to bind to the annotated field.
// If no API is set, get it from the annotated field type.
Class<? extends Resource> api = era.api();
// If no api is specified, look at the annotated field
if (api == Resource.class) {
if (Resource.class.isAssignableFrom(field.getType())
|| SharedResourceObject.class.isAssignableFrom(field.getType())) {
// If no API is set, check if the field type is already a resource type
api = (Class<? extends Resource>) field.getType();
} else {
// If the field does not have a resource type, assume whatever. This allows to use
// a resource locator without having to specify the api parameter. It also allows
// to directly inject Java objects - yes, I know that Object does not extend
// Resource - REC, 2011-03-25
api = (Class) Object.class;
}
}
return ExternalResourceFactory.createExternalResourceDependency(key, api, !era.mandatory(),
era.description());
}