Method and Field Literals

arjan tijms arjan.tijms at gmail.com
Sat Nov 22 15:51:56 UTC 2014


>FWIW, method/field literals would be very useful for POJO-centric libraries
such as JPA/Hibernate, Bean Validation etc.

Absolutely, in fact JPA is now simulating Field Literals by means of
generating a so-called Metamodel for every entity class. See
https://docs.oracle.com/javaee/6/tutorial/doc/gjiup.html#gjivl

In short, given a class:

public class Foo {
    public String bar;
}

JPA generates a class Foo_:

@Static @Metamodel(Foo.class)
public class Foo_ {
    public static volatile SingularAttribute<Foo, String> bar;
}

Everywhere a Field Literal would be actually needed, we then use
Foo_.bar, half telling ourselves that there's the imaginary operator
"_.".

While this works, it's a nasty workaround and for some reason it's
always an issue to generate that Metamodel. Real Field Literals would
make this so much easier.

Kind regards,
Arjan Tijms


More information about the discuss mailing list