14

I wanted to know if anyone knows about a good way to update,constructors, equals,hash,to string, etc. generated by eclipse in Java. Lot's of time, after I use the auto-generated code-stubs, I add a member variable to the class, and then I need to delete the auto-generated code, and do it all over again. Is there a way to cause eclipse to add the new variable to the auto-generated code-stubs ?

edit: ok deleting is not essential, however I still have to go and generate each on of them, I'm looking for an automatic solution.

stdcall
  • 27,613
  • 18
  • 81
  • 125
  • I think its much more important to have it correct than auto generated. The Apache HashCode answer is the way to go. – JohnKlehm Oct 05 '11 at 12:43

3 Answers3

6

This isn't exactly a solution to your question, but I no longer use the Eclipse auto-generated methods, I use the Apache commons lang EqualsBuilder and HashCodeBuilder:

So, for instance you can do:

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public class EqualsTest {
    private String foo;
    private int bar;

    // getters and setters

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
}

This uses reflection, and doesn't need changing when you add a field. However, there are other options where you can specify the fields to use, and if you want to take into account the hashCode of the superclass as well.

EDIT: As has been pointed out, the reflection aspect of this may have some performance penalties associated. Personally, I don't use the reflection HashCodeBuilder or EqualsBuilder in production code, I use the toHashCode (as below). I do however use the ReflectionToStringBuilder for logging and such like.

Here is an example which doesn't use reflection, but requires you to add another line when you add a field:

public int hashCode() {
  // you pick a hard-coded, randomly chosen, non-zero, odd number
  // ideally different for each class
  return new HashCodeBuilder(17, 37).
    append(foo).
    append(bar).
    toHashCode();
}

For more discussion about hashCodeBuilder, see apache commons equals/hashcode builder

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Well if your for objects that are immutable -which I usually attempt to do- you could cache the values for toString and hashCode that would help a lot. – Sled Oct 05 '11 at 12:29
  • @Ernest It's not bad actually, it depends what you're doing. The advantages of not having to generate your own hashCode() and using a standard, and above all, correct hashCode outweigh any minor performance problems. – Matthew Farwell Oct 05 '11 at 12:29
  • After I wrote my comment I realized that maybe `EqualsBuilder` generates code, loads it and caches it; that could perform perfectly well. – Ernest Friedman-Hill Oct 05 '11 at 12:31
  • I know the apache commons framework, however, this is not a solution, but rather trying to solve the issue by avoiding the use of auto-generators... – stdcall Oct 05 '11 at 13:05
  • @Ernest, in fact the reflection methods get the field values by reflection and create a builder, and then executes it. This is true for HashCodeBuilder & EqualsCodeBuilder. There isn't any caching done. – Matthew Farwell Oct 07 '11 at 15:26
4

Take a look at www.projectlombok.org as an alternative to writing these methods yourself. In particular the @Data annotation seems to fit your need, see http://www.projectlombok.org/features/Data.html.

Kevin
  • 24,871
  • 19
  • 102
  • 158
  • This is nice. but it has a drawback. when referencing sets, gets, etc. from other class, eclipse won't know how to auto-complete (using CTRL + SPACE) because theses methods are generated on the fly... – stdcall Oct 05 '11 at 16:17
  • interesting, i'll check it out – stdcall Oct 05 '11 at 19:57
2

Iv'e created a project of my own with one field and asked eclipse to generate all the base methods. After that I added a new field, I asked it to generate these methods again (source -> generate...), it prompted me about replacing the old ones, I clicked 'yes' and the updated methods were displayed.

Hope it helped

Maor Veitsman
  • 1,544
  • 9
  • 21
  • It didn't. I want it to be updated automatically, or with a button push. as you said, I have to do it all over again for all the stubs – stdcall Oct 01 '11 at 06:40