Earlier the list helped me build some methods to include in
an entity bean to set/get field values by name using reflection.
As it turns out, the PropertyDescriptor approach reported
earlier to work doesn't work all the time. I was not able to
pin down exactly what the problem was because it worked
with some methods and not with others with no pattern that
I could see.

Below is code that works as part of an entity bean (ie, "this"
is the bean). The "ReflectException" is my name for an
exception handling class and the "capitalize" utility just make
the first letter of a String capitalized.

I'm using JDK 1.3.1_01 under Solaris 8/SPARC.

Frank

public void set(String propertyName,Object value) throws ReflectException {
     try {
          Class[] classlist = { value.getClass() };
          Method method =
getClass().getDeclaredMethod("set"+Util.capitalize(propertyName),classlist);
          Object[] args = { value };
          method.invoke(this,args);
          }
     catch(NoSuchMethodException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     catch(IllegalAccessException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     catch(InvocationTargetException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     }

public Object get(String propertyName) throws ReflectException {
     try {
          Method method =
getClass().getDeclaredMethod("get"+Util.capitalize(propertyName),null);
          return(method.invoke(this,null));
          }
     catch(NoSuchMethodException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     catch(IllegalAccessException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     catch(InvocationTargetException e) {
          throw new ReflectException("unknown property "+propertyName,e);
          }
     }





_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to