get String Constant Values - Android java.lang.reflect

Android examples for java.lang.reflect:Field Value

Description

get String Constant Values

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static String[] getStringConstantValues(Class<?> clazz) {
        List<String> stringValues = new ArrayList<String>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();
            boolean isStaticFinal = Modifier.isStatic(modifiers)
                    && Modifier.isFinal(modifiers);
            boolean isString = field.getType().isAssignableFrom(
                    String.class);
            if (isStaticFinal && isString) {
                String value = (String) getFieldValue(field, null);
                stringValues.add(value);
            }//from w  ww  . ja va2 s  . c om
        }
        Class<?>[] supportedInterfaces = clazz.getInterfaces();
        for (Class<?> supportedInterface : supportedInterfaces) {
            String[] interfaceConstantValues = getStringConstantValues(supportedInterface);
            stringValues.addAll(Arrays.asList(interfaceConstantValues));
        }
        Class<?> parentClass = clazz.getSuperclass();
        if (parentClass != null) {
            String[] parentConstantValues = getStringConstantValues(parentClass);
            stringValues.addAll(Arrays.asList(parentConstantValues));
        }
        return stringValues.toArray(new String[stringValues.size()]);
    }

    private static Object getFieldValue(Field field, Object object) {
        try {
            field.setAccessible(true);
            return field.get(object);
        } catch (IllegalArgumentException e) {
            throw new IllegalStateException("Error getting field's value",
                    e);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Error getting field's value",
                    e);
        }
    }
}

Related Tutorials