java - How to teach eclipse to generate compact equals() and hashCode() from the jdk 7 Objects class? -
some days ago switched java 7 within company - finally! jay \o/ found out objects
class , astonished how short methods hashcode()
, equals()
realized, reducing lot of boylerplate code compared ones generated eclipse per default (alt+shift+s --> h).
i wondering if change default behaviour of eclipse generated hashcode()
, equals()
?
i'd love see this:
@override public int hashcode() { return objects.hash(one, two, three, four/*, ...*/); }
instead of this:
@override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((one == null) ? 0 : one.hashcode()); result = prime * result + ((two == null) ? 0 : two.hashcode()); result = prime * result + ((three == null) ? 0 : three.hashcode()); result = prime * result + ((four== null) ? 0 : four.hashcode()); // ... return result; }
the same goes equals()
. this article got from.
any ideas how realize best?
in eclipse preferences go java > editor > templates.
in there can create new template. pattern like:
@override public int hashcode() { return objects.hash(one, two, three, four/*, ...*/); }
i'm not sure if there's variable enumerate fields however.
you might want @ some further explanations on these templates
Comments
Post a Comment