Community
Participate
Working Groups
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.
> 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(); } }