If want to access the public methods of a class
you should use getMethod() instead of getDeclaredMethod().
getMethod() returns the public method in the
class or in one of its supperclasses
getDeclaredMethod() returns only the method from the class where it
is defined it also returns private,protected or default scope methods.

Tbone


----- Original Message -----
From: "Frank Morton" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, October 31, 2001 8:35 PM
Subject: [JBoss-user] setter/getter methods by name solution


> 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().getMethod("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().getMethod("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
>


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

Reply via email to