A Java
library to programmatically return the name of fields similar to the C# nameof
expression
The library is part of the Central Repository and can simply be used like this:
<dependency>
<groupId>de.mobiuscode.nameof</groupId>
<artifactId>nameof</artifactId>
<version>1.0</version>
</dependency>
implementation 'de.mobiuscode.nameof:nameof:1.0'
If you have a Java Bean like this:
public class User {
private String userName;
private String firstName;
private String lastName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
you can use this method to get the name of a field by its getter:
Name.of(User.class, User::getFirstName);
This will return the string "firstName"
, just like the C# expression nameof(firstName)
would do.
This is particularly handy in case you are refactoring the name of that field, because then the name of the getter would also be adjusted and with it the Name.of()
method would return the new field name.