I’ve always asked myself this question, particularly when i see how JPA 2.0 metamodel works…
For exemple in JPA 2.0 we can, with a processor, for an entity Entity, create a metamodel class Entity_
It’s then possible in JPA 2.0 Criteria api to use this metamodel to have strongly typed criterias.
For exemple, you would write:
criteriaBuilder.equal(u.get(User_.username), username);
Instead of using the fieldname as string “username”.
I just wonder why it’s not handled natively in Java, without having to deal with a processor and a couple of extra metadata classes.
It’s not just for JPA, it could leverage too the reflection api.
So are there reasons why we can’t access the metadata directly in the java classes?
Can someone provide a drawback of having such a feature?
I guess it may be a problem for encapsulation no?
For more infos about JPA2 metamodel:
http://www.inze.be/andries/2010/09/19/jpa2-metamodel-example/
Java 7 doesn’t support methods or fields/properties as first class objects. The reason these haven’t been added is to keep the language simple.
However, it appears to me that Oracle has taken a different view (at least for methods) and you will be able to have method references in Java 8 and possibly one of the JSRs using field/property references will be accepted and merged into Java 8 or 9.
Some articles on how method handles are currently support. Java 8 will add syntactic sugar for this functionality.
http://blogs.oracle.com/jrose/entry/method_handles_in_a_nutshell
http://java.sun.com/developer/technicalArticles/DynTypeLang/
http://www.java7developer.com/blog/?p=191