Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:Main.java

public static int getIntFieldIfExists(Class<?> klass, String fieldName, Class<?> obj, int defaultVal) {
    try {/*from   w  w  w  . ja v a 2  s . c  o m*/
        Field f = klass.getDeclaredField(fieldName);
        return f.getInt(obj);
    } catch (Exception e) {
        return defaultVal;
    }
}

From source file:Main.java

public static Field getFieldRecursiveLy(Class<?> clazz, String fieldName) {
    Field f = null;//w ww.  ja v  a  2  s  .c  o m
    try {
        f = clazz.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class<?> superClazz;
        while ((superClazz = clazz.getSuperclass()) != null && superClazz != Object.class) {
            try {
                f = superClazz.getDeclaredField(fieldName);
                break;
            } catch (Exception e2) {
                //ignore
            }
        }
    }
    if (f == null) {
        throw new RuntimeException(
                "can't find the field , class = " + clazz.getName() + " , fieldName = " + fieldName);
    }
    f.setAccessible(true);
    return f;
}

From source file:Main.java

/**
 * Decode a variable. Variables are in the form $varname. If the incoming
 * expression is not a variable or is not recognised it is simply returned
 * verbatim.//from ww w  . j  a v a 2 s.  c  om
 * @param in The incoming attribute
 * @return String
 * @throws Exception
 */
private static String decode(String in) throws Exception {
    if (in != null && in.length() > 1 && in.charAt(0) == '$') {
        String key = in.substring(1);
        if (key.charAt(0) == '#') {
            // It's a class name and reflection job in the form $$full.class.name.member
            int lastIdx = key.lastIndexOf('.');
            String member = key.substring(lastIdx + 1);
            String className = key.substring(1, lastIdx);
            Class<?> clazz = Class.forName(className);
            Field field = clazz.getDeclaredField(member);
            field.setAccessible(true);
            return String.valueOf(field.get(null));
        } else {
            String val = vars.get(key);
            if (val == null) {
                throw new Exception("Unknown variable " + in);
            } else {
                return val;
            }
        }
    } else {
        return in;
    }
}

From source file:Main.java

public static <T> T getConstantValue(final Class aClass, final String constantName) {
    try {// w  ww. java 2 s. c  o m
        return (T) aClass.getDeclaredField(constantName).get(null);
    } catch (NoSuchFieldException e) {
        Log.e("error", "can not find " + constantName + " in " + aClass.getName());
    } catch (IllegalAccessException e) {
        Log.e("error", constantName + " is not accessible");
    } catch (IllegalArgumentException e) {
        Log.e("error", "arguments error when get " + constantName);
    } catch (Exception e) {
        Log.e("error", "can not get constant" + constantName);
    }

    return null;
}

From source file:Main.java

public static <T> T getConstantValue(final Class aClass, final String constantName) {
    try {//  www .  j  av  a  2  s .c o  m
        return (T) aClass.getDeclaredField(constantName).get(null);
    } catch (NoSuchFieldException e) {
        Log.e("error", "can not find " + constantName + " in " + aClass.getName());
    } catch (IllegalAccessException e) {
        Log.e("error", constantName + " is not accessable");
    } catch (IllegalArgumentException e) {
        Log.e("error", "arguments error when get " + constantName);
    } catch (Exception e) {
        Log.e("error", "can not get constant" + constantName);
    }

    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E getProperty(Object object, String fieldName) {
    Class<?> clazz = object.getClass();
    while (clazz != null) {
        try {//  w  w w  .ja va2  s. co m
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            return (E) field.get(object);
        } catch (NoSuchFieldException e) {
            clazz = clazz.getSuperclass();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return null;
}

From source file:Main.java

public static Object getStaticFieldValue(@SuppressWarnings("rawtypes") final Class clazz, final String name)
        throws Exception {
    Field field = clazz.getDeclaredField(name);
    field.setAccessible(true);//from   w w w .  ja  va 2 s .co m
    return field.get(null);
}

From source file:Main.java

/**
 * An utility function that returns the menu identifier for a particular
 * menu item.//from  w  w w .  j  av  a 2 s.c o m
 * 
 * @param cls Class object of the class that handles the menu ite,.
 * @param identifier Menu identifier.
 * @return The integer corresponding to the menu item.
 */
public static int getMenuIdentifier(Class cls, String identifier) {
    int id = -1;
    try {
        Integer field = (Integer) cls.getDeclaredField(identifier).get(cls);
        id = field.intValue();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    return id;
}

From source file:Main.java

public static Object getField(Class<?> clazz, String fieldName, Object object)
        throws NoSuchFieldException, IllegalAccessException {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);/*ww w.  j  a v a2s .co  m*/
    return field.get(object);
}

From source file:Util.java

public static boolean isFieldAnnotationPresent(Class<?> clazz, String fieldName,
        Class<? extends Annotation> annotationClass) throws NoSuchFieldException {
    Field field = clazz.getDeclaredField(fieldName);
    return (field.isAnnotationPresent(annotationClass));
}