Saturday, June 13, 2015

Eclipse Mars - how to switch back to previous Java formatter?

Update: the Luna formatter plugin now lives on GitHub, thanks to Asier Lostalé!

Java code formatter in Eclipse 4.5 has been completely rewritten. There's a lot less of bugs, the behavior is more consistent, and line wrapping is a bit smarter. It also opens way to easier implementation of new improvements in the future.
While most users will probably be happy with the new formatter, for some the changes may be unwelcome. Probably the most controversial change is a more restrictive approach to the "Never join already wrapped lines" option - a lot of line breaks that used to be tolerated by the old formatter, will be now removed if they don't fit the line wrapping settings. Also, some teams just don't want to force everyone to immediately switch to the newest Eclipse, so during the transition it would be problematic if part of the team used different formatter.
If you also find that problems related to changed formatter behavior outweigh the benefits of bug fixes and improvements, you'll be glad to hear that Eclipse Mars has a new extension point for Java formatter. So it's easy to take the code of the old formatter and wrap it in a plugin. I did just that for your convenience, the plugin can be downloaded here. Although I tested it a bit, the plugin is provided "as is" and I take no responsibility for anything that happens because of it. Just unzip the provided jar into the "plugins" directory of your Eclipse installation, restart, and in the Preferences -> Code Style -> Formatter page select Formatter implementation: Old formatter. Happy formatting!

Wednesday, September 23, 2009

New plug-in: scrolling with middle button

My favorite way to scroll site contents in internet browsers is to press middle mouse button and slightly move mouse up or down. It's usually more convenient than rotating mouse wheel or grabbing scrollbar. This feature is supported by every popular browser but it's also being added to other applications (even the primitive Notepad in Windows has it!). I got used to it so much that I really miss it in Eclipse.

So I've done a little investigation to check whether it would be possible to add such a feature to Eclipse workbench. It turns out it would be very hard to do, as there's no general API in SWT to programmatically invoke scrolling. Only limited number of custom widgets provide methods that can do it. Fortunately, StyledText which is used by all text editors in Eclipse is one of them. This allowed me to prepare a plug-in that adds MMB scrolling at least to text editors. It's not much, but better than nothing. If you're interested, please visit the project page or go directly for download.

I'll be trying to hack other popular editors and viewers, so the list of scrollable elements may get longer in next versions. If you know something that may be useful for this task, please let me know :)

Thursday, August 21, 2008

ToString() Generator Plug-in

You don't have to wait for Eclipse 3.5 to conveniently generate toString() method for any class. Here's a plug-in version, tested with Eclipse 3.2, 3.3 and 3.4.
Enjoy!

Friday, August 08, 2008

ToString() generator status update

Since my last post I've been working mainly on user documentation and jUnit tests. I managed to create an extension point for my generator. It lets you change not only the look of generated code, but also the way the templates are handled (so you can for example add your own tokens). I also tried to add toString() generation to code completion proposals, but there are some problems I can't solve right now. This and the fact that JDT guys are rather skeptic about this feature made me postpone the work on it.

So the project is now finished and waiting to be reviewed by JDT team. I can't wait for the results. If everything goes fine, it will be added to Eclipse 3.5. Meanwhile, as I have nothing better to work on (and official end of GSoC is August 18th), I'll try to make the generator a separate plugin so that it can be used with earlier versions of Eclipse.

Thursday, July 17, 2008

all bout toString

GSoC midterm evaluation is now behind us so I think it's a good occasion to sum up my progress. I want to describe what is already working and what I'm going to add. I hope this post will be also a good start for creating user documentation.

  1. Code styles


    Code style determines how the generated method works and what classes it uses. There are several code styles available to chose from the combo box in generator's dialog.

    • String concatenation


      This style uses simple sum expressions so it's very efficient and relatively easy to read and modify. Here's an example outcome in the simplest case:
      return "FooClass [aFloat=" + aFloat + ", aString=" + aString + ", anInt=" + anInt 
      + ", anObject=" + anObject + "]";

      With "Skip null values" option turned on, the code becomes a little harder to read:
      return "FooClass [aFloat=" + aFloat + ", "
      + (aString != null ? "aString=" + aString + ", " : "")
      + "anInt=" + anInt + ", "
      + (anObject != null ? "anObject=" + anObject : "") + "]";


    • StringBuilder/StringBuffer


      This style uses StringBuilder if project is compatible with JDK1.5 or later and StringBuffer otherwise.
      StringBuilder builder = new StringBuilder();
      builder.append("FooClass [aFloat=");
      builder.append(aFloat);
      builder.append(", aString=");
      builder.append(aString);
      builder.append(", anInt=");
      builder.append(anInt);
      builder.append(", anObject=");
      builder.append(anObject);
      return builder.toString();

      The "Skip null values" option doesn't obfuscate the code as much as previously:
      StringBuilder builder = new StringBuilder();
      builder.append("FooClass [aFloat=");
      builder.append(aFloat);
      builder.append(", ");
      if (aString != null) {
      builder.append("aString=");
      builder.append(aString);
      builder.append(", ");
      }
      builder.append("anInt=");
      builder.append(anInt);
      builder.append(", ");
      if (anObject != null) {
      builder.append("anObject=");
      builder.append(anObject);
      }
      return builder.toString();


    • String.format();


      This style is very pleasant with relatively short list of elements, but with longer ones it becomes hard to see which fields are associated with which variables. Unfortunately, the "Skip null values" option is ignored by this style.
      return String.format("FooClass [aFloat=%s, aString=%s, anInt=%s, anObject=%s]",
      aFloat, aString, anInt, anObject);

      For JDK1.4 and earlier, the code is slightly different:
      UPDATE: As I learned today, there's no String.format() in JDK 1.4, so MessageFormat.format() will be used instead:
      return MessageFormat.format("FooClass [aFloat={1}, aString={2}, anInt={3}, anObject={4}]", 
      new Object[] { Float.valueOf(aFloat), aString, Integer.valueOf(anInt), anObject });


    • Apache Commons-Lang ToStringBuilder


      When this style is chosen, format template is ignored because ToStringBuilder takes care the output string's format itself. Maybe it's a little less flexible, but the power of this solution is that you can easily change the style of all toStrings within the project without changing any actual object's toString method.
      ToStringBuilder builder = new ToStringBuilder(this);
      builder.append("aFloat", aFloat);
      builder.append("aString", aString);
      builder.append("anInt", anInt);
      builder.append("anObject", anObject);
      return builder.toString();

      Skipping nulls works this way:
      ToStringBuilder builder = new ToStringBuilder(this);
      builder.append("aFloat", aFloat);
      if (aString != null)
      builder.append("aString", aString);
      builder.append("anInt", anInt);
      if (anObject != null)
      builder.append("anObject", anObject);
      return builder.toString();


    • Spring Framework's ToStringCreator


      This style behaves the same as Apache ToStringCreator except it uses different class to create output string.


  2. Format templates


    This is a simple mechanism that allows you to change format of generated method's output string: beginning, ending, separator, and so on. There are four tokens to use:




    ${class.name}inserts the class name as a String
    ${member.name}inserts a member's name
    ${member.value}inserts a member's value
    ${otherMethods}this token must stand between the separator and the ending string

    This is the template used for all examples in the previous part of this post:
    ${class.name} [${member.name}=${member.value}, ${otherMembers}]

    And of course, output string for this template looks like this:
    FooClass[aFloat=1.0, aString=hello, anInt=10, anObject=null]


  3. I plan to define more tokens:

    • inserting super.toString() and hashCode() (now they can be printed the same way as other methods which is not convenient)

    • ${class.getName} to use this.getClass.getName() instead of plain string

    • two different tokens for printing method names with or without parenthesis at the end

    • I'm thinking about more type specific options, e. g. putting strings between quotation-marks, printing integers as hexadecimal, printing length of arrays and so on. But don't know how they should work...



  4. Arrays handling


    When "Ignore arrays' default toString()" option is switched on, generated toString() method lists items contained in arrays, for example
    intArray = [1, 2, 3, 5, 8, 13]
    instead of
    intArray = [I@9304b1

    This is realized differently according to chosen JDK compatibility. For JDK1.5 and later java.util.Arrays.toString() is used. For earlier versions, which do not have this method, a helper arrayToString() method is generated:
    private String arrayToString(Object array, int length) {
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append("[");
    for (int i = 0; i < length; i++) {
    if (i > 0)
    stringBuffer.append(", ");
    if (array instanceof Object[])
    stringBuffer.append(((Object[]) array)[i]);
    if (array instanceof float[])
    stringBuffer.append(((float[]) array)[i]);
    if (array instanceof int[])
    stringBuffer.append(((int[]) array)[i]);
    }
    stringBuffer.append("]");
    return stringBuffer.toString();
    }

    It takes object as a parameter and then uses instanceof so that one method can work for all kinds of arrays. It checks only these array types that are actually passed to it in the main toString() method, so in case new types are added, arrayToString is regenerated every time toString generator is run.

  5. Limiting number of items


    This option changes behavior of generated toString in case of Collections, Maps and Arrays(if "Ignore default arrays' toString()" option is on). Again, generated code differs for each JDK version.

    In JDK1.6 collection or map is turned into an array (collection.toArray() or map.entrySet().toArray()), then Arrays.copyOf() is used to make it shorter and Arrays.toString() is called to print it out. Of course, usually it's also necessary to check for nulls. All in all, the code becomes fairly complicated, with something like this in the worst case:
    builder.append(hashMap != null ? Arrays.toString(Arrays.copyOf(hashMap.entrySet().toArray(),
    Math.min(maxItem, hashMap.size()))) : null);

    At least there's no need to generate additional methods.

    Since Arrays.copyOf was introduced in JDK1.6, in earlier versions helper toString() methods must be used to limit number of elements. In case of arrays the method is similar to the one showed in the previous part, only with one more statement at the beginning: length = Math.min(lenght, maxItem);. For collections and maps there's another method:
    private String toString(Collection collection) {
    StringBuilder stringBuilder = new StringBuilder();
    final int maxItem = 10;
    stringBuilder.append("[");
    int i = 0;
    for (Iterator iterator = collection.iterator();
    iterator.hasNext() && i < maxItem; i++) {
    if (i > 0)
    stringBuilder.append(", ");
    stringBuilder.append(iterator.next());
    }
    stringBuilder.append("]");
    return stringBuilder.toString();

    }

    This method is not overwritten every time the generator runs so that it can be changed by user.

    Another solution would be to convert a collection into an array and then use arrayToString, but this way is more efficient and looks better if there are no arrays.

  6. Plans for the future


    In addition to things I mentioned earlier, I'm going to create an extension point for new code styles and add toString generation to code assist. This time I won't be able to copy solutions from hashCode/equals generator though, so the work may not go as smooth as earlier. Still, I don't loose my optimism :)

Monday, July 07, 2008

generator ready to use

The toString() generator reached the state where it can be useful. For now there's only one coding style - string concatenation, but I plan add more of them. There's support for templates and all the options visible in the dialog have effect on generated method. I'd love to hear opinions from people interested in the subject: are the options useful? are the generated methods well implemented? should something be added? should the templates mechanism be enriched?

Again, to see my work you can apply the latest patch from bug 26070 to the org.eclipse.jdt.ui plugin. I'm sorry if it's inconvenient but I use some internal Eclipse packages and creating a separate plugin would be too difficult for me. If you like, you can download a ready project, import it to Eclipse workspace and run as "Eclipse Application". Here's the project working under Eclipse 3.4 and here version for Eclipse 3.5.

Saturday, June 21, 2008

toString() generator will be in a separate dialog

Last week I did some work to make my code clean enough to post on Bugzilla. When I submitted it, Martin suggested that it's not a good idea to have toString() generator in the same dialog with hashCode()/equals(). I was surprised at first, as he was the first person to tell me so. But his arguments were strong: the dialog is too big and there's no good name for the action. His main argument was that hashCode/equals doesn't usually use the same fields as toString. I had doubts about it but I don't have enough experience to argue.
I've made a quick poll about it on the eclipse IRC channel - 100% of participants said that toString generation should not be combined with hashCode/equals (100% of 1 person - I guess IRC is not a good place for polls :P)
I tried to fit elements on the dialog to make it look better, but without satisfactory results (see here and here). So I decided to make a separate action and I won't get back to the previous idea unless there's *a lot* of people asking for it. I submitted a patch with the new action today and I'm waiting for feedback. If you're too lazy to check the patch, here's a screenshot of prepared dialog:

Plan for the next week: investigate possible ways of template handling. I know there's a rich template support in Eclipse but I'm afraid it may not be exactly what I need as these templates relate to the code directly. I saw a nice dialog with templates in Mylyn plugin (Preferences->Mylyn->Team) and I'm going to see how it's done. Do you have any suggestions of where else I should look?