Eclipse
Eclipse notes and tricks.
Check also my blogs in the eclipse category.
My favorite Eclipse magic
(Shortcuts under Mac - under Linux they’re likely the same only having Ctrl instead of ) (| indicates position of the cursor; is Command, is Shift, is Alt, ^ is Control)
Open Type (T) - quickly find any class on the classpath
Open Resource (R) - quickly find any file in the project
Quick Outline (O) - opens a pop-up similar to the Outline view but filterable with support for the * wildcard - useful for jumping quickly to a method/member (much better than search)
Smart Complete(^Space) - complete just about anything (variables, methods, types, …) including predefined Code Templates and more
Class-level completion (press ^Space anywhere outside of a method for all proposals or type first few letters of what you want first)
Override/implement method: start typing the name of the method, e.g. “toS” for toString() - or just press ^Space and select the proposition you want
Quick constructor creation: if the class has no constructor, just press ^Space, it should come first
Quick setter/getter generation: as above
Create for/foreach loop to loop over Iterable/Collection/array - type “for” and ^Space
Surround with try-catch or another block statement (do/while/for loop, try-catch, synchronized, if, runnable) - select at least two lines and ^Space, go to the end of the proposition list (via up arrow in the list)
Print to sysout/syserr: select 1 line and ^Space, go to the last propositions
Quick Fix (1) - proposes things you are likely to do; experiment with how the proposals differ based on where exactly your cursor is and what is or is not selected
Create new field initialized in a constructor: declare the constructor’s parameter (leaving cusrsor just behind the name), press 1 and select Assign parameter to new field.
Introduce local variable: select a statement and 1
Complete definition of a new local variable - type: type e.g. “a = new String();|” and 1, select “Create local variable ‘a’” (notice also the other options - field and parameter).
Convert local variable to field (cursor behind variable’s name), also Inline local variable
Many other - just experiment
Open Call Hierarchy - right-click, select it
Organize Imports (O)
Minor
Convert to static import - M on a static method/constant (not yet available via quick-fix :-()
Next editor (F6) - to cycle between open files.
Completion - swtich between insert (default) and override - hold Ctrl (see Java - Editor - Content Assist - Insertion)
Other noteworthy things:
When you search for class name (Open Type, completions), Eclipse is clever enough to deal with abbreviated forms like IA, IAE, IllArgEx etc. when searching for IllegalArgumentException.
Quick Access (3) - search in open windows, options, views, commands, …
Word completion (useful e.g. in JavaDoc): Alt+/ (this and previous tips are from K. D. Sherwood)
Copy/Move Line (/ or down)
Johannes Brodwall: Some eye-openers that people enjoy learning (copy/move line and other valuable things - check it out!).
Coding help
Various
Simple import of static methods
If you have some static methods that you use often - such as JUnit’s Asserts Mockito’s fluent API - you may enable Eclipse to add the necessary static imports by adding these types to your Favorite imports:
Java → Editor → Content Assist → Favorites, click [New Type…] and select the class defining the static method.
Normally when you type the name of a static method defined in another class, Eclipse will complain about non-existing method and doesn’t offer you to create a static import for the method, unless it is among the favorite imports.
My favourites:
org.hamcrest.CoreMatchers.*; org.hamcrest.Matchers.*; org.junit.Assume.*; org.junit.Assert.*; org.mockito.Mockito.*; org.mockito.Matchers.*;
Source: Piotr Jagielski’s blog Working With Static Imports in Eclipse.
Templates
Logging
From the article The Dark Art of Logging (2010).
Variant A: commons-logging
Window → Preferences → Java → Editor →Templates.
Log declaration (“logdef”):
${:import(org.apache.commons.logging.Log, org.apache.commons.logging.LogFactory)} private static final Log LOG = LogFactory.getLog(${enclosing_type}.class);
“debug”:
if (LOG.isDebugEnabled()) { LOG.debug("${enclosing_method}: ${msg}"); }
Variant B: Java logging
As above except the templates themselves.
logdef:
${:import(java.util.logging.Logger)} private static final Logger LOG = Logger.getLogger(${enclosing_type}.class.getName()) ;
Default action in a catch block
Change the action on exception in the template for try-catch blocks through Window → Preferences → Java → Code Style → Code Templates. Change the Code → ‘Catch block body’ and ‘Code in new catch blocks’ template to the following.
LOG.error("${msg}", ${exception_var});
(Default is exception.printStackTrace().)
Various
Test method
A slight modification of the default “Test” template:
@${testType:newType(org.junit.Before)} public void setUp() throws Exception { //MockitoAnnotations.initMocks(this); ${cursor} }
Also setUp
may be useful:
@${testType:newType(org.junit.Test)} public void ${testname}() throws Exception { ${staticImport:importStatic('org.junit.Assert.*')}fail("not yet implemented");${cursor} }
Favorite Plugins
DBViewer
JUnitMax
AnyEdit Tools (text and whitespace conversions etc.)
m2eclipse
Git: ?
Checkstyle
Experimenting with:
Crap4j
CodePro AnalytiX
Using Eclipse at Presentations
Increase font size
Text font: Ctrl+ and Ctrl- with Tarlog’s plugin or Preferences - General - Appearance - Colors and Fonts (or just search for Fonts) - select Basic - Text Font and edit it
Tab labels: View and Editor Folders - Part title font
Content of views (e.g. Package Explorer): this cannot be set from Eclipse, it’s taken over from the windowing system so you need to change the font size in your desktop environment (in GTK/..)
Hide clutter - the best way is to create a new Perspective for presentations without unnecessary toolbar and menu elements
Right-click the top toolbar and select Hide Toolbar (redisplay: menu Window - Show Toolbar)
Try one of the Fullscreen plugins for Eclipse, f.ex. U. Sangiorgi’s Eclipse-Fullscreen plugin (download into dropins/, 9/2011 in E3.6, Pref. - General - Full Screen - choose whether to hide menu and/or status bar, see Window - Full Screen and Alt+Ctrl+Z or Esc) or M. Scharf’s Eclipse Mode plugin (12/2009, tested in E3.6) - hides the title bar (but not the status bar)
Beware that the settings are valid for a workspace → you may want to create a new one for the presentation.
You may also want to try the Eclipse Presentation Theme by Zoltán Ujhelyi to make switching to a bigger font size easier.