Subscribe: Mati@GSOC
http://eclipse-n-mati.blogspot.com/feeds/posts/default
Added By: Feedage Forager Feedage Grade B rated
Language: English
Tags:
afloat  anint  anobject  append  astring  builder append  builder  code  eclipse  generator  null  string  style  tostring 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Mati@GSOC

Mati@GSOC



This blog is dedicated to my work on Google Summer of Code projects: the "toString() generation" project from 2008 and more recent "Tree views for Zest". Both projects are done for Eclipse.



Updated: 2017-10-26T15:56:18.062+02:00

 



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

2015-06-13T01:33:06.979+02:00

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!




New plug-in: scrolling with middle button

2009-09-23T23:16:21.291+02:00

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 :)



ToString() Generator Plug-in

2008-08-21T16:36:56.823+02:00

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!



ToString() generator status update

2008-08-08T12:47:31.948+02:00

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.



all bout toString

2008-07-18T00:50:16.867+02:00

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.Code stylesCode 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 concatenationThis 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/StringBufferThis 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 ToStringBuilderWhen 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 ToStringCreatorThis style behaves the same as Apache ToStringCreator except it uses different class to create output string.Format templatesThis 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 stringThis is the template used fo[...]



generator ready to use

2008-07-07T17:39:13.413+02:00

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.



toString() generator will be in a separate dialog

2008-06-21T21:24:17.339+02:00

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:
(image)
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?



First steps...

2008-06-08T21:53:51.928+02:00

It's the end of the semester here and I must work hard on my grades therefore I can't spend much time on my GSOC project (this situation will last until the last week of june). I managed to do some work this weekend though and here are the results:

1. I created a simple toString() generator to see if it will work. And it does :) For now it's only a string concatenationi with hardcoded format. I don't know how to put line breaks in generated code and I have no idea where to look for it. Any clues?

2. I decided to start with something simple - redesign hashcode/equals generation dialog to include toString() features. Here's the effect:

(image)
"Up" and "Down" buttons already work - I copied this functionality from "generate constructor using fields" dialog. The rest of the controls don't have any effect yet..
I plan to add a list of class methods to the view (in a separate node of the tree). With all methods on the list, the option to use getters instead of fields accessors is no longer needed (unless we want this option for hashCode and equals, but that's another story).

That's all for now, I know it's not much. But I think I can complete the project without many problems if I focus all of my attention on it next month.

BTW, Mike, should I put my code somewhere for everybody to see? Right now I modify my local copy of org.eclipse.jdt.ui plugin - is that ok?



toString() with templates? Hmm...

2008-05-23T21:48:36.988+02:00

As my blog has just joined Planet Eclipse, I'd like to say 'hi' to all the Eclipse fans :) For the next few months you'll probably be hearing from me about automatic toString() generation feature for Eclipse as this is my project for this year's Google Summer of Code.I'd like to start by sharing some dilemmas I have regarding the form of templates support in the generator. At first I wanted to use templates to set the format of the generated string, for example this template would create toString() returning a class name, followed by a comma separated list of values in one line:${class_name}: ${field_value}, ${other_fields}If every field should be placed in a separate line and have its name printed, the template would look like this:${class_name} ( ${field_name} = ${field_value} ${other_fields} [Super: ${super_string}])The ${super_string} element puts in the call to super.toString(). There could be other elements to call other common methods. The method generated with this template would look like this:public String toString() { return "FooClass (\n" + " field1 = " + field1 + " field2 = " + field2 + " [Super: " + super.toString() + "]\n" + ")";I thought this was fine until Mike Haller suggested that not only the format of generated string is important, but also the style of the implementation itself. Some might want to use string concatenation, others StringBuilder or ToStringCreator from the Spring Framework and so on. I'm still not completely convinced about this - string concatenation is IMO the easiest to read and probably the fastest implementation (java compiler turns '+' into StringBuilder.append()) - then why would someone need something else? But let's assume that multiple style support is necessary. One solution is to hardcode the most common implementations and let the user choose one in the generator's dialog box. But then there will always be unhappy users who need a style that was not implemented.The other solution is to use templates to determine the toString() method body itself. This approach is used by JUtils ToString Generator, but it's not very flexible. You cannot change the method behaviour for specific categories of fields, for example to skip null values (comparing primitives to null is an error). Considering this I realized that if there were some additional mechanisms making it more flexible, it would be more than just a toString() generator. It could easily replace almost all source generators currently available in Eclipse - a general purpose code generator useful always when you need to perform similar operations on multiple fields (or methods) of a class!My idea to accomplish this is to use two level template-tree. Every node would have attributes determining when it should be used. To show what I mean, I will use an example of hashCode() method. Here, the top level template (marked to use only with fields) would look like this:public int hashCode() { final int prime = 31; int result = 1; ${hashcode.sub1} ${hashcode.sub2} return result;}The subtemplates should be called twice in order to determine if there should be any separator between them (here the separator is just "\n\t"). There will be several children:result = prime * result + ((${field_name} == null) ? 0 : ${field_name}.hashCode()); (Marked for use with objects)result = prime * result + ${field_name}; (Marked for use with ints)result = prime * result + Float.floattoIntBits(${field_name}); (Marked for use with floats)result = prime * result + (${field_name} ? 1231 : 1237); (Marked for use with booleans)And so on...Can you see the potential of this mechanism? :) One more extension is template grouping, which allows to iterate over class fields more than once. Again, let me use an example - generate constructor using fields. Here's the top level template:public ${cla[...]



Preparing for coding

2008-05-22T19:51:55.259+02:00

The start of GSOC coding phase is getting closer and closer, so I started examining and debuging the Eclipse source. I'm beginning to understand how all the code generators work, although not without problems. The most difficult for me is the dialogs part - I would say that a little refactoring should be done here. Anyway, it's just GUI so I think it won't be so hard to handle it. As for the code generation itself, the operations seemed rather clear to me. Also, I'm going to read this article about abstract syntax tree to get more details.

After reading Remy's great article How to contribute to Eclipse projects I decided to give it a try and found this bug: 203132. It was pretty easy and referred to functionality I was just examining. But still I forgot to clean up some unused strings and had to send the patch three times. Well, at least I'll be more careful next time. Too bad it's too late to make changes in Eclipse 3.4. It would be much more fun if I didn't have to wait so long to see my code in HEAD CVS.



Welcome!

2008-05-13T17:36:02.140+02:00

In this blog I will be reporting my progress on the "toString() generation" project for Eclipse, which is part of this year Google Summer of Code.

Through this blog my mentors, Markus Kuppe and Mike Haller, will know what I'm doing and I hope to get some feedback from the Eclipse community.