如何在 Java 中使用反射获取枚举字段名称?

How can I get Enum fields names using reflection in Java?

这是我的 class:

public enum Currency {
    NIS, USD, EUR, GBP, JPY, AUD, CAD, DKK, NOK, ZAR, SEK, CHF, JOD, LBP, EGP;

    private String name;
    private int unit;
    private String country;
    private double rate;
    private double change;
}

当我尝试使用反射获取此 class 的字段名称时,我得到了私有字段和枚举常量(NIS、EUR、....)

如何才能只获取私有字段名称?

这段代码应该可以做到(基于 bayou.io 的回答):

List<String> fields = Arrays.stream(Currency.class.getDeclaredFields())
        .filter(x -> !x.isEnumConstant() && !x.isSynthetic())
        .map(x -> x.getName())
        .collect(Collectors.toList());

它只是迭代所有字段,过滤那些既不是枚举常量也不是合成常量的字段(比如 $VALUES 字段)。

更新

根据您的评论,尝试以下操作。它也不会选择任何 public 字段,如果你有的话。

    Field[] allFields = Currency.class.getDeclaredFields();
    for (Field field : allFields) {
        if (Modifier.isPrivate(field.getModifiers())) {
            System.out.println(field.getName());
        }
    }

结果:

要从结果中删除 $VALUES,请检查该字段是否为合成字段

    Field[] allFields = Currency.class.getDeclaredFields();
    for (Field field : allFields) {
        if (Modifier.isPrivate(field.getModifiers()) && !field.isSynthetic()) {
            System.out.println(field.getName());
        }
    }

结果:

就为这些字段赋值和访问它们而言,您不需要进行反射。您只需要按如下所示为每个枚举适当地定义字段。

public enum Currency {
    NIS("Name1", 0, "NIS", 1, 2), 
    USD("Name2", 1, "USD", 1, 2), 
    EUR("Name3", 2, "EUR", 1, 2), 
    GBP("Name4", 3, "GBP", 1, 2), 
    JPY("Name5", 4, "JPY", 1, 2), 
    AUD("Name6", 5, "AUD", 1, 2), 
    CAD("Name7", 6, "CAD", 1, 2), 
    DKK("Name8", 7, "DKK", 1, 2), 
    NOK("Name9", 8, "NOK", 1, 2), 
    ZAR("Name10", 9, "ZAR", 1, 2), 
    SEK("Name11", 10, "SEK", 1, 2), 
    CHF("Name12", 11, "CHF", 1, 2), 
    JOD("Name13", 12, "JOD", 1, 2), 
    LBP("Name14", 13, "LBP", 1, 2), 
    EGP("Name15", 14, "EGP", 1, 2);

    private final String name;
    private final int unit;
    private final String country;
    private final double rate;
    private final double change;

    private Currency(String name, int unit, String country, double rate, double change) {
        this.name = name;
        this.unit = unit;
        this.country = country;
        this.rate = rate;
        this.change = change;
    }

    public String getName() {
        return name;
    }

    public int getUnit() {
        return unit;
    }

    public String getCountry() {
        return country;
    }

    public double getRate() {
        return rate;
    }

    public double getChange() {
        return change;
    }
}

它的用法是:

public static void main(String[] args) throws Exception {
    for (Currency currency : Currency.values()) {
        System.out.println("Name: " + currency.getName());
        System.out.println("Unit: " + currency.getUnit());
        System.out.println("Country: " + currency.getCountry());
        System.out.println("Rate: " + currency.getRate());
        System.out.println("Change: " + currency.getChange());
        System.out.println("");
    }
}

结果(未显示所有结果):

或者您可以直接使用枚举,如下所示:

System.out.println(Currency.AUD.getName() + " " + Currency.AUD.getCountry());

结果:

Name6 AUD

尝试测试Field.isEnumConstant()

您还可以检查 getModifiers() 并测试它是否是静态的、私有的等等。

也可能需要 isSynthetic() 进行测试。