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.
The extends keyword can be used to subclass custom classes as well as built-in objects. Any constructor that can be called with new (which means it must have the prototype property) can be the candidate for the parent class.
A constructor in Java is simply a bundle of statements that are particularly useful for initializing the object with default values. For example, when you are declaring an object you may want the class variables to start off with some default value, right? Well, constructors are the ideal tool for that.
We use constructors to initialize the object with the default or initial state. The default values for primitives may not be what are you looking for. Another reason to use constructor is that it informs about dependencies.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With