Closed GoogleCodeExporter closed 9 years ago
So basically you're looking for a Guava equivalent of Python's
`operator.methodcaller` (which would have to implement `Function`)?
For the sake of completeness, `operator.attrgetter` and `operator.itemgetter`
also belong to the team.
However, I'm (generally) a bit worried about using reflection as that breaks
some links that allow secure refactoring through IDEs and stuff.
On the other hand: In the end, it all comes down to Java's verbosity for
defining anonymous functions, and JDK 7 (or, as it seems, 8) is quite far away
again.
Original comment by j...@nwsnet.de
on 13 Sep 2010 at 8:25
I think breaking refactoring is plenty reason enough to not do this. Something
I've been doing occasionally is defining a Function for a getter as a public
static final field in the class the method is defined in. Then you can
reference the getter like "Bar.GET_FOO" which looks reasonably nice.
And yeah, it's really unfortunate that lambda expressions won't be in Java til
mid-2012 at the soonest... when that's in, you'll be able to use method
references like "Bar#getFoo()" as Functions.
Original comment by cgdec...@gmail.com
on 13 Sep 2010 at 1:26
Broken refactoring does seem like a big a penalty to incur for the benefit.
I've seen the "Bar.GET_FOO" used a lot too, sometimes named "Bar.EXTRACT_FOO".
Original comment by boppenh...@google.com
on 23 Sep 2010 at 4:43
In fact, I started using Bar.GET_FOO as well, as I don't use too many getters
anyway and initializing Bar.GET_FOO in Bar.java doesn't make the join statement
more obscure to read (as an in-place definition of an anonymous class would do).
I guess I'm looking forward to JDK 7 or 8 then...
Original comment by andre.ho...@gmail.com
on 23 Sep 2010 at 5:55
Original comment by fry@google.com
on 28 Jan 2011 at 8:27
This issue has been migrated to GitHub.
It can be found at https://github.com/google/guava/issues/<id>
Original comment by cgdecker@google.com
on 1 Nov 2014 at 4:15
Original comment by cgdecker@google.com
on 3 Nov 2014 at 9:09
Original issue reported on code.google.com by
andre.ho...@gmail.com
on 12 Sep 2010 at 8:25