I am looking for a library which gives generated getter method name by looking up variable name, I can do it my own naively through get+change the first letter of variable name to upperCase and then append to it.
-
Probably easiest to go through `BeanInfo`, see [here](https://docs.oracle.com/javase/8/docs/api/java/beans/Introspector.html). – Kayaman Aug 07 '23 at 15:26
1 Answers
There are 2 conflicting beanspecs.
One is simple and just says: Take the name, uppercase the first letter, slap set
in front for setters, and slap is
or get
in front for getters.
However, there are real issues with that:
- Does
Boolean
(note the capital!) implyisFoo()
orgetFoo()
? Most beanspec implementations think the answer isgetFoo()
, but humans tend to think/expectisFoo()
. - Uppercase is just plain wrong. The correct way to do it, is of course, titlecase. If all you know is english, there is no difference between these two concepts. However, For example, FF ligatures exist. Lowercase, that's a single symbol that looks like ff, uppercase, that'd be FF, but titlecase, that looks like Ff. How does one deal with the fact that a spec strongly suggests a stupid thing? Some implementations decide to implement the spirit and not the letter - and thus invoke
toTitleCase
to generate it, some implement the letter and not the spirit, and invoke toUpperCase. Leaving a world split in twain - with some beanspec things expecting / generatingFf
, and others generatingFF
. - What do you do with a name that consists of multiple camel-cased-together parts, where the first part consists of a single letter? Imagine I have
public class Chart {}
and one of its fields is calledString xAxisUnit
. If I beanspec that in a very very simple way the way you described it, I getgetXAxisUnit()
. And indeed the first release of beanspec says that's right. But later, different beanspec docs say that the right way to 'getify' that is actuallygetxAxisUnit()
, for complicated reasons. Now again we are faced with a dilemma - which of the various conflicting beanspecs do you want? (For context as to why this occurs and which docs are out there that are in conflict, I suggest you read through this lombok issue about the problem) - all the relevant links, as well as direct quotes from the various specs, are present there. There's also this StackOverflow question.
Thus, the real question becomes: Which spec would you care to follow?
Here's lombok's CapitalizationStrategy
- it's only half a page and covers the capitalization part for both (you're going to have to figure out the set
/is
/get
part). In case you don't want to rely on java.beans.Introspector
because it doesn't properly deal with all these variations.
TL;DR: If you have variable names that starts with a letter that has a different titlecase than uppercase, you're in trouble. If you have properties whose type is capital-B Boolean
, that's problematic. It's also problematic if you have a field whose first 'word' is only a single letter long: Various beanspecs have different opinions on how to convert those from getFoobar
to foobar
form and back.

- 85,357
- 5
- 51
- 72
-
Thanks for the very detailed explanation on this, I am not going into much complexity of it, i just need get+Capitalise first letter. Is there any library which completes my task – user3309305 Aug 07 '23 at 18:19
-
1There is no _just_, you can't dodge this complexity. Alternative if you insist, in general there are no libraries that openly accept doing completely the wrong thing in many scenarios, so, no, there is no library that does the simple and incomplete half-baked crud you want to do, no. And there is likely to never be. – rzwitserloot Aug 07 '23 at 22:48