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);
}
}
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);
}
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));
}
}
Aggregations