mobiuscode-com / nameof

Library to programatically return the name of fields similar to the C# nameof operator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nameof()

Maven Central

A Java library to programmatically return the name of fields similar to the C# nameof expression

How to use?

The library is part of the Central Repository and can simply be used like this:

Maven

<dependency>
    <groupId>de.mobiuscode.nameof</groupId>
    <artifactId>nameof</artifactId>
    <version>1.0</version>
</dependency>

Gradle

implementation 'de.mobiuscode.nameof:nameof:1.0'

Example

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.

About

Library to programatically return the name of fields similar to the C# nameof operator

License:MIT License


Languages

Language:Java 100.0%