Search in sources :

Example 1 with Sharable

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

the class ResourceData method determineSharableProperties.

private void determineSharableProperties(Class<?> cls, Collection<String> hiddenPropertyNames) throws GateException {
    BeanInfo bi;
    try {
        bi = Introspector.getBeanInfo(cls);
    } catch (IntrospectionException e) {
        throw new GateException("Failed to introspect " + cls, e);
    }
    // setter methods
    for (Method m : cls.getDeclaredMethods()) {
        Sharable sharableAnnot = m.getAnnotation(Sharable.class);
        if (sharableAnnot != null) {
            // determine the property name from the method name
            PropertyDescriptor propDescriptor = null;
            for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                if (m.equals(pd.getWriteMethod())) {
                    propDescriptor = pd;
                    break;
                }
            }
            if (propDescriptor == null) {
                throw new GateException("@Sharable annotation found on " + m + ", but only Java Bean property setters may have " + "this annotation.");
            }
            if (propDescriptor.getReadMethod() == null) {
                throw new GateException("@Sharable annotation found on " + m + ", but no matching getter was found.");
            }
            String propName = propDescriptor.getName();
            if (sharableAnnot.value() == false) {
                // hide this property name from the search in superclasses
                hiddenPropertyNames.add(propName);
            } else {
                // this property is sharable if it has not been hidden in a subclass
                if (!hiddenPropertyNames.contains(propName)) {
                    sharableProperties.add(propName);
                }
            }
        }
    }
    // fields
    for (Field f : cls.getDeclaredFields()) {
        Sharable sharableAnnot = f.getAnnotation(Sharable.class);
        if (sharableAnnot != null) {
            String propName = f.getName();
            // check it's a valid Java Bean property
            PropertyDescriptor propDescriptor = null;
            for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                if (propName.equals(pd.getName())) {
                    propDescriptor = pd;
                    break;
                }
            }
            if (propDescriptor == null || propDescriptor.getReadMethod() == null || propDescriptor.getWriteMethod() == null) {
                throw new GateException("@Sharable annotation found on " + f + " without matching Java Bean getter and setter.");
            }
            if (sharableAnnot.value() == false) {
                // hide this property name from the search in superclasses
                hiddenPropertyNames.add(propName);
            } else {
                // this property is sharable if it has not been hidden in a subclass
                if (!hiddenPropertyNames.contains(propName)) {
                    sharableProperties.add(propName);
                }
            }
        }
    }
    // go up the class tree
    if (cls.getSuperclass() != null) {
        determineSharableProperties(cls.getSuperclass(), hiddenPropertyNames);
    }
    for (Class<?> intf : cls.getInterfaces()) {
        determineSharableProperties(intf, hiddenPropertyNames);
    }
}
Also used : Field(java.lang.reflect.Field) Sharable(gate.creole.metadata.Sharable) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) GateException(gate.util.GateException) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Aggregations

Sharable (gate.creole.metadata.Sharable)1 GateException (gate.util.GateException)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1