Bug 495903 - [change method signature] adding a new parameter may have a default value with side effects
Summary: [change method signature] adding a new parameter may have a default value wit...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-10 14:06 EDT by Jongwook Kim CLA
Modified: 2023-03-20 20:33 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jongwook Kim CLA 2016-06-10 14:06:51 EDT
When method m introduces a new parameter (of type B) whose default value is new B(), m invocation will evaluate its argument expression new B() after the refactoring. When the argument expression has side effects, the change-method-signature may change program behavior.

BEFORE
-----------------------------------
class A {
	void m() {}

	void n() {
		new A().m();
	}
}

AFTER
-----------------------------------
class A {
	void m(B b) {}

	void n() {
		new A().m(new B());  // new B() may have side effects
	}
}
Comment 1 Eclipse Genie CLA 2019-01-16 07:10:31 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.

If you have further information on the current state of the bug, please add it. 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.
Comment 2 Dani Megert CLA 2019-01-16 10:48:57 EST
The refactoring creates the new class with a no-arg constructor. Side effects will only happen if the new class is changed.
Comment 3 Jongwook Kim CLA 2019-01-16 10:55:29 EST
Dani -- I showed an "example" above.  Side effects can occur when the argument expression is not a simple variable.  Read my comment again.
Comment 4 Dani Megert CLA 2019-01-16 10:58:06 EST
(In reply to Jongwook Kim from comment #3)
> Dani -- I showed an "example" above.  Side effects can occur when the
> argument expression is not a simple variable.  Read my comment again.

There won't be a side effect as the constructor for B is created. If you disagree, please attach an example that can be executed and shows a different value after the refactoring.
Comment 5 Jongwook Kim CLA 2019-01-16 11:04:23 EST
BEFORE (Output: 0)
-----------------------------------
class A {
	void m() {}

	void n() {
		new A().m();
	}
}

class B {
	static int i = 0;
	
	B() {
		B.i++;
	}
	
	public static void main(String[] args) {
		new A().n();
		System.out.println(B.i);
	}
}


AFTER (Output: 1)
-----------------------------------
class A {
	void m(B b) {}

	void n() {
		new A().m(new B());  // new B() may have side effects
	}
}

class B {
	static int i = 0;
	
	B() {
		B.i++;
	}
	
	public static void main(String[] args) {
		new A().n();
		System.out.println(B.i);
	}
}
Comment 6 Dani Megert CLA 2019-01-16 11:12:48 EST
The attached code is not generated by the refactoring but changed by you after doing the refactoring. If I am wrong, please provide details steps that do not contain any code changes made by you and only use the refactoring.
Comment 7 Jongwook Kim CLA 2019-01-16 11:21:21 EST
(1) Select "Change Method Signature" refactoring on method m()
(2) Click "Add" button to add a new parameter to method m()
(3) Set "Type" to "B"
(4) Set "Name" to "b"
(5) Set "Default value" to "new B()"
(6) Don't turn on the delegate option
(7) Click "OK" button to run the refactoring
Comment 8 Dani Megert CLA 2019-01-16 11:29:24 EST
OK, got it. I was using the introduce parameter refactoring. Sorry for that.
Comment 9 Jongwook Kim CLA 2019-01-16 12:19:04 EST
That is OK.  The previous title was misleading; Eclipse has another refactoring named "introduce parameter object".
Comment 10 Stephan Herrmann CLA 2019-01-16 12:45:14 EST
My 2c:

(In reply to Jongwook Kim from comment #7)
> (5) Set "Default value" to "new B()"

This is the user programming, not s.t. that the refactoring created.
Hence the user should not be surprised if this expression produces a side-effect.
(Some argue that constructors with side-effect are the problem to begin with).

If we expect the refactoring to catch this, what should it do? Prohibit any non-trivial expressions in the "Default value" field? Would people be happy about that that limitation?
Comment 11 Eclipse Genie CLA 2021-01-06 14:09:44 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.

If you have further information on the current state of the bug, please add it. 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.
Comment 12 Eclipse Genie CLA 2023-03-20 20:33:28 EDT
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.

If you have further information on the current state of the bug, please add it. 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.