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

Bug 29117

Summary: [templates] add refactoring: singletonize
Product: [Eclipse Project] JDT Reporter: Patrice Kerremans <patrice_kerremans>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: P3 CC: dannydig, dmitry.astu, markus.kell.r, nobody, pshivale
Version: 2.1Keywords: helpwanted
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Patrice Kerremans CLA 2003-01-07 16:58:40 EST
add a refactoring menuitem to create a singleton from a class by:

say you right click the class MySingleton and select to singletonize it:

1. add (or modify if already existing) a private default constructor
           private MySingleton() {
           }
2. add a member variable
           private static final MySingletonClass INSTANCE = new MySingleton();
3. add a static getinstance method
           public static MySingleton getInstance() {
               return INSTANCE;
           }
4. show preview
done.

THANKS a 1000K !
Comment 1 Dirk Baeumer CLA 2003-01-08 09:08:51 EST
What's about providing the code ;-). Without help this will very likely not be 
in 2.1
Comment 2 Patrice Kerremans CLA 2004-01-31 16:40:32 EST
ok, here's a thought:  add the following template to the list of editor 
templates, and we can create singletons using code completion which is -almost- 
as good as refactoring :)

-----------8<--------------8<--------------------

	/** the singleton instance */
	private static final ${enclosing_type} INSTANCE = 
                                             new ${enclosing_type}();

	/**
	 * Returns the singleton instance (not necessarily be of this class
	 * unique - may come from a pool read GoF-book). 
	 * Return the singleton instance of this class (not necessarily be 
         *        unique - may come from a pool read GoF-book)
	 */
	public static final ${enclosing_type} getInstance() {
		return INSTANCE;
	}

	/**
	 * Default constructor.
	 */
	private ${enclosing_type}() {
		super();
	}
------------8<--------------8<---------------
Comment 3 Dani Megert CLA 2004-02-06 02:50:21 EST
Patrice,

what you provided in comment 2 is a template. We could add a template but that's
not a refactoring. This will not
- give a preview
- change occurrences of new Class() to use the singleton

Note: you can add the template yourself since templates is configurable.
Comment 4 Patrice Kerremans CLA 2004-02-06 06:32:32 EST
Yes, you're absolutely right.  I just gave it to you as a workaround.  If and
when I come up with a real refactoring I'll come back to you.
Comment 5 Dani Megert CLA 2004-02-06 06:41:18 EST
reopen to reassing
Comment 6 Dani Megert CLA 2004-02-06 06:42:02 EST
.
Comment 7 Eclipse Webmaster CLA 2009-08-30 02:35:25 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.
Comment 8 Nobody - feel free to take it CLA 2009-09-13 10:43:11 EDT
I also would appreciate a refactoring for singleton instance usage or at least a template delivered. I offer my help if desired.
Comment 9 Markus Keller CLA 2009-09-14 11:55:54 EDT
*** Bug 67192 has been marked as a duplicate of this bug. ***
Comment 10 Markus Keller CLA 2009-09-14 12:04:42 EDT
Still no plans to do this. "Introduce Singleton" would most of the time not be a refactoring (as soon as you have two instantiations, it cannot be behavior-preserving). The main work can already be done with "Introduce Factory". The rest can easily be done manually.

Furthermore, there are multiple styles how singletons can be created - each with its own benefits and drawbacks. An editor template with your preferred style is the way to go!
Comment 11 Dani Megert CLA 2010-04-14 03:40:03 EDT
*** Bug 302434 has been marked as a duplicate of this bug. ***
Comment 12 Dani Megert CLA 2012-10-29 04:26:07 EDT
*** Bug 393000 has been marked as a duplicate of this bug. ***