commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Dudziak <tom...@gmail.com>
Subject Re: [BeanUtils] Handling array creation?
Date Thu, 08 Sep 2005 19:22:03 GMT
On 9/8/05, Frank W. Zammetti <fzlists@omnytex.com> wrote:
> I just can't get any love with this :)
> 
> I'm trying to configure a bean manually (forget the whole configuring from
> an XML file for just a minute), and I'm baseing the code on what Niall
> posted this morning... here's what I have:
> 
> public class DFTestBean {
>   private String[] certifications;
>   public void setCertifications(int inIndex, String inValue) {
>     certifications[inIndex] = inValue;
>   }
>   public String getCertifications(int inIndex) {
>     return certifications[inIndex];
>   }
>   public void setCertifications(String[] inArray) {
>     certifications = inArray;
>   }
>   public String[] getCertifications() {
>     return certifications;
>   }
> }
> 
> Then, in another class I'm doing this:
> 
> DFTestBean obj = new DFTestBean();
> try {
>   Object certArray = Array.newInstance(String.class, 3);
>   Converter converter = ConvertUtils.lookup(String.class);
>   Object v;
>   v = converter.convert(String.class, "CNE");
>   Array.set(certArray, 0, v);
>   v = converter.convert(String.class, "SCJD");
>   Array.set(certArray, 1, v);
>   v = converter.convert(String.class, "INETP");
>   Array.set(certArray, 2, v);
>   BeanUtils.setProperty(obj, "certifications", certArray);
> } catch (IllegalAccessException iae) {
>   iae.printStackTrace();
> } catch (InvocationTargetException ite) {
>   ite.printStackTrace();
> }

The problem is the indexed setter, it confuses the BeanUtils class,
probably because it (in contrast to PropertyUtils) tries to convert
the value to the type expected by the setter method. Since you know
the target type already, you could use PropertyUtils instead.

Tom

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


Mime
View raw message