Java Reflection Enum getOptionsFromEnum(Class> enumClass)

Here you can find the source of getOptionsFromEnum(Class> enumClass)

Description

Get the label and value for select options of forms from the given enum class.

License

Apache License

Parameter

Parameter Description
enumClass the enum class

Return

a map of label and value

Declaration

public static Map<String, String> getOptionsFromEnum(Class<? extends Enum<?>> enumClass) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**//from   ww w .j a  v a 2  s. c o m
     * Get the label and value for select options of forms from the given enum class.
     * 
     * @param enumClass the enum class
     * @return a map of label and value
     */
    public static Map<String, String> getOptionsFromEnum(Class<? extends Enum<?>> enumClass) {
        Map<String, String> map = new HashMap<String, String>();
        Enum<?>[] enumConstants = enumClass.getEnumConstants();
        for (Enum<?> enumConstant : enumConstants) {
            map.put(enumConstant.toString(), getLabel(enumConstant.toString()));
        }
        return map;
    }

    /**
     * Get the label from the given value.
     * 
     * @param value the value
     * @return the label
     */
    private static String getLabel(String value) {
        return value.substring(0, 1).toUpperCase() + value.substring(1);
    }
}

Related

  1. getEnumConstants(Class c)
  2. getEnumNames(Class> e)
  3. getEnumValues(Class type)
  4. isLegalEnum(Class clazz, Object enuValue)
  5. toEnum(Class e, String s)
  6. toStrings(Class clenum)