copyProperty of BeanUtil class

I see the BeanUtil copyProperty  documentation here..
http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#copyProperty%28java.lang.Object,%20java.lang.String,%20java.lang.Object%29

 I'm unable to set Boolean object using BeanUtils  copyProperty.

Here is my code:
// myType is  a Boolean field
BeanUtils.copyProperty(myVo,"myVo.myInfo.myType", new Boolean(true)); 

Open in new window


// This does not create myInfo instance even !   it shows myInfo=null

How do I  set Boolean(true) using copyProperty then ?

Whats the workaround for this ?
cofactorAsked:
Who is Participating?
 
for_yanCommented:


Follow the instruction in this API
http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#copyProperty%28java.lang.Object,%20java.lang.String,%20java.lang.Object%29

 to read more in BeanUtilsBean
and in BeanUtilsBeans you read:

http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtilsBean.html#copyProperty%28java.lang.Object,%20java.lang.String,%20java.lang.Object%29

"If the specified bean does not have a property of the specified name, or the property is read only on the destination bean, return without doing anything. "
0
 
CEHJCommented:
Are you completely sure that it's a bean you've got?
0
 
cofactorAuthor Commented:
>>Are you completely sure that it's a bean you've got?

Yes.  

Is that working in your system ?   If yes, please post the sample so that I can test it in my system here.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
CEHJCommented:
Can you attach the source for your bean please?
0
 
cofactorAuthor Commented:
>>>Can you attach the source for your bean please?

Well, here is the code. I am posting the relevant piece of code that you may be interested in.
code.txt
0
 
cofactorAuthor Commented:
comments please.
0
 
CEHJCommented:
It's the full code of FinanceInfoVO and ProposalVO that i need to see
0
 
for_yanCommented:

I am wondering, do you understand what this method copyProperty() is supposed to do?

I have a good example which I downoloaded from here:

http://www.avajava.com/tutorials/lessons/how-do-i-copy-properties-from-one-bean-to-another.html?page=1

and nicely tested, which shows how properties of one bean can be copied to properties of another bean
using copyProperties(fromBean, toBean) method (just previos one in the API)
This one really works exactly as expected copying those properties which exist in destination and ignoring
those properties which do not exist in the destination bean.

Now looking at this copyProperty() API description and browsing on the internet
(where this method is obviously much less popular)
I'm trying to understand what it is supposed to do?

Ok, it takes destination bean as parameter,
but from where is it  supposed to copy the property?
And if it copies the property, why you need to provide the value?
And then maybe it should be called setProperty in this case?
And it does not work in this way as a setter - I tried it both with existing or non-existent property and it had no effect in this way.

So I cannot understand it.
I see you are working a lot with beans.
Perhaps you can explain to me, what it is that I'm missing.




0
 
CEHJCommented:
Not sure what the problem is - the following worked fine for me right away ...
import org.apache.commons.beanutils.BeanUtils;

public class Beans {
    public static void main(String[] args) throws Exception {
        B1 b1 = new B1();
        b1.setX(true);

        B2 b2 = new B2();
        BeanUtils.copyProperty(b2, "y", b1.getX());
        System.out.println(b2);
    }
}

==========================================================
public class B1 {
    private boolean x;

    public boolean getX() {
        return this.x;
    }

    public void setX(boolean x) {
        this.x = x;
    }
}

==========================================================

public class B2 {
    private Boolean y;

    public Boolean getY() {
        return this.y;
    }

    public void setY(Boolean y) {
        this.y = y;
    }

    public String toString() {
        return String.format("The value of y is %b", y);
    }
}

Open in new window

0
 
for_yanCommented:

And it works for me either (see below)
and this copyProperty - why is this copy?
It is just should be setting the property.

But if toBean does not have this which is named
in the method  it will do no change in the toBean,
not even for simple property
package test;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.lang.builder.ToStringBuilder;

public class BeanUtilsCopyPropertiesTest {

	public static void main(String[] args) {

		FromBean fromBean = new FromBean("fromBean", "fromBeanAProp", "fromBeanBProp");
		ToBean toBean = new ToBean("toBean", "toBeanBProp", "toBeanCProp");
		System.out.println(ToStringBuilder.reflectionToString(fromBean));
		System.out.println(ToStringBuilder.reflectionToString(toBean));
		try {
			System.out.println("Copying properties to toBean");
			//BeanUtils.copyProperties(toBean, fromBean);
                         //  BeanUtils.copyProperty(toBean,"MyProperty","tratata");
          //  BeanUtilsBean bub = new BeanUtilsBean();
                            BeanUtils.copyProperty(toBean,"CProp","tratata");

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(ToStringBuilder.reflectionToString(fromBean));
		System.out.println(ToStringBuilder.reflectionToString(toBean));
	}
}

Open in new window



output:

test.FromBean@a62fc3[name=fromBean,aProp=fromBeanAProp,bProp=fromBeanBProp]
test.ToBean@60aeb0[name=toBean,bProp=toBeanBProp,cProp=toBeanCProp]
Copying properties to toBean
test.FromBean@a62fc3[name=fromBean,aProp=fromBeanAProp,bProp=fromBeanBProp]
test.ToBean@60aeb0[name=toBean,bProp=toBeanBProp,cProp=tratata]

Open in new window


package test;

import java.io.Serializable;

public class FromBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private String name;
	private String aProp;
	private String bProp;

	public FromBean() {
	}

	public FromBean(String name, String aProp, String bProp) {
		this.name = name;
		this.aProp = aProp;
		this.bProp = bProp;
	}

	public String getAProp() {
		return aProp;
	}

	public void setAProp(String prop) {
		aProp = prop;
	}

	public String getBProp() {
		return bProp;
	}

	public void setBProp(String prop) {
		bProp = prop;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Open in new window


package test;

import java.io.Serializable;

public class ToBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private String name;
	private String bProp;
	private String cProp;

	public ToBean() {
	}

	public ToBean(String name, String bProp, String cProp) {
		this.name = name;
		this.bProp = bProp;
		this.cProp = cProp;
	}

	public String getBProp() {
		return bProp;
	}

	public void setBProp(String prop) {
		bProp = prop;
	}

	public String getCProp() {
		return cProp;
	}

	public void setCProp(String prop) {
		cProp = prop;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

Open in new window



0
 
for_yanCommented:

So if in the above example you change "CProp" to something which
is not prenset in toBean - then ther will be no change
   BeanUtils.copyProperty(toBean,"CProp","tratata");
in copliance with what API says:

"If the specified bean does not have a property of the specified name, or the property is read only on the destination bean, return without doing anything. "

So it would  not create anything - only would set the specified value
if property exists
0
 
CEHJCommented:
Well that seems entirely reasonable
0
 
for_yanCommented:
But is it reasonable to call iot copy?
Copy from where?
0
 
cofactorAuthor Commented:
>>>the following worked fine for me right away

you are not using nested beans !  your code is different .


I'm attaching my project here. I have exported Eclipse java project  src folder.  Please try running  main class  Test.java.  

This is not copying  property.






src.zip
0
 
for_yanCommented:
Did you try to use upper case?

In my case when I was trying to  do like that:
  BeanUtils.copyProperty(toBean,"cProp","tratata");
it didn't work

I needed to do like that:

BeanUtils.copyProperty(toBean,"CProp","tratata");

because the methods there in the bena are setCProp, not setcProp

I think the same thing with your property name, try upper case

 BeanUtils.copyProperty(proposalVo, "proposalVo.FinanceInfo.IsFunded", true);

0
 
CEHJCommented:
You can't copy/set the property of your member 'financeInfo' of type FinanceInfoVO because no instance of it exists. You need to create it first
0
 
cofactorAuthor Commented:
>>>Did you try to use upper case?
Yes.  But  that too does not work.


0
 
CEHJCommented:
It's got nothing to do with case. You don't have an object on which to set the property
0
 
for_yanCommented:
NO, you need to instantiate, sure,  and change to upper case - both things need to be done
0
 
for_yanCommented:
why do you need this copy when it is such a pain?
why not just set this property without problems?
It anyways does not copy of anywere - just set it without any BeanUtils.
0
 
CEHJCommented:
The following is all you need (works fine for me):
public class ProposalVO extends BaseVO {
	private FinanceInfoVO financeInfo;

	public ProposalVO() {
	    financeInfo = new FinanceInfoVO();
	}
....
}

==============================

	        BeanUtils.copyProperty(proposalVo.getFinanceInfo(), "isFunded", true);

Open in new window

0
 
for_yanCommented:
But it is not you what you want form this method - you want to have propertyy name as a string
In this way it is much easier and more starightforward  to set consecutively, which works very nicely:

                 FinanceInfoVO ff = new FinanceInfoVO();
                ff.setIsFunded(true);



                proposalVo.setFinanceInfoVO(ff);

Open in new window



0
 
for_yanCommented:
You want both of them as string in the second argument
0
 
for_yanCommented:

This is not setting nested property - it is setting simple property and we already know that it works for  simple property.

              BeanUtils.copyProperty(proposalVo.getFinanceInfo(), "isFunded", true);

0
 
CEHJCommented:
That's not true. Besides, the following works just as well:

"name - Property name (can be nested/indexed/mapped/combo)"
BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

Open in new window

0
 
for_yanCommented:

I also read that:
"name - Property name (can be nested/indexed/mapped/combo)"

but the following

BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

Open in new window


is how cofactor had it in the very beginning.

Didn't work for him.

0
 
for_yanCommented:
After huge pain it did work for me.
I think ther was some mixup with VO and not VO
When I first renamed it all FinanceInfo it worked forst time - then I returned back
and it works like below.

and also in your Test.java you should definitely not have proprsalVO within the quotes:

  BeanUtils.copyProperty(proposalVo, "proposalVo.financeInfo.isFunded", true);

About upper case -lower case - I'm not sure I understadn - in my previous example with copyProperties
I'm sure it required the case as in methods, here it dioes not seem to do it.

Anyway this code exactly works for me
(I removed BaseVO in the process - don't think it makes any difference).
I'd prefer to do normal setting than have all this pain.
Unless you have some special situation, when this method is neccssary.

Anywy there is simpilar method setProprty(...) with similar arguments - do you know what  is the difference?

package com;

import org.apache.commons.beanutils.BeanUtils;




public class Test {
	 public static void main(String[] args) throws Exception {
	        try{

	        ProposalVO proposalVo = new ProposalVO();


	      BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

	        System.out.println("proposalVo="+proposalVo);
	        System.out.println("proposalVo.getFinanceInfo().isFunded="+proposalVo.getFinanceInfo().getIsFunded());

	        }catch(Exception e){
	        	System.out.println(e);
	        }
	    }
}

Open in new window


package com;


public class FinanceInfoVO {

	private boolean isFunded;

   public FinanceInfoVO(){
        isFunded =  false;
    }

	public void setIsFunded(boolean b) {
		this.isFunded = b;
	}

	public boolean getIsFunded() {
		return isFunded;
	}

}

Open in new window


package com;

public class ProposalVO  {
	private FinanceInfoVO financeInfo;
public ProposalVO(){
    financeInfo = new FinanceInfoVO();
}

	public void setFinanceInfo(FinanceInfoVO f) {
		this.financeInfo = f;
	}

	public FinanceInfoVO getFinanceInfo() {
		return financeInfo;
	}


}

Open in new window


proposalVo=com.ProposalVO@12d03f9
proposalVo.getFinanceInfo().isFunded=true

Open in new window



0
 
for_yanCommented:
Yes, extending with BaseVO makes no difference:

package com;


public class FinanceInfoVO extends BaseVO {

	private boolean isFunded;

   public FinanceInfoVO(){
        isFunded =  false;
    }

	public void setIsFunded(boolean b) {
		this.isFunded = b;
	}

	public boolean getIsFunded() {
		return isFunded;
	}

}

Open in new window



package com;

public class ProposalVO extends BaseVO {
	private FinanceInfoVO financeInfo;
public ProposalVO(){
    financeInfo = new FinanceInfoVO();
}

	public void setFinanceInfo(FinanceInfoVO f) {
		this.financeInfo = f;
	}

	public FinanceInfoVO getFinanceInfo() {
		return financeInfo;
	}


}

Open in new window


proposalVo=com.ProposalVO@1abc7b9
proposalVo.getFinanceInfo().isFunded=true

Open in new window


0
 
cofactorAuthor Commented:
>>>and also in your Test.java you should definitely not have proprsalVO within the quotes:

strange !  ..I do that  way to copyProperties for String property and they work fine. ..so tried the same to copy boolean property .

Is this  only for  Boolean property that I can not keep proprsalVO within the quotes ?

By the way , I'm no longer using this code now, I have switched to  <s:hidden of Struts 2 which loads the data as expected and solved my problem.

But would still like to know what caused the problem?

0
 
for_yanCommented:

This is with proposalVo:
package com;

import org.apache.commons.beanutils.BeanUtils;




public class Test {
	 public static void main(String[] args) throws Exception {
	        try{

	        ProposalVO proposalVo = new ProposalVO();


	      BeanUtils.copyProperty(proposalVo, "proposalVo.financeInfo.isFunded", true);

	        System.out.println("proposalVo="+proposalVo);
	        System.out.println("proposalVo.getFinanceInfo().isFunded="+proposalVo.getFinanceInfo().getIsFunded());

	        }catch(Exception e){
	        	System.out.println(e);
	        }
	    }
}

Open in new window


Output:
proposalVo=com.ProposalVO@1ac3c08
proposalVo.getFinanceInfo().isFunded=false

Open in new window


This is with only cahnge - removal of proprosalVo:



package com;

import org.apache.commons.beanutils.BeanUtils;




public class Test {
	 public static void main(String[] args) throws Exception {
	        try{

	        ProposalVO proposalVo = new ProposalVO();


	      BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

	        System.out.println("proposalVo="+proposalVo);
	        System.out.println("proposalVo.getFinanceInfo().isFunded="+proposalVo.getFinanceInfo().getIsFunded());

	        }catch(Exception e){
	        	System.out.println(e);
	        }
	    }
}

Open in new window



Output:

proposalVo=com.ProposalVO@15dfd77
proposalVo.getFinanceInfo().isFunded=true

Open in new window

0
 
for_yanCommented:
I addeed simple (not nested) String property prop
to the PropsoalVO:

package com;

public class ProposalVO extends BaseVO {
	private FinanceInfoVO financeInfo;
    private String prop;
public ProposalVO(){
    financeInfo = new FinanceInfoVO();
}

	public void setFinanceInfo(FinanceInfoVO f) {
		this.financeInfo = f;
	}

	public FinanceInfoVO getFinanceInfo() {
		return financeInfo;
	}

    public String getProp() {return prop; }

    public void setProp(String s){
        prop = s;
    }

}

Open in new window



And observe the same picture:

package com;

import org.apache.commons.beanutils.BeanUtils;




public class Test {
	 public static void main(String[] args) throws Exception {
	        try{

	        ProposalVO proposalVo = new ProposalVO();


	      BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

                     BeanUtils.copyProperty(proposalVo, "prop", "abc");

	        System.out.println("proposalVo="+proposalVo);
	        System.out.println("proposalVo.getFinanceInfo().isFunded="+proposalVo.getFinanceInfo().getIsFunded());
              System.out.println("proposalVo.prop="+proposalVo.getProp());


	        }catch(Exception e){
	        	System.out.println(e);
	        }
	    }
}

Open in new window


Output:
proposalVo=com.ProposalVO@15dfd77
proposalVo.getFinanceInfo().isFunded=true
proposalVo.prop=abc

Open in new window


If I add "proposalVo." to the property name:

package com;

import org.apache.commons.beanutils.BeanUtils;




public class Test {
	 public static void main(String[] args) throws Exception {
	        try{

	        ProposalVO proposalVo = new ProposalVO();


	      BeanUtils.copyProperty(proposalVo, "financeInfo.isFunded", true);

                     BeanUtils.copyProperty(proposalVo, "proposalVo.prop", "abc");

	        System.out.println("proposalVo="+proposalVo);
	        System.out.println("proposalVo.getFinanceInfo().isFunded="+proposalVo.getFinanceInfo().getIsFunded());
              System.out.println("proposalVo.prop="+proposalVo.getProp());


	        }catch(Exception e){
	        	System.out.println(e);
	        }
	    }
}

Open in new window


then the value is not set:

proposalVo=com.ProposalVO@1f630dc
proposalVo.getFinanceInfo().isFunded=true
proposalVo.prop=null

Open in new window


And I think I tried all possible combination of cases in theis proposalVo. or ProposalVO. or whatever

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.