I’ve always been fan of tools for automation of development and testing. I’ve used SonarQube for a long time, and even connect it to my IDE (usually Eclipse), so that I can act on any warnings for code as I’m working on it.
SonarLint takes that to a new level, as it gives notifications before the code is even commited for SonarQube to analyze.
While the instructions here are for Eclipse, SonarLint is also available for IntelliJ IDEA, VisualStudio, and as a command line tool for download from the website.
Eclipse Update Site:
Most Java developers are familiar with the Eclipse IDE, even if they use alternatives.
One thing that’s bothered me for some time is that while JUnit is natively supported in Eclipse, TestNG is not. Thankfully, you can easily add this functionality.
- Help; Eclipse MarketPlace;
- Find – enter: testng
- Click on "TestNG for Eclipse" and install by stepping through the prompts.
- Restart of Eclipse is required to enable the new functionality.
While the intgration plugins for most SCM products are done rather simply within Eclipse, MKS(now PTC) IntegrityClient requires a few manual steps.
Caution, the plugin has always been a bit “buggy” but nothing too annoying for daily use. I’ve personally used it since IBM WSAD 5.1(Eclipse 3.x based) up to and including the most current Eclipse Luna (4.4) release.
- Go to your MKS installation path, then find the “integrations” folder as in the example below:
- That folder SHOULD have two folders (features/plugins) as well as three files (.eclipseextension, artifacts.xml, content.xml)
- Copy the entire contents and paste/overlay into your ‘eclipse’ folder (where you should already have folders for ‘features/plugins)
- Restart Eclipse
- Configure as required.
If you have already embraced Continuous Inspection with Sonar/SonarQube, you may find it advantageous to do analysis of new or modified code within the IDE without having to wait for a new build/compile/analyze cycle. Additionally, it’s often faster to keep all of your required information within the IDE without having to also open a web browser.
You can easily add this capability:
- Help > Install New Software… > Add… > then enter:
- Restart Eclipse
- RightClick on project – Configure > Associate with SonarQube > chose project
I’ve been a huge fan of the ‘
final‘ modifier in Java for function arguments and variables. While there’s some debate on their usefulness for performance, they definitely add in readability and understanding of code as developers are instantly notified by modern IDE’s and when they try to reassign such values as the compiler will indicate the error.
NOTE: Use of final for classes or functions often contradicts the paradigm of Object Oriented programming as you’ll be restricted from extending or overriding those items!
I’ve found that many developers still use the classic coding style on JSP’s, unfortunately this makes it difficult to use some common tools for validation and complicates matters when looking for improperly nested tags in the markup. Migrating the XML formatted JSP markup simplifies matters and makes it possible for developers to quickly identify many problem areas of code within the IDE.
<%@ page language="java" %> = <jsp:page.directive language="java" />
<%@ page contentType="text/html; charset=utf-8" %> = <jsp:page.directive contentType="text/html; charset=utf-8" />
<%@ page import="" %> = <jsp:page.directive import="" />
NOTE: you can combine page.directive’s to a single tag with all attributes.
<% //some scriptlet %> = <jsp:scriptlet>//some scriptlet</jsp:scriptlet>
<%! String somevalue="1"; %> = <jsp:declaration>String somevalue="1";</jsp:declaration>
<%= somevalue %> = <jsp:expression>somevalue</jsp:expression>
<jsp:include page="" />
<jsp:directive.include file="" />
<jsp:useBean id="" scope="" type="" />
<jsp:setProperty name="" />
Unfortunately, there’s one common type of tag that does not have an XML equivalent:
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld" %>
I’ve previously written on the benefits of static analysis of java code with the use of PMD and FindBugs. I was recently turned on to a new tool that performs similar testing of code within the Eclipse IDE.
When I first found this tool it was free, since that time it’s come out of beta and is now a little costly, but it may still be worth it due to the functionality it provides.
The premise of this tool is a little different than other ones, while it covers much of the same need, it also performs many tests that I would previously use CheckStyle to do. This only provides them at runtime and in a common manner within the IDE.
I typically use the open-source Eclipse IDE for most of my Java and PHP work. For my corporate work, this means that I use IBM‘s packaged RAD and WSAD offerings that are based on various versions of the Eclipse framework.
When working on Internationalized (I18n) applications, most experienced Java architects rely on ResourceBundles to store the various text that is needed for different languages, problem is that editing these files becomes problematic, especially when dealing with multi-byte character sets as are often used in Unicode (non Latin-1, aka ISO-8859-1) languages.
The best editor I’ve found for this case is, as you may have guessed, free for download.
Here’s the links:
I recently found out about ‘static analysis’ of Java code. I’ve found two of these tools that are both free and easy to use. Both provide review of java bytecode and look for common development errors and inefficiencies…
FindBugs is based on the concept of bug patterns. A bug pattern is a code idiom that is often an error. Bug patterns arise for a variety of reasons:
- Difficult language features
- Misunderstood API methods
- Misunderstood invariants when code is modified during maintenance
- Garden variety mistakes: typos, use of the wrong boolean operator
PMD scans Java source code and looks for potential problems like:
- Possible bugs – empty try/catch/finally/switch statements
- Dead code – unused local variables, parameters and private methods
- Suboptimal code – wasteful String/StringBuffer usage
- Overcomplicated expressions – unnecessary if statements, for loops that could be while loops
- Duplicate code – copied/pasted code means copied/pasted bugs
Both integrate easily within Eclipse based (and other IDE’s) is typically done with the use of a simple plugin.
FindBugs can also run as a Java WebStart (JNLP) application, however a different UI is shown for JRE 1.4 vs. 1.5 and above (look out!).
While no tool can identify all problems, these will help you find some troublesome problems and give you areas to take a deeper look at.
Happy reviewing and fixing.