From: mh Date: Sat, 27 Oct 2001 17:16:00 +0000 (+0000) Subject: added the reflection stuff to call the set* method in each class to set a property X-Git-Tag: prexmlproducerconfig~317 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=bd3068cfd2fb9a7cd0c81571f762f779e3a414e3;p=mir.git added the reflection stuff to call the set* method in each class to set a property --- diff --git a/source/mir/xml/XmlConfigurator.java b/source/mir/xml/XmlConfigurator.java index c82f2231..4b084873 100755 --- a/source/mir/xml/XmlConfigurator.java +++ b/source/mir/xml/XmlConfigurator.java @@ -2,6 +2,7 @@ package mir.xml; import java.io.*; import java.util.*; +import java.lang.reflect.*; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.*; import javax.xml.parsers.ParserConfigurationException; @@ -403,7 +404,13 @@ public class XmlConfigurator { } saxContext.push(tag+":"+name); matchedCount += checkRequiredTag(saxContext); - ///// + + //finally try to set the property + try { + setProperty(classN, name, value); + } catch (Exception e) { + throw new SAXParseException(e.toString(), locator); + } } protected void finished() { @@ -441,4 +448,74 @@ public class XmlConfigurator { return new String(chars); } + /** Find a method with the right name + * If found, call the method ( if param is int or boolean we'll convert + * value to the right type before) - that means you can have setDebug(1). + */ + static void setProperty( Class classN, String name, String value ) + throws Exception { + + String setter= "set" +capitalize(name); + + try { + Method methods[]=classN.getMethods(); + Method setPropertyMethod=null; + + // First, the ideal case - a setFoo( String ) method + for( int i=0; i< methods.length; i++ ) { + Class paramT[]=methods[i].getParameterTypes(); + if( setter.equals( methods[i].getName() ) && + paramT.length == 1 && + "java.lang.String".equals( paramT[0].getName())) { + + methods[i].invoke( null, new Object[] { value } ); + return; + } + } //end for + + // Try a setFoo ( int ) or ( boolean ) + for( int i=0; i< methods.length; i++ ) { + boolean ok=true; + if( setter.equals( methods[i].getName() ) && + methods[i].getParameterTypes().length == 1) { + + // match - find the type and invoke it + Class paramType=methods[i].getParameterTypes()[0]; + Object params[]=new Object[1]; + if ("java.lang.Integer".equals( paramType.getName()) || + "int".equals( paramType.getName())) { + try { + params[0]=new Integer(value); + } catch( NumberFormatException ex ) {ok=false;} + } else if ("java.lang.Boolean".equals( paramType.getName()) || + "boolean".equals( paramType.getName())) { + params[0]=new Boolean(value); + } else { + throw new Exception("Unknown type " + paramType.getName() + "for property \""+name+"\"with value \""+value+"\""); + } + + if( ok ) { + System.out.println("XXX: " + methods[i] + " " + classN + " " + params[0] ); + methods[i].invoke( null, params ); + return; + } //end if + } //end if setter + } //end for + + //if we got this far it means we were not successful in setting the + //property + throw new Exception("Count not find method \""+setter+"\" in Class \""+classN.getName()+"\" in order to set property \""+name+"\""); + + } catch( SecurityException ex1 ) { + throw new Exception("SecurityException for " + classN.getName() + " " + name + "=" + value +")" ); + //if( ctx.getDebug() > 1 ) ex1.printStackTrace(); + } catch (IllegalAccessException iae) { + throw new Exception("IllegalAccessException for " + classN.getName() + " " + name + "=" + value +")" ); + //if( ctx.getDebug() > 1 ) iae.printStackTrace(); + } catch (InvocationTargetException ie) { + throw new Exception("InvocationTargetException for " + classN.getName() + " " + name + "=" + value +")" ); + //if( ctx.getDebug() > 1 ) ie.printStackTrace(); + } + } + }