get Static Property - Android java.lang.reflect

Android examples for java.lang.reflect:Field Static

Description

get Static Property

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    public static Object getStaticProperty(String className,
            String fieldName) {/*from  www . j  ava2s .co  m*/
        Object property = null;
        try {
            final Class<?> ownerClass = Class.forName(className);
            final Field field = ownerClass.getField(fieldName);
            property = field.get(ownerClass);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return property;
    }

    public static Field getField(Field[] fields, String fieldName)
            throws Exception {
        Field f = null;

        for (Field lf : fields) {
            String currentFieldName = lf.getName();
            if (currentFieldName.equals(fieldName)) {
                f = lf;
                break;
            }
        }

        if (f == null)
            throw new Exception(new String(fieldName + " field not found!"));

        return f;
    }
}

Related Tutorials