Example usage for java.lang Class getEnumConstants

List of usage examples for java.lang Class getEnumConstants

Introduction

In this page you can find the example usage for java.lang Class getEnumConstants.

Prototype

public T[] getEnumConstants() 

Source Link

Document

Returns the elements of this enum class or null if this Class object does not represent an enum type.

Usage

From source file:Programming.java

public static void main(String[] args) {
    Class cls = Programming.class;

    // returns the elements of this enum class
    for (Object obj : cls.getEnumConstants()) {
        System.out.println(obj);//from   ww w .j a v a  2 s  .com
    }
}

From source file:Eon.java

public static void main(String... args) {
    try {//from  www.  ja va2 s. co  m
        Class<?> c = (args.length == 0 ? Eon.class : Class.forName(args[0]));
        out.format("Enum name:  %s%nEnum constants:  %s%n", c.getName(), Arrays.asList(c.getEnumConstants()));
        if (c == Eon.class)
            out.format("  Eon.values():  %s%n", Arrays.asList(Eon.values()));

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:org.beer30.jdefault.JDefaultUtils.java

public static <T extends Enum<?>> T randomEnum(Class<T> clazz) {
    int x = RandomUtils.nextInt(clazz.getEnumConstants().length);
    return clazz.getEnumConstants()[x];
}

From source file:org.openmrs.module.amrsreports.db.hibernate.type.MohStringEnumReflector.java

/**
 * All enum constants (instances) declared in the specified class.
 * //from   w w w  . j a va  2s. c  om
 * @param enumClass Class to reflect
 * @return Array of all declared EnumConstants (instances).
 */
private static <T extends Enum<T>> T[] getValues(final Class<T> enumClass) {
    return enumClass.getEnumConstants();
}

From source file:Main.java

public static <ENUM extends Enum> ENUM fromValue(Class<ENUM> aClass, String value) {
    try {// w w  w . jav  a  2 s. c om
        for (ENUM c : aClass.getEnumConstants()) {
            Field field = aClass.getField(c.name());

            XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);

            if (annotation.value().equals(value)) {
                return c;
            }
        }
    } catch (NoSuchFieldException e) {
    }
    throw new IllegalArgumentException(value);
}

From source file:com.dianping.lion.util.EnumUtils.java

/**
 * ????/*from  w  ww  .  j  av  a 2s  .  co m*/
 * @param <T>
 * @param enumClass
 * @param constantName
 * @return
 */
public static <T extends Enum<T>> T fromEnumConstantName(Class<T> enumClass, String constantName) {
    T[] enumConstants = enumClass.getEnumConstants();
    for (T t : enumConstants) {
        if (((Enum<?>) t).name().equals(constantName)) {
            return t;
        }
    }
    return null;
}

From source file:cherry.foundation.type.EnumCodeUtil.java

public static <C, E extends Code<C>> Map<C, E> getMap(Class<E> type) {
    checkArgument(type.getEnumConstants() != null, "%s does not represent an enum type.", type.getSimpleName());
    Map<C, E> map = new LinkedHashMap<>();
    for (E e : type.getEnumConstants()) {
        map.put(e.code(), e);//  w w w .ja v a  2s.co  m
    }
    return map;
}

From source file:cherry.foundation.type.EnumCodeUtil.java

public static <C, E extends Code<C>> List<LabeledCode<C, E>> getLabeledCodeList(Class<E> type) {
    checkArgument(type.getEnumConstants() != null, "%s does not represent an enum type.", type.getSimpleName());
    List<LabeledCode<C, E>> list = new ArrayList<>();
    for (E e : type.getEnumConstants()) {
        list.add(getLabeledCode(e));/*from   ww w .j a  v  a 2  s .  c o  m*/
    }
    return list;
}

From source file:org.sejda.conversion.EnumUtils.java

/**
 * @param enumClass//from  ww  w . j  a  v  a 2 s. c o m
 * @param displayName
 * @return Returns the enum value matching the input displayName, belonging to the specified enum class
 *         Does not throw an exception if enum value is not found, returns null
 */
public static <T extends Enum<?> & FriendlyNamed> T valueOfSilently(Class<T> enumClass, String displayName) {
    for (T each : enumClass.getEnumConstants()) {
        if (StringUtils.equalsIgnoreCase(each.getFriendlyName(), displayName)) {
            return each;
        }
    }

    return null;
}

From source file:cherry.foundation.type.EnumUtil.java

public static <E extends Enum<E>> List<LabeledEnum<E>> getLabeledEnumList(Class<E> type) {
    List<LabeledEnum<E>> list = new ArrayList<>();
    for (E e : type.getEnumConstants()) {
        list.add(getLabeledEnum(e));//from  w  ww .j av a  2  s .  c o  m
    }
    return list;
}