Example usage for java.lang.reflect Field get

List of usage examples for java.lang.reflect Field get

Introduction

In this page you can find the example usage for java.lang.reflect Field get.

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

From source file:Main.java

public static void disableChangeTransition(LayoutTransition layoutTransition) {
    if (layoutTransition == null) {
        return;// w w  w  .  j  ava2  s .c o m
    }
    try {
        Method method = LayoutTransition.class.getMethod("disableTransitionType", new Class[] { int.class });
        Field field = LayoutTransition.class.getField("CHANGING");
        method.invoke(layoutTransition, field.get(null));
    } catch (Exception e) {
        sLayoutTransitions.remove(layoutTransition);
    }
}

From source file:Main.java

/**
 * Gets the given field's value of the specified object instance, or null if
 * the value cannot be obtained.//from  w ww.j  a v a  2 s. c om
 */
public static Object getValue(final Field field, final Object instance) {
    try {
        field.setAccessible(true);
        return field.get(instance);
    } catch (final IllegalAccessException e) {
        return null;
    }
}

From source file:Main.java

public static void copyBean(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);/*from   w  w  w.j  av  a2s.  c  om*/
        try {
            Object value = field.get(from);
            field.set(to, value);
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static Object get(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
    f.setAccessible(true);/*from w ww. java 2  s.  com*/
    return f.get(obj);
}

From source file:Main.java

/**
 * Get the API access token request uri// ww  w. ja v  a 2  s.co m
 *
 * @param cloudApi type of cloud account
 * @return Uri
 */
public static Uri getAccessTokenUri(String cloudApi) throws MalformedURLException {
    // use reflection for flexibility
    try {
        Class clazz = Class.forName(cloudApi);
        Field tokenUrl = clazz.getField("TOKEN_URL");
        return Uri.parse((String) tokenUrl.get(null));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    throw new MalformedURLException("No url or malformed url for request!");
}

From source file:Main.java

/**
 * Replace the value of a field containing a non null array, by a new array containing the elements of the original
 * array plus the elements of extraElements.
 *
 * @param instance the instance whose field is to be modified.
 * @param fieldName the field to modify.
 * @param extraElements elements to append at the end of the array.
 *///from  w  w  w  . j ava2 s .  c  om
static void expandFieldArray(Object instance, String fieldName, Object[] extraElements)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    Field jlrField = findField(instance, fieldName);
    Object[] original = (Object[]) jlrField.get(instance);
    Object[] combined = (Object[]) Array.newInstance(original.getClass().getComponentType(),
            original.length + extraElements.length);
    System.arraycopy(extraElements, 0, combined, 0, extraElements.length);
    System.arraycopy(original, 0, combined, extraElements.length, original.length);
    jlrField.set(instance, combined);
}

From source file:Main.java

/**
 * invoke the leftParameter and get the name property.
 * it will try the Array.length() and Map.get(),
 * then it will try get'Name' and is'Name',
 * last, it will to find the name by invoke field.
 *//*ww  w.  jav a 2 s. c o  m*/
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);//from w w w  . ja  v a 2s . c om
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }

}

From source file:Main.java

/**
 * Get the API redirect uri/*from w ww.j  av a2 s. c  o m*/
 *
 * @param cloudApi type of cloud account
 * @return Uri
 */
public static Uri getRedirectUri(String cloudApi) {
    // use reflection for flexibility
    try {
        Class clazz = Class.forName(cloudApi);
        Field redirectUrl = clazz.getField("REDIRECT_URL");
        return Uri.parse((String) redirectUrl.get(null));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static void copyBeanWithOutNull(Object from, Object to) {
    Class<?> beanClass = from.getClass();
    Field[] fields = beanClass.getFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        field.setAccessible(true);//from   www.  j a  v  a  2  s  .  c  o m
        try {
            Object value = field.get(from);
            if (value != null) {
                field.set(to, value);
            }
        } catch (Exception e) {
        }
    }
}