New version of Additional Java Hints plugin – 1.2.0

FYI: Recently I released the new version 1.2.0 of the “Additional Java Hints” plugin. There are some new hints/small quickfixes, which may be useful (or not) for Java developers.

  • [New Hint]: Change the modifier of a class/method/field to public/package protected/protected/private (by rasa-silva)
  • [New Hint]: Remove public/abstract/final modifiers from field declarations within interfaces
  • [New Hint]: Convert from char and string and back
  • [New Hint]: Convert number in literal to number and back
  • [New Hint]: Convert to StringUtils.isBlank()/StringUtils.isNotBlank()/StringUtils.isEmpty()
  • [New Hint]: Convert from if/else to ternary and back
  • [New Hint]: Invert ternary if/else
  • [New Hint]: Report methods that have class name
  • [Updated Hint]: “Convert to assertTrue/assertFalse” now supports junit.framework.Assert too
  • [Updated Hint]: “Replace +…” hints can now be configured

screenshot

The plugin is verified for NB 8.0 and NB 7.4 and already available from the plugin dialog within your NetBeans IDE. Or you can download it from http://plugins.netbeans.org/plugin/47589/additional-hints

As always please file issues and enhancements at https://github.com/markiewb/nb-additional-hints/issues . Pull request are also appreciated.

Advertisements

New version of Additional Java Hints plugin – 1.1.0

Another short note for today: I recently updated the “Additional Java Hints” plugin. There are some new hints/small quickfixes, which may be useful (or not) for Java developers. 

  • [New Hint]: Support transformation to BigDecimal constants
  • [New Hint]: Split a string at caret
  • [New Hint]: Convert to assertTrue/assertFalse
  • [New Hint]: Remove “public abstract” modifiers from method declarations within interfaces

Image

The plugin is verified for NB 7.3 and NB 7.4 and already available from the plugin dialog within your NetBeans IDE. Or you can download it from http://plugins.netbeans.org/plugin/47589/additional-hints

As always please file issues and enhancements at https://github.com/markiewb/nb-additional-hints/issues . Pull request are also appreciated.

NetBeans IDE: Additional hints plugin to simplify the work with String literals

Recently I wrote some Java hints, which simplify the work with String literals. Users of Eclipse or Intellij IDEA may recognize some of them.

New hints:

  • Replace ‘+’ with ‘MessageFormat.format()’
  • Replace ‘+’ with ‘new StringBuilder().append()’
  • Replace ‘+’ with ‘String.format()’
  • Join literals
  • Copy joined literals to clipboard
  • Split the String on linebreaks

screenshot

Here some use-cases:

Use-case 1: “Simplify concatenation”
"Hello " + "world" can be replaced by "Hello world"

Use-case 2: “Join for localization”
When localizing your application, you often have to struggle with concatenated Strings like the following:

"Info: only " + number + " entries found"

With the help of the hints such terms can be easily replaced by

String.format("Info: only %s entries found", number) //or 
MessageFormat.format("Info: only {0} entries found", number)

The generated joined literals can now be extracted more easily by the Internationalization wizard/tooling.

Use-case 3: “Developing test-cases for hints”

Copy joined literals to clipboard vs. pasting from clipboard
clipboard
Join literals vs. Split on linebreaks
splitonlinebreaks

The plugin is compatible to NetBeans 7.2 (and above) and verified, thus it can be installed directly from your IDE. Or you can download it from [1] and install it manually.

The sourcecode is available at [2]. The infrastructure code is based on code from the “I18N Checker” plugin from Jan Lahoda [3]. Feel free to file issues or enhancements at [2].

[2] http://plugins.netbeans.org/plugin/47589/additional-hints
[3] https://github.com/markiewb/nb-additional-hints
[3] http://hg.netbeans.org/main/contrib/file/tip/editor.hints.i18n

NetBeans IDE: Custom hints

Another mostly unnoticed but powerful feature of NetBeans is the possibility of creating custom hints.

Custom hints (a.k.a custom inspections or quickfixes in other IDEs) are available since NetBeans 7.1. They match patterns in Java code and can provide a transformation. This enables you to create small refactorings based on the AST without coding. A typical usecase is the type-safe migration of API – instead of the error-prone plain text-based “search and replace” approach.

You can create your own hint in a special DSL as described in [1],[2],[3]. Geertjan already blogged several times about this topic at [4] and [5]. Yes, the UI for creating/enabling the hints is not very intuitive, but it works.

The best thing is the integration in the editor:

Your custom hints

  • are available in the Java editor like the “standard” hints 2013-02-12_23h45_13
  • can be applied to a file/package/project or even all opened projects. It includes also a changes-preview. This is pretty useful to do apply transformations in a batch.2013-02-12_23h44_25

Here some very basic examples:

<!description=&quot;Convert to assertTrue/assertFalse&quot;>
org.junit.Assert.assertEquals(true, $any)
=> org.junit.Assert.assertTrue($any)
;;
org.junit.Assert.assertEquals($msg, true, $any)
=> org.junit.Assert.assertTrue($msg, $any)
;;
org.junit.Assert.assertEquals(false, $any)
=> org.junit.Assert.assertFalse($any)
;;
org.junit.Assert.assertEquals($msg, false, $any)
=> org.junit.Assert.assertFalse($msg, $any)
;;
<!description=&quot;Remove dead if-branch&quot;>
if (false){
$statements$
}
=>
;;
<!description=&quot;Add extra parenthesis&quot;>
$a && $b
=>
($a) && ($b)
;;
$a || $b
=>
($a) || ($b)
;;
<!description=&quot;Flip operands&quot;>
$a & $b
=>
$b & $a
;;
$a && $b
=>
$b && $a
;;
$a || $b
=>
$b || $a
;;
<!description=&quot;Flip &lt; &gt;&quot;>
$a < $b =>
$b > $a
;;
$a > $b
=>
$b < $a
;;
<!description="JUL to SLF4J">
$jul.info($msg) :: $jul instanceof java.util.logging.Logger && $msg instanceof java.lang.String
=> org.slf4j.Logger.info($msg)
;;

Those patterns can also be used to code more advanced hints [7] [8].

Give it a try and share your patterns! Which selfmade hints do you use on a regular basis?

FYI: Intellj IDEA provides a similar feature called “structural replace” [9]

Update from 2015/02: Added new links

References:
[1] http://wiki.netbeans.org/JavaDeclarativeHintsFormat
[2] https://bitbucket.org/jlahoda/jackpot30/wiki/RulesLanguageAdditionalDocs
[3] https://bitbucket.org/jlahoda/jackpot30/wiki/RulesLanguage

[4] https://blogs.oracle.com/geertjan/entry/custom_declarative_hints_in_netbeans
[5] https://blogs.oracle.com/geertjan/entry/oh_no_override_oh_no
[6] https://blogs.oracle.com/geertjan/entry/oh_no_vector

[7] https://blogs.oracle.com/geertjan/entry/java_hint_in_netbeans_for
[8] https://blogs.oracle.com/geertjan/entry/netbeans_java_hints_quick_dirty

[9] http://www.jetbrains.com/idea/documentation/ssr.html