Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 385984

Summary: Modifying a variable value using cell editor changes another variable
Product: [Eclipse Project] JDT Reporter: Miloslav Brada <email>
Component: DebugAssignee: JDT-Debug-Inbox <jdt-debug-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: email, samrat.dhillon
Version: 4.2   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard: stalebug

Description Miloslav Brada CLA 2012-07-25 16:03:18 EDT
Build Identifier: I20120608-1400

When I change a variable value in Variables view, it changes value of another variable in the same bean also. Tested with Short data type, see the steps to reproduce. I had to enter "(short)2" because simple "2" wasn't accepted.

Reproducible: Always

Steps to Reproduce:
1. Debug the program to the commented line
2. In the Variables view change the value of "a" to "(short)2".
3. The b also changes to "2".

public class TestBug {
	Short a = 1;
	Short b = 1;

	public static void main(final String[] args) {
		TestBug testBug = new TestBug();

		System.err.println(testBug.a);
		System.err.println(testBug.b);
		// stop here and change the "a" variable value
		System.err.println(testBug.a);
		System.err.println(testBug.b);
                // the "b" also get changed :(
	}

}
Comment 1 Samrat Dhillon CLA 2012-08-28 12:45:09 EDT
(In reply to comment #0)
> Build Identifier: I20120608-1400
> 
> When I change a variable value in Variables view, it changes value of
> another variable in the same bean also. Tested with Short data type, see the
> steps to reproduce. I had to enter "(short)2" because simple "2" wasn't
> accepted.
> 
> Reproducible: Always
> 
> Steps to Reproduce:
> 1. Debug the program to the commented line
> 2. In the Variables view change the value of "a" to "(short)2".
> 3. The b also changes to "2".
> 
> public class TestBug {
> 	Short a = 1;
> 	Short b = 1;
> 
> 	public static void main(final String[] args) {
> 		TestBug testBug = new TestBug();
> 
> 		System.err.println(testBug.a);
> 		System.err.println(testBug.b);
> 		// stop here and change the "a" variable value
> 		System.err.println(testBug.a);
> 		System.err.println(testBug.b);
>                 // the "b" also get changed :(
> 	}
> 
> }

I don't think this is a bug in Eclipse. What you have actually done is modified  "private final value" field on java.lang.Short class. Short and other wrapper classes like Integer are supposed to be immutable, but when you modify a private final field it is no longer immutable and leads to all sorts of problems.  Here is a sample code which will change show the same behaviour as  you have shown in the defect without using debugger. In the code below I modified the "value" field on Integer class and can will notice in output that both a and c have changed. 

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class TestBug {

	public static void main(final String[] args) {

		Integer a = 2;
		Integer c = 2;

		System.err.println(a);
		System.err.println(c);

		try {
			setFinalStatic(a, "value", 4);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.err.println(a);

		System.err.println(c);
		System.err.println(Integer.valueOf(2));// Even this has changed to 4 by now
	}

	static void setFinalStatic(Object o, String fieldLabel, Object newValue)
			throws Exception {
		Field field = o.getClass().getDeclaredField(fieldLabel);
		field.setAccessible(true);

		Field modifiersField = Field.class.getDeclaredField("modifiers");
		modifiersField.setAccessible(true);
		modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

		field.set(o, newValue);
	}

}

I will be nice to have a preference in eclipse through you could control if you would allow eclipse debugger to modify private final fields.
Comment 2 Eclipse Genie CLA 2020-02-02 19:48:01 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.