Search in sources :

Example 21 with GateException

use of gate.util.GateException 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)

Example 22 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleAnnotationHandler method processAnnotationsForResource.

/**
 * Process the given RESOURCE element, adding extra elements to it based on
 * the annotations on the resource class.
 *
 * @param element
 *          the RESOURCE element to process.
 */
private void processAnnotationsForResource(Element element) throws GateException {
    String className = element.getChildTextTrim("CLASS");
    if (className == null) {
        throw new GateException("\"CLASS\" element not found for resource in " + plugin);
    }
    Class<?> resourceClass = null;
    try {
        resourceClass = Gate.getClassLoader().loadClass(className);
    } catch (ClassNotFoundException e) {
        log.debug("Couldn't load class " + className + " for resource in " + plugin, e);
        throw new GateException("Couldn't load class " + className + " for resource in " + plugin);
    }
    processCreoleResourceAnnotations(element, resourceClass);
}
Also used : GateException(gate.util.GateException)

Example 23 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleRegisterImpl method parseDirectory.

/**
 * Parse a directory file (represented as an open stream), adding resource
 * data objects to the CREOLE register as they occur. If the resource is from
 * a URL then that location is passed (otherwise null).
 */
protected void parseDirectory(Plugin plugin, Document jdomDoc, URL directoryUrl, URL creoleFileUrl) throws GateException {
    // this will create ResourceData entries in the register
    try {
        CreoleAnnotationHandler annotationHandler = new CreoleAnnotationHandler(plugin);
        GateClassLoader gcl = Gate.getClassLoader().getDisposableClassLoader(creoleFileUrl.toExternalForm());
        // Add any JARs from the creole.xml to the GATE ClassLoader
        annotationHandler.addJarsToClassLoader(gcl, jdomDoc);
        // Make sure there is a RESOURCE element for every resource type the
        // directory defines
        annotationHandler.createResourceElementsForDirInfo(jdomDoc);
        processFullCreoleXmlTree(plugin, jdomDoc, annotationHandler);
    } catch (IOException e) {
        throw (new GateException(e));
    } catch (JDOMException je) {
        if (DEBUG)
            je.printStackTrace(Err.getPrintWriter());
        throw (new GateException(je));
    }
}
Also used : GateException(gate.util.GateException) GateClassLoader(gate.util.GateClassLoader) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException)

Aggregations

GateException (gate.util.GateException)23 IOException (java.io.IOException)11 URL (java.net.URL)10 GateRuntimeException (gate.util.GateRuntimeException)8 MalformedURLException (java.net.MalformedURLException)8 Resource (gate.Resource)5 JDOMException (org.jdom.JDOMException)5 Plugin (gate.creole.Plugin)3 InputStream (java.io.InputStream)3 LanguageResource (gate.LanguageResource)2 ProcessingResource (gate.ProcessingResource)2 VisualResource (gate.VisualResource)2 ResourceInstantiationException (gate.creole.ResourceInstantiationException)2 CreoleResource (gate.creole.metadata.CreoleResource)2 GateClassLoader (gate.util.GateClassLoader)2 BeanInfo (java.beans.BeanInfo)2 IntrospectionException (java.beans.IntrospectionException)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 File (java.io.File)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2