Finding the name of a given constant using reflection The code below comes from the Arm_const class of my Android disassembler project:

// ARM condition code

public static final int ARM_CC_INVALID = 0;
public static final int ARM_CC_EQ = 1;
public static final int ARM_CC_NE = 2;
public static final int ARM_CC_HS = 3;
public static final int ARM_CC_LO = 4;
public static final int ARM_CC_MI = 5;
public static final int ARM_CC_PL = 6;
public static final int ARM_CC_VS = 7;
public static final int ARM_CC_VC = 8;
public static final int ARM_CC_HI = 9;
public static final int ARM_CC_LS = 10;
public static final int ARM_CC_GE = 11;
public static final int ARM_CC_LT = 12;
public static final int ARM_CC_GT = 13;
public static final int ARM_CC_LE = 14;
public static final int ARM_CC_AL = 15;

public static String getCCName(int cc)
{
    Class clazz=Arm_const.class;
    Field[] fields=clazz.getFields();
    for(Field f:fields)
    {
        String s=f.getName();
        if(s.contains("ARM_CC_"))
        try
        {
            if (((int)f.get(null))==cc)
            {
                return s.replace("ARM_CC_","");
            }
        }
        catch (IllegalAccessException e)
        {
            Log.e("arm","",e);
        }
        catch (IllegalArgumentException e)
        {}
    }
    return "";
}


The method getCCName returns the name of a constant from the declared public static final ints.

Examples:


3 → "HS"
12 → "LT"


Any suggestions to improve its performance (speed) are appreciated!
If performance is your concern I would implement getCCName as a lookup in a map that's filled when the class is loaded, e.g. like

private static final Map&lt;Integer, String&gt; _int2string;

static
{
    final Map&lt;Integer, String&gt; int2string = new HashMap&lt;&gt;();

    try
    {
        for (Field field: Arm_const.class.getFields())
        {
            final int mod = field.getModifiers();

            if (!int.class.equals(field.getType()))
                continue;

            if (!Modifier.isStatic(mod) || !Modifier.isPublic(mod))
                continue;

            if (!field.getName().startsWith("ARM_CC_"))
                continue;

            int2string.put(field.getInt(null),
                           field.getName().substring("ARM_CC_".length()));
        }
    }
    catch (IllegalAccessException l_e)
    {
        throw new RuntimeException(l_e); // should not occur
    }

    _int2string = Collections.unmodifiableMap(int2string);
}

public static String getCCName(int cc)
{
    return _int2string.get(cc);
}