You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2001/03/04 04:37:16 UTC
cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util IntrospectionUtils.java
costin 01/03/03 19:37:16
Modified: src/share/org/apache/tomcat/util IntrospectionUtils.java
Log:
Added few more introspection tools.
Revision Changes Path
1.5 +106 -0 jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java
Index: IntrospectionUtils.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/IntrospectionUtils.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- IntrospectionUtils.java 2001/01/20 21:44:39 1.4
+++ IntrospectionUtils.java 2001/03/04 03:37:16 1.5
@@ -191,4 +191,110 @@
}
return null;
}
+
+
+ /** 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).
+ */
+ public static void setProperty( Object o, String name, String value )
+ {
+ if( dbg > 1 ) d("setProperty(" +
+ o.getClass() + " " + name + "=" +
+ value +")" );
+
+ String setter= "set" +capitalize(name);
+
+ try {
+ Method methods[]=o.getClass().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( o, new Object[] { value } );
+ return;
+ }
+ }
+
+ // 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 {
+ d("Unknown type " + paramType.getName() );
+ }
+
+ if( ok ) {
+ methods[i].invoke( o, params );
+ return;
+ }
+ }
+
+ // save "setProperty" for later
+ if( "setProperty".equals( methods[i].getName())) {
+ setPropertyMethod=methods[i];
+ }
+ }
+
+ // Ok, no setXXX found, try a setProperty("name", "value")
+ if( setPropertyMethod != null ) {
+ Object params[]=new Object[2];
+ params[0]=name;
+ params[1]=value;
+ setPropertyMethod.invoke( o, params );
+ }
+
+ } catch( SecurityException ex1 ) {
+ if( dbg > 0 )
+ d("SecurityException for " + o.getClass() + " " +
+ name + "=" + value +")" );
+ if( dbg > 1 ) ex1.printStackTrace();
+ } catch (IllegalAccessException iae) {
+ if( dbg > 0 )
+ d("IllegalAccessException for " +
+ o.getClass() + " " + name + "=" + value +")" );
+ if( dbg > 1 ) iae.printStackTrace();
+ } catch (InvocationTargetException ie) {
+ if( dbg > 0 )
+ d("InvocationTargetException for " + o.getClass() +
+ " " + name + "=" + value +")" );
+ if( dbg > 1 ) ie.printStackTrace();
+ }
+ }
+
+ /** Reverse of Introspector.decapitalize
+ */
+ public static String capitalize(String name) {
+ if (name == null || name.length() == 0) {
+ return name;
+ }
+ char chars[] = name.toCharArray();
+ chars[0] = Character.toUpperCase(chars[0]);
+ return new String(chars);
+ }
+
+ // debug --------------------
+ static final int dbg=0;
+ static void d(String s ) {
+ System.out.println("IntrospectionUtils: " + s );
+ }
}