use of gate.creole.metadata.RunTime in project gate-core by GateNLP.
the class CreoleAnnotationHandler method processElement.
/**
* Processes parameter annotations on a single element. Can support both Field
* and Method elements.
*
* @param element
* The Method of Field from which to discern parameters
* @param bi
* the BeanInfo for the corresponding class.
* @param resourceElement
* the RESOURCE element to which the PARAMETERs are to be added
* @param parameterMap
* a map from parameter names to the PARAMETER elements that define
* them. This is used as we combine information from the original
* creole.xml, the parameter annotation on the target method and the
* annotations on the same method of its superclasses and interfaces.
* Parameter names that have been hidden by a
* {@link HiddenCreoleParameter} annotation are explicitly mapped to
* <code>null</code> in this map.
* @param disjunctionMap
* a map from disjunction IDs to the OR elements that define them.
* Disjunctive parameters are handled by specifying a disjunction ID
* on the {@link CreoleParameter} annotations - parameters with the
* same disjunction ID are grouped under the same OR element.
*/
private void processElement(AnnotatedElement element, BeanInfo bi, Element resourceElement, Map<String, Element> parameterMap, Map<String, Element> disjunctionMap) throws GateException {
CreoleParameter paramAnnot = element.getAnnotation(CreoleParameter.class);
HiddenCreoleParameter hiddenParamAnnot = element.getAnnotation(HiddenCreoleParameter.class);
// Extracted name of this parameter
String paramName;
Class<?> paramType;
// The type of this parameter.
Type genericParamType;
if (paramAnnot != null || hiddenParamAnnot != null) {
// Enforce constraints relevant for this type of element
if (element.getClass().equals(java.lang.reflect.Field.class)) {
java.lang.reflect.Field field = (java.lang.reflect.Field) element;
paramName = field.getName();
genericParamType = field.getGenericType();
paramType = field.getType();
PropertyDescriptor paramDescriptor = null;
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
if (paramName.equals(pd.getName())) {
paramDescriptor = pd;
break;
}
}
if (paramDescriptor == null) {
throw new GateException("CREOLE parameter annotation found on field " + field + " but no corresponding JavaBean accessor methods exist.");
} else if (paramDescriptor.getReadMethod() == null || paramDescriptor.getWriteMethod() == null) {
throw new GateException("CREOLE parameter annotation found on field " + field + " but getter or setter is missing. CREOLE parameters require both.");
}
} else if (element.getClass().equals(Method.class)) {
Method method = (Method) element;
// Extract the parameter name from the BeanInfo
PropertyDescriptor paramDescriptor = null;
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
if (method.equals(pd.getWriteMethod())) {
paramDescriptor = pd;
break;
}
}
if (paramDescriptor == null) {
throw new GateException("CREOLE parameter annotation found on " + method + " but this method is not a Java Bean property setter.");
}
paramName = paramDescriptor.getName();
// And the type is that of the first argument
genericParamType = method.getGenericParameterTypes()[0];
paramType = method.getParameterTypes()[0];
} else {
throw new GateException("CREOLE parameter annotation found on " + element + " but can only be placed on Method or Field");
}
// Hidden parameters can be added straight to the map.
if (hiddenParamAnnot != null && !parameterMap.containsKey(paramName)) {
parameterMap.put(paramName, null);
}
// Visible parameters need converting to JDOM Elements
if (paramAnnot != null) {
Element paramElt = null;
if (parameterMap.containsKey(paramName)) {
// Use existing annotation if there is such a thing.
paramElt = parameterMap.get(paramName);
} else {
// Otherwise create on - type depends on whether it is disjunctive or
// not.
paramElt = new Element("PARAMETER").setAttribute("NAME", paramName);
if (!"".equals(paramAnnot.disjunction())) {
// Disjunctive parameters (cannot both be set) need special markup.
Element disjunctionElt = disjunctionMap.get(paramAnnot.disjunction());
if (disjunctionElt == null) {
disjunctionElt = new Element("OR");
resourceElement.addContent(disjunctionElt);
disjunctionMap.put(paramAnnot.disjunction(), disjunctionElt);
}
disjunctionElt.addContent(paramElt);
} else {
resourceElement.addContent(paramElt);
}
parameterMap.put(paramName, paramElt);
}
if (paramElt != null) {
// which has not been masked by a @HiddenCreoleParameter
if (paramElt.getTextTrim().length() == 0) {
// The text of the element should be the the type
paramElt.setText(paramType.getName());
// the item type.
if ((!Resource.class.isAssignableFrom(paramType)) && Collection.class.isAssignableFrom(paramType)) {
determineCollectionElementType(element, genericParamType, paramElt);
}
}
// other attributes
addAttribute(paramElt, paramAnnot.comment(), "", "COMMENT");
addAttribute(paramElt, paramAnnot.suffixes(), "", "SUFFIXES");
addAttribute(paramElt, paramAnnot.defaultValue(), CreoleParameter.NO_DEFAULT_VALUE, "DEFAULT");
addAttribute(paramElt, String.valueOf(paramAnnot.priority()), String.valueOf(CreoleParameter.DEFAULT_PRIORITY), "PRIORITY");
// runtime and optional are based on marker annotations
String runtimeParam = "";
if (element.isAnnotationPresent(RunTime.class)) {
runtimeParam = String.valueOf(element.getAnnotation(RunTime.class).value());
}
addAttribute(paramElt, runtimeParam, "", "RUNTIME");
String optionalParam = "";
if (element.isAnnotationPresent(Optional.class)) {
optionalParam = String.valueOf(element.getAnnotation(Optional.class).value());
}
addAttribute(paramElt, optionalParam, "", "OPTIONAL");
}
}
}
}
Aggregations