Java Reflection Enum toStrings(Class clenum)

Here you can find the source of toStrings(Class clenum)

Description

to Strings

License

Open Source License

Declaration

static public String[] toStrings(Class<?> clenum) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

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

public class Main {
    static private final Map<Class<?>, String[]> strings = new HashMap<Class<?>, String[]>();

    static public String[] toStrings(Class<?> clenum) {
        synchronized (strings) {
            String[] result = strings.get(clenum);

            if (result == null) {
                try {
                    Enum<?>[] values = (Enum<?>[]) clenum.getMethod("values").invoke(null);
                    result = new String[values.length];

                    for (int i = 0; i < values.length; i++) {
                        result[i] = values[i].toString();
                    }/*from   w w w. j a va 2s  .  c  o m*/
                } catch (Exception e) {
                    result = new String[] { "EnumUtils.toStrings failure" };
                }
                strings.put(clenum, result);
            }

            return result;
        }
    }
}

Related

  1. getEnumNames(Class> e)
  2. getEnumValues(Class type)
  3. getOptionsFromEnum(Class> enumClass)
  4. isLegalEnum(Class clazz, Object enuValue)
  5. toEnum(Class e, String s)