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 intentToAndroidLayoutMapper(Class<?> classObj, Intent intent, String prefixStr,
        Activity view) {/*w  ww. j ava2  s . com*/

    Bundle map = intent.getExtras();

    for (Object keyObj : map.keySet().toArray()) {
        String keyStr = keyObj.toString();
        Field fieldObj = null;
        try {
            fieldObj = classObj.getField(prefixStr + "_" + keyStr);
        } catch (NoSuchFieldException e) {
            continue;
        }
        Object layoutObj = null;
        try {
            layoutObj = fieldObj.get(fieldObj);
        } catch (IllegalAccessException e) {
            continue;
        }
        Integer layoutIntvalue = (Integer) layoutObj;

        View selectedView = view.findViewById(layoutIntvalue);

        if (selectedView instanceof TextView) {
            TextView textView = (TextView) selectedView;
            textView.setText((String) map.get(keyStr));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }

    }

}

From source file:com.npower.dm.util.BeanHelper.java

/**
 * private/protected//from  w  w  w .j a v a  2  s. com
 */
static public Object getDeclaredProperty(Object object, Field field) throws IllegalAccessException {
    assert object != null;
    assert field != null;
    boolean accessible = field.isAccessible();
    field.setAccessible(true);
    Object result = field.get(object);
    field.setAccessible(accessible);
    return result;
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {

    Class<?> c = null;// w w w .j  a v  a2 s  .c  o  m

    Object obj = null;

    Field field = null;

    int

    x = 0, statusBarHeight = 0;

    try

    {

        c = Class.forName("com.android.internal.R$dimen");

        obj = c.newInstance();

        field = c.getField("status_bar_height");

        x = Integer.parseInt(field.get(obj).toString());

        statusBarHeight = context.getResources().getDimensionPixelSize(x);

    } catch

    (Exception e1) {

        e1.printStackTrace();

    }

    return

    statusBarHeight;

}

From source file:com.vmware.photon.controller.common.dcp.validation.NotBlankValidator.java

public static void validate(ServiceDocument state) {
    try {//from  ww w . ja va 2  s. c  om
        Field[] declaredFields = state.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
            for (Annotation annotation : declaredAnnotations) {
                if (annotation.annotationType() == NotBlank.class) {
                    checkState(null != field.get(state), String.format("%s cannot be null", field.getName()));
                    if (String.class.equals(field.getType())) {
                        checkState((StringUtils.isNotBlank((String) field.get(state))),
                                String.format("%s cannot be blank", field.getName()));
                    }
                }
            }
        }
    } catch (IllegalStateException e) {
        throw e;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:Main.java

public static void jsonToAndroidLayoutMapper(Class<?> classObj, JSONObject json, String prefixStr,
        Activity view) throws JSONException {

    for (int i = 0; i < json.names().length(); i++) {
        String keyStr = (String) json.names().get(i);
        Field fieldObj = null;
        try {//w  ww  .ja va  2 s  . co m
            fieldObj = classObj.getField(prefixStr + "_" + keyStr.toLowerCase());
        } catch (NoSuchFieldException e) {
            continue;
        }
        Object layoutObj = null;
        try {
            layoutObj = fieldObj.get(fieldObj);
        } catch (IllegalAccessException e) {
            continue;
        }
        Integer layoutIntvalue = (Integer) layoutObj;

        View selectedView = view.findViewById(layoutIntvalue);

        if (selectedView instanceof TextView) {
            TextView textView = (TextView) selectedView;
            textView.setText((String) json.get(keyStr));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }
    }

}

From source file:Main.java

/**
 * This method will iterate over all the fields on an object searching for declared fields defined as
 * Collection, List, Set, Map type.  If those fields are null they will be set to empty unmodifiable
 * instances.  If those fields are not null then it will force them to be unmodifiable.
 *
 * This method does not handle nested types.
 *
 * @param o the object to modify.  a null object will do nothing.
 *//* www  .j a v  a 2  s  . co m*/
public static void makeUnmodifiableAndNullSafe(Object o) throws IllegalAccessException {
    if (o == null) {
        return;
    }

    Class<?> targetClass = o.getClass();
    for (Field f : targetClass.getDeclaredFields()) {
        f.setAccessible(true);
        try {
            if (f.getType().isAssignableFrom(List.class)) {
                f.set(o, unmodifiableListNullSafe((List<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Set.class)) {
                f.set(o, unmodifiableSetNullSafe((Set<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Collection.class)) {
                f.set(o, unmodifiableCollectionNullSafe((Collection<?>) f.get(o)));
            } else if (f.getType().isAssignableFrom(Map.class)) {
                f.set(o, unmodifiableMapNullSafe((Map<?, ?>) f.get(o)));
            }
        } finally {
            f.setAccessible(false);
        }
    }
}

From source file:Main.java

public final static void fixPopupWindow(final PopupWindow window) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        try {//from w w w .j av a2 s  .c o  m
            final Field mAnchorField = PopupWindow.class.getDeclaredField("mAnchor");
            mAnchorField.setAccessible(true);
            Field mOnScrollChangedListenerField = PopupWindow.class
                    .getDeclaredField("mOnScrollChangedListener");
            mOnScrollChangedListenerField.setAccessible(true);

            final OnScrollChangedListener mOnScrollChangedListener = (OnScrollChangedListener) mOnScrollChangedListenerField
                    .get(window);

            OnScrollChangedListener newListener = new OnScrollChangedListener() {
                public void onScrollChanged() {
                    try {
                        WeakReference<?> mAnchor = WeakReference.class.cast(mAnchorField.get(window));
                        Object anchor = mAnchor != null ? mAnchor.get() : null;

                        if (anchor == null) {
                            return;
                        } else {
                            mOnScrollChangedListener.onScrollChanged();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };

            mOnScrollChangedListenerField.set(window, newListener);
        } catch (Exception e) {
        }
    }
}

From source file:org.comicwiki.Repository.java

private static Object mergeObjects(Object source, Object target) {
    Field[] fields = source.getClass().getFields();
    for (Field field : fields) {
        field.setAccessible(true);//w  w w. j a va 2 s.c  o m
        try {
            Object sourceValue = field.get(source);
            if (sourceValue == null) {
                continue;
            }
            Object targetValue = field.get(target);
            if (targetValue == null) {
                field.set(target, sourceValue);
            } else if (targetValue instanceof Thing) {// CreativeWork
                mergeObjects(sourceValue, targetValue);
            } else if (targetValue instanceof IRI[]) {
                IRI[] tV = (IRI[]) targetValue;
                IRI[] sV = (IRI[]) sourceValue;
                field.set(target, Add.both(tV, sV, IRI.class));
            } else if (targetValue instanceof String[]) {
                String[] tV = (String[]) targetValue;
                String[] sV = (String[]) sourceValue;
                field.set(target, Add.both(sV, tV, String.class));
            } else if (targetValue instanceof Number[]) {
                Number[] tV = (Number[]) targetValue;
                Number[] sV = (Number[]) sourceValue;
                field.set(target, Add.both(tV, sV, Number.class));
            } else if (targetValue instanceof URL[]) {
                URL[] tV = (URL[]) targetValue;
                URL[] sV = (URL[]) sourceValue;
                field.set(target, Add.both(tV, sV, URL.class));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return target;
}

From source file:Main.java

public static int deepHashCode(Object obj) {
    Set visited = new HashSet();
    LinkedList<Object> stack = new LinkedList<Object>();
    stack.addFirst(obj);//from ww w  .  ja  v a2  s  .c  om
    int hash = 0;

    while (!stack.isEmpty()) {
        obj = stack.removeFirst();
        if (obj == null || visited.contains(obj)) {
            continue;
        }

        visited.add(obj);

        if (obj.getClass().isArray()) {
            int len = Array.getLength(obj);
            for (int i = 0; i < len; i++) {
                stack.addFirst(Array.get(obj, i));
            }
            continue;
        }

        if (obj instanceof Collection) {
            stack.addAll(0, (Collection) obj);
            continue;
        }

        if (obj instanceof Map) {
            stack.addAll(0, ((Map) obj).keySet());
            stack.addAll(0, ((Map) obj).values());
            continue;
        }

        if (hasCustomHashCode(obj.getClass())) {
            hash += obj.hashCode();
            continue;
        }

        Collection<Field> fields = getDeepDeclaredFields(obj.getClass());
        for (Field field : fields) {
            try {
                stack.addFirst(field.get(obj));
            } catch (Exception ignored) {
            }
        }
    }
    return hash;
}

From source file:com.spotify.helios.client.DefaultHttpConnector.java

private static void setRequestMethod(final HttpURLConnection connection, final String method,
        final boolean isHttps) {
    // Nasty workaround for ancient HttpURLConnection only supporting few methods
    final Class<?> httpURLConnectionClass = connection.getClass();
    try {/*from  w w  w. ja va  2s. c  o  m*/
        Field methodField;
        HttpURLConnection delegate;
        if (isHttps) {
            final Field delegateField = httpURLConnectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            delegate = (HttpURLConnection) delegateField.get(connection);
            methodField = delegate.getClass().getSuperclass().getSuperclass().getSuperclass()
                    .getDeclaredField("method");
        } else {
            delegate = connection;
            methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
        }

        methodField.setAccessible(true);
        methodField.set(delegate, method);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}