Bug 278167 - [introduce factory] Allow the creation of non-static factory methods
Summary: [introduce factory] Allow the creation of non-static factory methods
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.4.2   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-28 04:11 EDT by Mauro Molinari CLA
Modified: 2009-05-28 06:52 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mauro Molinari CLA 2009-05-28 04:11:34 EDT
It would be very useful to have an option in the "Introduce Factory" refactoring wizard to specify to use a non-static factory method.

I mean, suppose you have class A for which you want to create a factory method on class B and suppose that 99% of A instances are created from within a <C extends B> instance. With this option checked, you may have calls like

-- within C implementation --
A a = new A(...);

replaced by:

-- within C implementation --
A a = createA(...);

by adding the method:

-- within B definition --
public A createA(...)

that is non-static. With this option checked, a warning may arise in the preview step if an instance of A is created within a class D that does not extend B, in which case the call to the A constructor should not be replaced by a call to the non-static factory method and the A constructor should not set to private/protected even if the user had checked the corresponding option.

Right now I find myself in this situation: I need to create a non-static factory method starting from a constructor and Eclipse is helping me most, but not completely, because I will then need to remove the static call in each of the modified classes, after changing the method definition... 
It would be much easier if Eclipse at least didn't generate the static call with the classname qualifier (that is, using "B." in A a = B.createA(...)) or could let me choose wether to generate it or not.
Comment 1 Markus Keller CLA 2009-05-28 06:52:27 EDT
> It would be much easier if Eclipse at least didn't generate the static call
> with the classname qualifier (that is, using "B." in A a = B.createA(...)) or
> could let me choose wether to generate it or not.

I'd rather do this than adding an option for a non-static factory method that often ends with compile errors. But we need to be careful, since omitting the explicit class reference can result in a wrong call when a method 'createA' already binds at a constructor reference (because it exists in C or one of its super classes, or via a static import).


Complete example:

package p;
class A {
}

package p;
public class B {
}

package p;
public class C extends B {
	void foo() {
		A a = new A();
	}
}