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

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

About these ads
Categories: Uncategorized Tags: , , ,
  1. Jiri Pejchal
    18/03/2013 at 22:26

    This should be included in netbeans by default.

  2. Christian P.
    22/03/2013 at 08:29

    Hi,

    sorry for using this way to notify about an issue, but I don’t have a github account (and don’t wanna create one).

    I really like these hints and use ‘em often to clear out my code, but unfortunately there’s a “small” problem. Imagine the following code:

    int a = 5;
    String string = “foo”;
    double d = .5d;

    String out = a + ” is an integer and \”” + string + “\” is a string and ” + d + ” is an double”;

    Now, when you use “Replace ‘+’ with ‘String.format()’” the result is this:

    String out = String.format(“%s is an integer and ” % s);

    I think the problem is the escaped quote in the original string.

  3. sapibuntinx
    18/09/2013 at 08:28

    for all the Java Noob:
    Short messages and the like: System.out.println(“x:”+x+” y:”+y);
    The compiler transforms this to: System.out.println((new StringBuilder()).append(“x:”).append(x).append(” y:”).append(y).toString());

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: