e.g. with
class Foo {
Integer bar;
}
I wonder why there isn't a language feature that enables me to do
Foo.class.bar //yes, xxx.class returns something of java.lang.Class<T>
to refer to the meta field bar?
I'm reading the Pro JPA 2 Book and it seems to me the canonical metamodel generation is necessary, because this isn't possible in Java.
Note, this is a theoretical question out of curiosity, where I would like to gain some insights, why this feature wasn't implemented.
--- Update ---
To elaborate my question a bit more, consider the example of adding attributes in JPA by the Entity Graph API:
EntityGraph<Foo> g = myEntityManager.createEntityGraph(Foo.class)
g.addAttributeNodes("bar")
There is no formal link (for the compiler / the IDEs) between the string "bar" and Foo´s attribute bar
.
I would say that the main reason for the absence of such a feature is that it would not be useful.
Why would we ever ask for meta-information of a specific field in a specific class?
If we hardcode the class and field pair, then we already know all of the field's meta-information, because they can't be changed at runtime. What would be the purpose of the information obtained via
Foo.class.bar.getType()
when we already know that the result isInteger.class
?