1

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.)

Julien May
  • 2,011
  • 15
  • 18
  • When talking about properties and reflection on Java beans, the first thing that comes to my mind is the [Apache Commons BeanUtils](https://commons.apache.org/proper/commons-beanutils/) lib. But I don't know if you can solve your exact problem by using it. – Gilberto Torrezan May 21 '15 at 21:17

1 Answers1

1

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");
James
  • 11,654
  • 6
  • 52
  • 81