Search in sources :

Example 1 with CreoleParameter

use of gate.creole.metadata.CreoleParameter 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");
            }
        }
    }
}
Also used : RunTime(gate.creole.metadata.RunTime) PropertyDescriptor(java.beans.PropertyDescriptor) Optional(gate.creole.metadata.Optional) GateException(gate.util.GateException) Element(org.jdom.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) Resource(gate.Resource) CreoleResource(gate.creole.metadata.CreoleResource) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GuiType(gate.creole.metadata.GuiType) HiddenCreoleParameter(gate.creole.metadata.HiddenCreoleParameter) CreoleParameter(gate.creole.metadata.CreoleParameter) Collection(java.util.Collection) HiddenCreoleParameter(gate.creole.metadata.HiddenCreoleParameter)

Example 2 with CreoleParameter

use of gate.creole.metadata.CreoleParameter in project gate-core by GateNLP.

the class CreoleAnnotationHandler method determineCollectionElementType.

/**
 * Given a single-argument method whose parameter is a {@link Collection}, use
 * the method's generic type information to determine the collection element
 * type and store it as the ITEM_CLASS_NAME attribute of the given Element.
 *
 * @param method
 *          the setter method
 * @param paramElt
 *          the PARAMETER element
 */
private void determineCollectionElementType(AnnotatedElement method, Type paramType, Element paramElt) {
    if (paramElt.getAttributeValue("ITEM_CLASS_NAME") == null) {
        Class<?> elementType;
        CreoleParameter paramAnnot = method.getAnnotation(CreoleParameter.class);
        if (paramAnnot != null && paramAnnot.collectionElementType() != CreoleParameter.NoElementType.class) {
            elementType = paramAnnot.collectionElementType();
        } else {
            elementType = findCollectionElementType(paramType);
        }
        if (elementType != null) {
            paramElt.setAttribute("ITEM_CLASS_NAME", elementType.getName());
        }
    }
}
Also used : HiddenCreoleParameter(gate.creole.metadata.HiddenCreoleParameter) CreoleParameter(gate.creole.metadata.CreoleParameter)

Aggregations

CreoleParameter (gate.creole.metadata.CreoleParameter)2 HiddenCreoleParameter (gate.creole.metadata.HiddenCreoleParameter)2 Resource (gate.Resource)1 CreoleResource (gate.creole.metadata.CreoleResource)1 GuiType (gate.creole.metadata.GuiType)1 Optional (gate.creole.metadata.Optional)1 RunTime (gate.creole.metadata.RunTime)1 GateException (gate.util.GateException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 Element (org.jdom.Element)1