Recursive Reflection and Exceptionhandling
I'm processing an XML-file including the Element <Content> and others. To process i use reflection so my methods are recursive automaticaly called. The disadvantage is that every thrown exception ends in a InvocationTargetException in the root. So the excetions are not very usefull. Can i get the real thrown exception via reflection. Is there any design trick I can use?
publicvoid addContent(Node node, Object obj, Class theClass)throws Exception
{
String value= ((Element)node).getAttribute("value");
if(value.equals(""))
{
thrownew MyException("Value-Attribute is not optional", node);
}else{
System.out.println(value);
}
processChildElements(node, paramObj, paramClass);
}// addContent
privatevoid processChildElements(Node node, Object obj, Class cl)throws Exception
{
if(((Element)node).getElementsByTagName("*") !=null)
{
Node childNode =null;
//process child elements
NodeList nlChild = ((Element)node).getChildNodes();
for(int i=0; i<nlChild.getLength(); i++)
{
childNode = nlChild.item(i);
if(childNode.getNodeType() == Node.ELEMENT_NODE){
String nodeName = childNode.getNodeName().trim();
Method method = this.getClass().getMethod("add" + nodeName,new Class[]{
org.w3c.dom.Node.class,
java.lang.Object.class,
java.lang.Class.class});
method.invoke((Object)this,new Object[]{childNode, obj, cl});
}
}
}
}
>

