get Final Static Fields - Android java.lang.reflect

Android examples for java.lang.reflect:Field Static

Description

get Final Static Fields

Demo Code


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

public class Main {
    public static <Type> Type getFinalStatic(Class clazz, String fieldName,
            Class<Type> returnType) throws Exception {
        Field field = getClassField(clazz, fieldName);
        field.setAccessible(true);/*w  w  w.j  av  a  2  s. c om*/
        return returnType.cast(field.get(null));
    }

    public static Field getClassField(Class clazz, String fieldName)
            throws NoSuchFieldException {
        try {
            return clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            Class superClass = clazz.getSuperclass();
            if (superClass == null) {
                throw e;
            } else {
                return getClassField(superClass, fieldName);
            }
        }
    }
}

Related Tutorials