Method Interception to get property name

72 views Asked by At

I am looking for a utility class or library that gives me the name of the property in a type-safe way. I have something similar like the following in mind:

PropertyDescriptor descriptor = property(on(Foo.class).getBar());

assertThat(descriptor.getName()).isEqualTo("bar")

To have such a convenience method implemented properly requires IMHO quiet a lot of work. As the handling of final classes and the like can be extremely complex (see mockito, easymock etc.)

1

There are 1 answers

0
James On

You could use QueryDSL's aliases as:

import static com.querydsl.core.alias.Alias.*

Foo foo = alias(Foo.class, "foo");
assertThat($(foo.getBar().getBaz()).getMetaData().getName()).isEqualTo("foo.bar.baz");