Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LscBean clone method does not shallow copy #331

Open
elecharny opened this issue Jan 20, 2025 · 0 comments
Open

LscBean clone method does not shallow copy #331

elecharny opened this issue Jan 20, 2025 · 0 comments
Milestone

Comments

@elecharny
Copy link

The LscBeean.clone() method does not shallow copy the attributes, which leads to data being shared between two objects:

	public LscBean clone() throws CloneNotSupportedException {
		try {
			LscBean bean = this.getClass().newInstance();
			bean.setMainIdentifier(this.getMainIdentifier());

			for (String attributeName : this.getAttributesNames()) {
				bean.setAttribute(attributeName,
						this.getDatasetAsSetById(attributeName));  <<<------------------Here
			}
			return bean;
		} catch (InstantiationException ex) {
			throw new CloneNotSupportedException(ex.getLocalizedMessage());
		} catch (IllegalAccessException ex) {
			throw new CloneNotSupportedException(ex.getLocalizedMessage());
		}
	}

When an object A is cloned, the object B refers to object A attributes.

this    SimpleBean  (id=114)    
datasets    HashMap<K,V>  (id=174)  
    [0] HashMap$Node<K,V>  (id=175) 
        key "sn" (id=165)   
        value   HashSet<E>  (id=176) <--+   
    [1] HashMap$Node<K,V>  (id=195)     |
        key "cn" (id=186)               |
        value   HashSet<E>  (id=187) <-----+
mainIdentifier  "something" (id=157)    |  |
bean    SimpleBean  (id=156)             >.|..... Same reference
datasets    HashMap<K,V>  (id=202)      |  |
    [0] HashMap$Node<K,V>  (id=206)     |   >---- Same reference
        key "sn" (id=165)               |  |
        value   HashSet<E>  (id=176) <--+  |
    [1] HashMap$Node<K,V>  (id=207)        |
        key "cn" (id=186)                  |
        value   HashSet<E>  (id=187) <-----+
mainIdentifier  "something" (id=157)    
@elecharny elecharny changed the title LscBean clone methgod does not shallow copy LscBean clone method does not shallow copy Jan 20, 2025
@coudot coudot added this to the 2.2 milestone Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants