Baptiste Mathus |
MojoHaus Committer (previously MOJO@Codehaus) |
Jenkins Contributor & Plugins Maintainer |
#automation #tooling #java #passion #oss #jenkins #maven #nexus #sonarqube
Just wait for the next one then, it should not be long.
Don’t worry: slides with each tip will be provided
CTRL-S (Yay!)
This one is very useful. And alternatively you can also click (with the mouse) on the small icon that you will generally find on the left (because this is actually customisable, I’ve been told). I like it because it prevents you from going into the File/Save Menu. It took me years to find this one. I hope you will enjoy it too!
I hope you will like it as much as I did. I hope.
Lol…
Let’s now get a bit more serious, only 34 minutes left…
In Java/Appearance/abbreviate package names :
Example:
net.batmat.econ2015={DEMO}
Before |
After |
Control-Shift-T
Ctrl-O displays all the methods of the current class
Or Inherited
Ctrl-O-O methods of the current class + parent
Control-Shift-R
Or more selectively
Use space or < to stop matching
Alt-Shift-x will display a contextual menu.
Example: you’re in a JUnit class, just type Alt-Shift-X then T to run the tests.
Be explicit. Changing the code it’s natural to just run Ctrl-F11 or F11 to re-execute the previously ran test
Run/Debug/Launching/Launch Operation/Always launch the previously launched application
Type Ctrl-Shift-1 (or click on the lightbulb on the left of the line) to see what the possible actions are on the current focus
Select text, right click and extract the whole code block into a new method easily.
Inverse: see (11/51) Inline Method
Easily inline the code of a method.
Inverse: see (10/51) Extract Method
Replace your ternary expression with an if-else (more often than not!)
Before :
After :
Select the if/else to invert, and call the Quick Fix
Just type the collection/array, and call the Quick Fix
If putting your cursor next to the curly brace isn’t enough to find the associated closing/opening one:
Use Ctrl-Shift-P
Accessible through Clean Up & Quick Fix
Not enabled by default. In Java/Editor/Typing.
Use Ctrl-Shift-3 on a selected item, then start typing to filter the possible actions in the Quick access menu.
Useful: be able to automatically format only the code just modified when saving the file.
See in Java/Editor/Save Actions/Format source code/Format edited lines
// @formatter:off
// Some weirdly formatted source
String s2 =
"hello";
// @formatter:on
Select the text you want to format, and hit ctrl-shift-f as usual.
Typing Ctrl-Space many times shows different proposals.
Those are actually configurable (list and order of proposals).
See Java/Editor/Content Assist/Advanced.
Autocomplete static imports!
In Java/Editor/Content Assist/Favorites
Lets you add more typesafe-ness in your codebase (at least way more than Javadoc does!).
Beware: it’s not enforcing anything at all at runtime. This is only hints for the developer.
To go even further:
@Nonnull List<@Nonnull String> list = new ArrayList<>();
for(String string : list) {
if(string != null ) { // Useless if hinted by Eclipse
throw new IllegalArgumentException("WTF?");
}
...
}
Advice: annotate the package itself to define the default value for a whole package
⇒ package-info.java
Note
|
With regards to Null Analysis : because studies show developers actually expect parameters to be passed non null as a default, you will generally annotate the package with "Nonnull by default" and then only annotate methods where you actually expect or produce Nullable things. |
Java/Editor/Content Assist : toggle "Completion overwrites" instead of "Completion Inserts"
Can be live toggled using Ctrl key.
Inside an instanceof block, analyzes the type of the given instanceof and autocompletes with its methods:
In Java/Appearance/Type Filters
To filter out java.awt.* for example…
Filter out method coming from Object! (who wants to call notify()
…)
Categorize Filtering/choosing which methods to display in the Outline using javadoc’s @category tags
(thanks Jordi Barrère).
Be able to filter out stack when debugging:
Thanks Sebastien Bordes for the reminder
Alt-Shift-A or the icon, as if Sublime Text invented it all ;-)
Present complex/weird physical data structure in a logical way in the debugger
For reference see the official documentation or that forum discussion.
Ctrl-M
Ctrl-D
Useful to categorize projects, or packages.
String s = String.format("%05d", 7);
s
You can easily add any code block for future reuse.
Difference with snippets: can be variabilized.
Cf. Java/Editor/Templates
${iterable}.forEach(${iterable_element} -> {
${cursor}
});
Example: formatter on/off (cf. (21/51) Formatter: don’t touch that part (using begin/end tags))
Many conditions, not always well-known
Use it to debug :
System.out.println("HERE WE ARE: "+theVariable);
return false;
Gotcha: in external jars (like rt.jar) without local variable table, use the arg0
, arg1
… placeholders for parameters, instead of the original parameter names.
When debugging, you don’t have to put a breakpoint on some line to take the debugger up to it.
Just put the cursor on the line you want, then Ctrl-R (or right click/Run to line)
Thanks Olivier :-)
Many quick fixes are actually available, but not always through cleanup
Complementary to (25/51) JSR305 annotations to help null analysis
New in Eclipse Mars, released yesterday! (24/03/2015).
Useful if you often switch between workspaces.
Under General/Workspace, you can set the workspace name field.
FROM ndeloof/java
MAINTAINER Baptiste Mathus <batmat@batmat.net>
RUN curl http://ftp.halifax.rwth-aachen.de/eclipse//technology/epp/downloads/release/mars/R/eclipse-java-mars-R-linux-gtk-x86_64.tar.gz | tar -xvz
WORKDIR /eclipse
RUN ./eclipse -nosplash -application org.eclipse.equinox.p2.director \
-repository http://download.eclipse.org/releases/mars \
-installIUs org.eclipse.egit.feature.group,org.eclipse.jgit.feature.group
RUN ./eclipse -nosplash -application org.eclipse.equinox.p2.director \
-repository http://download.eclipse.org/releases/mars \
-installIUs org.eclipse.m2e.feature.feature.group
CMD ["cp","-R","/eclipse","/eclipse-provisioned"]
/