You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Henning Schmiedehausen <hp...@intermeta.de> on 2003/10/14 10:48:40 UTC

[BEANUTILS] Property Descriptor Visibility

Hi,

consider the following beans:

--- cut ---
package beans;

abstract class AbstractBean {
  private String foo = null;

  public AbstractBean() {}

  public String getFoo() { return this.foo; };

  public void setFoo(String foo) { this.foo = foo; };
}
--- cut ---

--- cut ---
package beans;

public class PublicBean
  extends AbstractBean {
  private String bar = null;

  public PublicBeanBean() { super(); }

  public String getBar() { return this.bar; };

  public void setBar(String bar) { this.bar = bar; };
}
--- cut ---

import beans.PublicBean;
[...]
PublicBean bean = new PublicBean();
BeanUtils.setSimpleProperty(bean,  "foo", "value");

fails. The reason for this is the Property visibility check in the
MethodUtils::getAccessibleMethod, which considers the "setFoo/getFoo"
Property setters to be inaccessible because they're not defined in a
public class and not defined by any Interface implemented by the Bean.

My question now is: Is this correct? The public visibility of the
PublicBean class makes IMHO the public methods AbstractBean::setFoo()
and AbstractBean::getFoo() accessible, even if the declaring class
(AbstractBean) is not public.

This is not an academic question, it is exactly the problem I've
written about yesterday with commons-dbcp. There is "public
SharedPoolDataSource" which extends "abstract
InstanceKeyDataSource". One cannot set properties defined in
InstanceKeyDataSource with PropertyUtils because of this problem.

I'd be interested if the correct solution is to add a "public" to
InstanceKeyDataSource or rewrite the 
MethodUtils::getAccessibleMethod().

        Regards
                Henning


-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

Java, perl, Solaris, Linux, xSP Consulting, Web Services 
freelance consultant -- Jakarta Turbine Development  -- hero for hire

"Dominate!! Dominate!! Eat your young and aggregate! I have grotty silicon!" 
      -- AOL CD when played backwards  (User Friendly - 200-10-15)


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [BEANUTILS] Property Descriptor Visibility

Posted by Thomas Plümpe <th...@gmx.de>.
> >My workaround was to set the class to public and document that it isn't
> >really. Obviously I'd much prefer having getAccessibleMethod rewritten,
> >but am not quite sure how to do that correctly.
> 
> The requirement for a public class is a limitation of introspection in 
> Java that BeanUtils inherits from java.beans.Introspector.  The logic in 
> getAccessibleMethod() took a long time to get to its current state (it 
> includes workarounds for bugs on some JVMs),
I see. I noticed there appears to be a bit of black magic going on. ;)

>  and I'm hesitant to mess 
> with it without a *substantial* suite of test cases to verify we don't 
> break anything.  Unfortunately, I don't have time to work on such a 
> suite at the moment :-(.
> 
> One workaround to exposing more public properties than you really want 
> (which has also been discussed on this thread) is to use a BeanInfo 
> class to describe your bean class.  Using this technique, you can tell 
> the introspector exactly which properties to expose (and even use 
> different method names than the usual design pattern requirements, if 
> you want).  See the JavaBeans Specification for more information:
> 
>   http://java.sun.com/products/javabeans/docs/
Thanks, but I guess for now I'll just keep the superclass public.

Thomas



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [BEANUTILS] Property Descriptor Visibility

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Thomas Plümpe wrote:

>Hi Henning,
>
>[can't set property if accessors defined in friendly superclass]
>  
>
>>This is not an academic question, it is exactly the problem I've
>>written about yesterday with commons-dbcp. There is "public
>>SharedPoolDataSource" which extends "abstract
>>InstanceKeyDataSource". One cannot set properties defined in
>>InstanceKeyDataSource with PropertyUtils because of this problem.
>>    
>>
>Right, that's certainly not an academic question. I encountered the same
>problem recently when I wanted to have a common package-private
>superclass for some beans, but didn't want to have this superclass
>exposed in the public interface of that package as it is really just an
>implementation detail: At some point I may have to reimplement the whole
>package (ideally have the classes generated) in which case the
>superclass might vanish.
>
>  
>
>>I'd be interested if the correct solution is to add a "public" to
>>InstanceKeyDataSource or rewrite the 
>>MethodUtils::getAccessibleMethod().
>>    
>>
>My workaround was to set the class to public and document that it isn't
>really. Obviously I'd much prefer having getAccessibleMethod rewritten,
>but am not quite sure how to do that correctly.
>  
>

The requirement for a public class is a limitation of introspection in 
Java that BeanUtils inherits from java.beans.Introspector.  The logic in 
getAccessibleMethod() took a long time to get to its current state (it 
includes workarounds for bugs on some JVMs), and I'm hesitant to mess 
with it without a *substantial* suite of test cases to verify we don't 
break anything.  Unfortunately, I don't have time to work on such a 
suite at the moment :-(.

One workaround to exposing more public properties than you really want 
(which has also been discussed on this thread) is to use a BeanInfo 
class to describe your bean class.  Using this technique, you can tell 
the introspector exactly which properties to expose (and even use 
different method names than the usual design pattern requirements, if 
you want).  See the JavaBeans Specification for more information:

  http://java.sun.com/products/javabeans/docs/


>Thomas
>  
>

Craig



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [BEANUTILS] Property Descriptor Visibility

Posted by Thomas Plümpe <th...@gmx.de>.
Hi Henning,

[can't set property if accessors defined in friendly superclass]
> This is not an academic question, it is exactly the problem I've
> written about yesterday with commons-dbcp. There is "public
> SharedPoolDataSource" which extends "abstract
> InstanceKeyDataSource". One cannot set properties defined in
> InstanceKeyDataSource with PropertyUtils because of this problem.
Right, that's certainly not an academic question. I encountered the same
problem recently when I wanted to have a common package-private
superclass for some beans, but didn't want to have this superclass
exposed in the public interface of that package as it is really just an
implementation detail: At some point I may have to reimplement the whole
package (ideally have the classes generated) in which case the
superclass might vanish.

> I'd be interested if the correct solution is to add a "public" to
> InstanceKeyDataSource or rewrite the 
> MethodUtils::getAccessibleMethod().
My workaround was to set the class to public and document that it isn't
really. Obviously I'd much prefer having getAccessibleMethod rewritten,
but am not quite sure how to do that correctly.

Thomas



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org