Are you writing your Java classes as Singletons quite often? Use this Eclipse template and make any class a singleton in two seconds:
Setup
Go to Window -> Preferences -> Java -> Editor -> Templates. Create New:
Code for copy paste:private static final ${enclosing_type} instance = new ${enclosing_type}();
private ${enclosing_type}() {}
public static ${enclosing_type} getInstance() {
return instance;
}
Action
Type "single" or "singleton", hit Content Assist shortcut key (ctrl+space by default), then enter:
And there you go - a singleton in two seconds (actual time may vary on your typing and CPU speed).
You may want to use a different Singleton implementation. Check out Java Singleton: The Proper Way for a good example.
2008-09-18
Eclipse Template: Singleton Pattern
2008-09-01
Add loggers to your Java code in seconds using Eclipse Templates
Custom Eclipse Templates can greatly increase your productivity by automating daily development. For instance, instead of creating Apache Commons Logging loggers by hand, you can do this:
Setup
Go to Window -> Preferences -> Java -> Editor -> Templates. Create New:
Action
Type "log", hit Content Assist shortcut key (ctrl+space by default), then enter:
Hit Ctrl+Shift+O to import Log and LogFactory:
Saves hell of a time in the long run.
2008-07-16
What is Eclipse Mylyn anyway?
Since the release of Eclipse Europa, there was one new big feature - Mylyn. I was sceptic about it, besides it made the IDE slower, so I've kept removing it without getting to know what the hell is Mylyn anyway? After seeing this video I'm about to give it another try. If you're using Eclipse without Mylyn, you must see the video below:
Update
Mylin still sucks big time. Could not get used to it. It's most probably for people with ADD. Read the discussion in comments.
2008-05-10
Getting Unicode output in Eclipse Console
Tired of seeing garbled Eclipse Console output? Here is a quick and dirty tutorial for getting Unicode output in Eclipse Console.
Fact: You will not get Unicode output in Eclipse Console while using System.out directly in Windows. See Eclipse BUG #13865.
1. add -Dfile.encoding=UTF-8 to your eclipse.ini
2. make sure your Eclipse Console font supports Unicode. You can try it out by typing unicode characters directly to console with keyboard. Console Font is set in Window -> Preferences -> General -> Appearance -> Colors and Fonts -> Debug -> Console Font
3. if you are NOT using Windows, set your system encoding to UTF-8. You should now see Unicode characters in Console after restarting Eclipse.
4. if you are using Windows or do not want to change your OS encoding, you will have to avoid using System.out stream directly. Instead, wrap it up with java.io.PrintStream:
PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
sysout.println("\u2297\u0035\u039e\u322F\u5193");
5. if you are using Log4J with ConsoleAppender, make sure to set the encoding property to UTF-8. Example:
#TRACE appender
log4j.appender.stdout.trace=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.trace.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.trace.encoding=UTF-8
log4j.appender.stdout.trace.layout.ConversionPattern=%p [%c] - %m%n
log4j.appender.stdout.trace.Threshold=TRACE
Happy development!
2008-04-21
Make your Eclipse rock
Eclipse is one of the greatest IDEs available out there. In my opinion it's the best, because in comparison with other choices such as Intellij IDEA, Eclipse is free and open. People say NetBeans are getting good, however I am too skeptic to believe. As it's the matter of preference, I will stick with Eclipse.
Yet, after a fresh and clean install Eclipse (v3.3 as of this moment) is not yet kicking. A few crucial things are missing. I'll give out my recipe to making Eclipse rock. So, let's get on with that:
- Fresh install from www.eclipse.org. As I'm NOT doing J2EE/Web/XML stuff at home, Eclipse IDE for Java Developers is the best choice for me.
- Give it more RAM! Default values are pathetic, so you should edit your eclipse.ini to look like this:
-showsplash
Say bye to those possible out of memory and permgen space errors.
org.eclipse.platform
--launcher.XXMaxPermSize
256M
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms256m
-Xmx1024m
-XX:PermSize=256m - Fine-tune the Preferences. Go straight to workspace and open Window -> Preferences.
General:
Check "Show heap status" - it's nice to see how much heap you've got.
General -> Appearance:
Uncheck "Show text on the perspective bar". That gives more space for perspective icons.
Check "Show traditional style tabs". Performance.
Uncheck "Enable animations". Performance.
General -> Editors -> Text Editors:
Check "Insert spaces for tabs". You may not want that if you're a tab fan.
Check "Show print margin". 80 is good.
Check "Show line numbers". Who would not want to see line numbers by default?
General -> Editors -> Text Editors -> Spelling:
Uncheck "Enable spell checking". Performance. And hell, that's no Microsoft Word.
General -> Startup and Shutdown:
Uncheck "Mylin Tasks UI" from "Plug-ins activated on startup". Unless you REALLY want to use Mylin. I think it sucks and terribly slows Eclipse down.
General -> Workspace:
Choose "UTF-8" as your "Text file encoding". Unicode is the way to go.
Choose "Unix" as your "New text file line delimiter". This is also is the way to go.
Java -> Code Style -> Formatter:
Click "Edit..." on Eclipse [built-in] profile. Set new profile name. In "Identation" tab set "Tab policy" to "Spaces only". You may skip this if you're a tab dude.
Java -> Compiler -> Errors/Warnings:
You may want to harden your compiler warnings for more beautiful and strict development. I tend to override these:
In "Code style": "Undocumented empty block" -> "Warning".
In "Unnecessary code": "Unnecessary 'else' statement" -> "Warning".
In "Unnecessary code": "Unnecessary cast or 'instanceof' operation" -> "Warning".
In "Unnecessary code": "Unnecessary declaration of thrown checked exception" -> "Warning".
Java -> Compiler -> Javadoc:
Set "Malformed Javadoc comments:" -> "Warning".
Set "Only consider members as visible as: " -> "Private".
You want your Javadoc clean, don't you.
Java -> Editor -> Content Assist -> Advanced:
In content assist proposal list uncheck entries marked with "(Mylin)" and check the alternatives: "Other Java Proposals", "Template Proposals", "Type Proposals".
In cycling list check all same entries: "Other Java Proposals", "Template Proposals", "Type Proposals".
Web and XML -> XML Files -> Source:
In "Formatting" section check "Indent using spaces" and set Indentation size to "2". Otherwise you will end up with tabs in your XML. - Subversive - the best Eclipse plug-in for SVN support:
Help -> Software Updates -> Find and Install. Search for new features to install. Add two New Remote Sites - "Subversive SVN Connectors" with URL: http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/ and "Subversive plug-in" with URL: http://download.eclipse.org/technology/subversive/0.7/update-site/
Check http://www.eclipse.org/subversive/downloads.php for latest URLs.
Install new components. Skip Mylin integration and sources. - Maven plug-in. You MUST know what Maven is, otherwise don't do Java. Seriously. You may choose from Tycho and Q: http://maven.apache.org/eclipse-plugin.html
I was a long-time user of Tycho, however Q looks really promising. I'm trying it right now for the first time. Yeah, definitely, go for Q. Installation is easy, just add New Remote Site:
Q4E: http://q4e.googlecode.com/svn/trunk/updatesite/ - XML Buddy. Your light weight swiss army knife in Eclipse XML editing. Download plug-in manually from http://xmlbuddy.com and drop it into your Eclipse plugins folder, then restart.
- JADClipse. Your daily Java decompiler. Best for those times when you need to go under the hood. Make this a habit. You will need JAD in your PATH: http://www.kpdus.com/jad.html
Then download JADClipse plug-in from http://jadclipse.sourceforge.net/, drop it to Eclipse plugins folder and restart.