Example usage for java.lang.reflect Field setByte

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void setByte(Object obj, byte b) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as a byte on the specified object.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getByte(x)); // Output: 10
    f.setByte(x, (byte) 20);
    System.out.println(f.getByte(x)); // Output: 20

}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Set the value of the given field in the given object.
 *///from   w  w  w .j ava2s  .  c o m
public static void set(Object target, Field field, byte value) {
    if (target == null || field == null)
        return;
    makeAccessible(field, field.getModifiers());
    try {
        field.setByte(target, value);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("set-field", new Object[] { target, field, value, "byte" }));
    }
}

From source file:org.sonar.api.batch.rule.Checks.java

private static void configureField(Object check, Field field, String value) {
    try {// w  ww .  j a  v  a 2  s.  c o  m
        field.setAccessible(true);

        if (field.getType().equals(String.class)) {
            field.set(check, value);

        } else if (int.class == field.getType()) {
            field.setInt(check, Integer.parseInt(value));

        } else if (short.class == field.getType()) {
            field.setShort(check, Short.parseShort(value));

        } else if (long.class == field.getType()) {
            field.setLong(check, Long.parseLong(value));

        } else if (double.class == field.getType()) {
            field.setDouble(check, Double.parseDouble(value));

        } else if (boolean.class == field.getType()) {
            field.setBoolean(check, Boolean.parseBoolean(value));

        } else if (byte.class == field.getType()) {
            field.setByte(check, Byte.parseByte(value));

        } else if (Integer.class == field.getType()) {
            field.set(check, Integer.parseInt(value));

        } else if (Long.class == field.getType()) {
            field.set(check, Long.parseLong(value));

        } else if (Double.class == field.getType()) {
            field.set(check, Double.parseDouble(value));

        } else if (Boolean.class == field.getType()) {
            field.set(check, Boolean.parseBoolean(value));

        } else {
            throw new SonarException(
                    "The type of the field " + field + " is not supported: " + field.getType());
        }
    } catch (IllegalAccessException e) {
        throw new SonarException(
                "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(),
                e);
    }
}

From source file:org.sonar.plugins.openedge.foundation.OpenEdgeComponents.java

private static void configureField(Object check, Field field, String value) {
    try {/* ww w  .  j  av a 2  s .co m*/
        field.setAccessible(true);

        if (field.getType().equals(String.class)) {
            field.set(check, value);
        } else if (int.class == field.getType()) {
            field.setInt(check, Integer.parseInt(value));
        } else if (short.class == field.getType()) {
            field.setShort(check, Short.parseShort(value));
        } else if (long.class == field.getType()) {
            field.setLong(check, Long.parseLong(value));
        } else if (double.class == field.getType()) {
            field.setDouble(check, Double.parseDouble(value));
        } else if (boolean.class == field.getType()) {
            field.setBoolean(check, Boolean.parseBoolean(value));
        } else if (byte.class == field.getType()) {
            field.setByte(check, Byte.parseByte(value));
        } else if (Integer.class == field.getType()) {
            field.set(check, new Integer(Integer.parseInt(value)));
        } else if (Long.class == field.getType()) {
            field.set(check, new Long(Long.parseLong(value)));
        } else if (Double.class == field.getType()) {
            field.set(check, new Double(Double.parseDouble(value)));
        } else if (Boolean.class == field.getType()) {
            field.set(check, Boolean.valueOf(Boolean.parseBoolean(value)));
        } else {
            throw MessageException
                    .of("The type of the field " + field + " is not supported: " + field.getType());
        }
    } catch (IllegalAccessException e) {
        throw MessageException.of(
                "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(),
                e);
    }
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Set the value of the given field in the given object.
 *//*from   w  ww.  j  a va  2 s.c  o  m*/
public static void set(Object target, Field field, byte value) {
    if (target == null || field == null)
        return;
    makeAccessible(field, field.getModifiers());
    try {
        field.setByte(target, value);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException {
    String key = f.getName();/*from   w w  w  .  ja v  a 2 s  . c om*/
    Class type = f.getType();
    try {
        if (type.isPrimitive()) {
            if (type == Integer.TYPE) {
                f.setInt(retObj, jsonObj.getInt(key));
            } else if (type == Long.TYPE) {
                f.setLong(retObj, jsonObj.getInt(key));
            } else if (type == Short.TYPE) {
                f.setShort(retObj, (short) jsonObj.getInt(key));
            } else if (type == Boolean.TYPE) {
                f.setBoolean(retObj, jsonObj.getBoolean(key));
            } else if (type == Double.TYPE) {
                f.setDouble(retObj, jsonObj.getDouble(key));
            } else if (type == Float.TYPE) {
                f.setFloat(retObj, (float) jsonObj.getDouble(key));
            } else if (type == Character.TYPE) {
                char ch = jsonObj.getString(key).charAt(0);
                f.setChar(retObj, ch);
            } else if (type == Byte.TYPE) {
                f.setByte(retObj, (byte) jsonObj.getInt(key));
            } else {
                throw new JSONSerializationException("Unknown primitive: " + type);
            }
        } else if (type == String.class) {
            f.set(retObj, jsonObj.getString(key));
        } else if (JSONSerializable.class.isAssignableFrom(type)) {
            JSONObject jObj = jsonObj.getJSONObject(key);
            JSONSerializable serObj = deSerialize(type, jObj);
            f.set(retObj, serObj);
        }
    } catch (Exception e) {
        throw new JSONSerializationException(e.getMessage(), e);
    }
}

From source file:android.reflect.ClazzLoader.java

/**
 * ????/*w w  w . ja  va  2s  .  c o m*/
 */
public static <O, V> void setFieldValue(O o, Field field, V v) {
    if (field != null) {
        try {
            field.setAccessible(true);

            if (v == null) {
                field.set(o, null);
            } else {
                Class<?> vType = v.getClass();
                if (vType == Integer.TYPE) {
                    field.setInt(o, (Integer) v);
                } else if (vType == Long.TYPE) {
                    field.setLong(o, (Long) v);
                } else if (vType == Boolean.TYPE) {
                    field.setBoolean(o, (Boolean) v);
                } else if (vType == Float.TYPE) {
                    field.setFloat(o, (Float) v);
                } else if (vType == Short.TYPE) {
                    field.setShort(o, (Short) v);
                } else if (vType == Byte.TYPE) {
                    field.setByte(o, (Byte) v);
                } else if (vType == Double.TYPE) {
                    field.setDouble(o, (Double) v);
                } else if (vType == Character.TYPE) {
                    field.setChar(o, (Character) v);
                } else {
                    field.set(o, v);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, t);
        }
    }
}

From source file:com.vk.sdk.api.model.ParseUtils.java

/**
* Parses object with follow rules://from   w  ww . j  a  v a2  s .co  m
*
* 1. All fields should had a public access.
* 2. The name of the filed should be fully equal to name of JSONObject key.
* 3. Supports parse of all Java primitives, all {@link String},
* arrays of primitive types, {@link String}s and {@link com.vk.sdk.api.model.VKApiModel}s,
* list implementation line {@link com.vk.sdk.api.model.VKList}, {@link com.vk.sdk.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdk.api.model.VKPhotoSizes},
* {@link com.vk.sdk.api.model.VKApiModel}s.
*
* 4. Boolean fields defines by vk_int == 1 expression.
* @param object object to initialize
* @param source data to read values
* @return initialized according with given data object
* @throws JSONException if source object structure is invalid
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException {
    if (source.has("response")) {
        source = source.optJSONObject("response");
    }
    if (source == null) {
        return object;
    }
    for (Field field : object.getClass().getFields()) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Class<?> fieldType = field.getType();

        Object value = source.opt(fieldName);
        if (value == null) {
            continue;
        }
        try {
            if (fieldType.isPrimitive() && value instanceof Number) {
                Number number = (Number) value;
                if (fieldType.equals(int.class)) {
                    field.setInt(object, number.intValue());
                } else if (fieldType.equals(long.class)) {
                    field.setLong(object, number.longValue());
                } else if (fieldType.equals(float.class)) {
                    field.setFloat(object, number.floatValue());
                } else if (fieldType.equals(double.class)) {
                    field.setDouble(object, number.doubleValue());
                } else if (fieldType.equals(boolean.class)) {
                    field.setBoolean(object, number.intValue() == 1);
                } else if (fieldType.equals(short.class)) {
                    field.setShort(object, number.shortValue());
                } else if (fieldType.equals(byte.class)) {
                    field.setByte(object, number.byteValue());
                }
            } else {
                Object result = field.get(object);
                if (value.getClass().equals(fieldType)) {
                    result = value;
                } else if (fieldType.isArray() && value instanceof JSONArray) {
                    result = parseArrayViaReflection((JSONArray) value, fieldType);
                } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKList.class.equals(fieldType)) {
                    ParameterizedType genericTypes = (ParameterizedType) field.getGenericType();
                    Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0];
                    if (VKApiModel.class.isAssignableFrom(genericType)
                            && Parcelable.class.isAssignableFrom(genericType)
                            && Identifiable.class.isAssignableFrom(genericType)) {
                        if (value instanceof JSONArray) {
                            result = new VKList((JSONArray) value, genericType);
                        } else if (value instanceof JSONObject) {
                            result = new VKList((JSONObject) value, genericType);
                        }
                    }
                } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) {
                    result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value);
                }
                field.set(object, result);
            }
        } catch (InstantiationException e) {
            throw new JSONException(e.getMessage());
        } catch (IllegalAccessException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodException e) {
            throw new JSONException(e.getMessage());
        } catch (InvocationTargetException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodError e) {
            // ?????????? ???????:
            // ?? ?? ????????, ?? ? ????????? ???????? getFields() ???????? ??? ???.
            // ?????? ? ??????? ???????????, ????????? ?? ? ????????, ?????? Android ? ???????? ????????? ??????????.
            throw new JSONException(e.getMessage());
        }
    }
    return object;
}

From source file:org.flycraft.vkontakteapi.model.ParseUtils.java

/**
 * Parses object with follow rules://  www  . jav  a  2  s  .co  m
 * <p/>
 * 1. All fields should had a public access.
 * 2. The name of the filed should be fully equal to name of JSONObject key.
 * 3. Supports parse of all Java primitives, all {@link java.lang.String},
 * arrays of primitive types, {@link java.lang.String}s and {@link org.flycraft.vkontakteapi.model.VKApiModel}s,
 * list implementation line {@link org.flycraft.vkontakteapi.model.VKList}, {@link org.flycraft.vkontakteapi.model.VKAttachments.VKApiAttachment} or {@link org.flycraft.vkontakteapi.model.VKPhotoSizes},
 * {@link org.flycraft.vkontakteapi.model.VKApiModel}s.
 * <p/>
 * 4. Boolean fields defines by vk_int == 1 expression.
 *
 * @param object object to initialize
 * @param source data to read values
 * @param <T>    type of result
 * @return initialized according with given data object
 * @throws JSONException if source object structure is invalid
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException {
    if (source.has("response")) {
        source = source.optJSONObject("response");
    }
    if (source == null) {
        return object;
    }
    for (Field field : object.getClass().getFields()) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Class<?> fieldType = field.getType();

        Object value = source.opt(fieldName);
        if (value == null) {
            continue;
        }
        try {
            if (fieldType.isPrimitive() && value instanceof Number) {
                Number number = (Number) value;
                if (fieldType.equals(int.class)) {
                    field.setInt(object, number.intValue());
                } else if (fieldType.equals(long.class)) {
                    field.setLong(object, number.longValue());
                } else if (fieldType.equals(float.class)) {
                    field.setFloat(object, number.floatValue());
                } else if (fieldType.equals(double.class)) {
                    field.setDouble(object, number.doubleValue());
                } else if (fieldType.equals(boolean.class)) {
                    field.setBoolean(object, number.intValue() == 1);
                } else if (fieldType.equals(short.class)) {
                    field.setShort(object, number.shortValue());
                } else if (fieldType.equals(byte.class)) {
                    field.setByte(object, number.byteValue());
                }
            } else {
                Object result = field.get(object);
                if (value.getClass().equals(fieldType)) {
                    result = value;
                } else if (fieldType.isArray() && value instanceof JSONArray) {
                    result = parseArrayViaReflection((JSONArray) value, fieldType);
                } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKList.class.equals(fieldType)) {
                    ParameterizedType genericTypes = (ParameterizedType) field.getGenericType();
                    Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0];
                    if (VKApiModel.class.isAssignableFrom(genericType)
                            && Identifiable.class.isAssignableFrom(genericType)) {
                        if (value instanceof JSONArray) {
                            result = new VKList((JSONArray) value, genericType);
                        } else if (value instanceof JSONObject) {
                            result = new VKList((JSONObject) value, genericType);
                        }
                    }
                } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) {
                    result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value);
                }
                field.set(object, result);
            }
        } catch (InstantiationException e) {
            throw new JSONException(e.getMessage());
        } catch (IllegalAccessException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodException e) {
            throw new JSONException(e.getMessage());
        } catch (InvocationTargetException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodError e) {
            //  ?:
            //   ,     getFields()   .
            //  ? ? ?,   ? ?,  Android  ?  .
            throw new JSONException(e.getMessage());
        }
    }
    return object;
}

From source file:com.vk.sdkweb.api.model.ParseUtils.java

/**
 * Parses object with follow rules:/*from  w  w w .j a va  2 s  . c o m*/
 *
 * 1. All fields should had a public access.
 * 2. The name of the filed should be fully equal to name of JSONObject key.
 * 3. Supports parse of all Java primitives, all {@link java.lang.String},
 *  arrays of primitive types, {@link java.lang.String}s and {@link com.vk.sdkweb.api.model.VKApiModel}s,
 *  list implementation line {@link com.vk.sdkweb.api.model.VKList}, {@link com.vk.sdkweb.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdkweb.api.model.VKPhotoSizes},
 *  {@link com.vk.sdkweb.api.model.VKApiModel}s.
 *
 * 4. Boolean fields defines by vk_int == 1 expression.
 *
 * @param object object to initialize
 * @param source data to read values
 * @param <T> type of result
 * @return initialized according with given data object
 * @throws JSONException if source object structure is invalid
 */
@SuppressWarnings("rawtypes")
public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException {
    if (source.has("response")) {
        source = source.optJSONObject("response");
    }
    if (source == null) {
        return object;
    }
    for (Field field : object.getClass().getFields()) {
        field.setAccessible(true);
        String fieldName = field.getName();
        Class<?> fieldType = field.getType();

        Object value = source.opt(fieldName);
        if (value == null) {
            continue;
        }
        try {
            if (fieldType.isPrimitive() && value instanceof Number) {
                Number number = (Number) value;
                if (fieldType.equals(int.class)) {
                    field.setInt(object, number.intValue());
                } else if (fieldType.equals(long.class)) {
                    field.setLong(object, number.longValue());
                } else if (fieldType.equals(float.class)) {
                    field.setFloat(object, number.floatValue());
                } else if (fieldType.equals(double.class)) {
                    field.setDouble(object, number.doubleValue());
                } else if (fieldType.equals(boolean.class)) {
                    field.setBoolean(object, number.intValue() == 1);
                } else if (fieldType.equals(short.class)) {
                    field.setShort(object, number.shortValue());
                } else if (fieldType.equals(byte.class)) {
                    field.setByte(object, number.byteValue());
                }
            } else {
                Object result = field.get(object);
                if (value.getClass().equals(fieldType)) {
                    result = value;
                } else if (fieldType.isArray() && value instanceof JSONArray) {
                    result = parseArrayViaReflection((JSONArray) value, fieldType);
                } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) {
                    Constructor<?> constructor = fieldType.getConstructor(JSONArray.class);
                    result = constructor.newInstance((JSONArray) value);
                } else if (VKList.class.equals(fieldType)) {
                    ParameterizedType genericTypes = (ParameterizedType) field.getGenericType();
                    Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0];
                    if (VKApiModel.class.isAssignableFrom(genericType)
                            && Parcelable.class.isAssignableFrom(genericType)
                            && Identifiable.class.isAssignableFrom(genericType)) {
                        if (value instanceof JSONArray) {
                            result = new VKList((JSONArray) value, genericType);
                        } else if (value instanceof JSONObject) {
                            result = new VKList((JSONObject) value, genericType);
                        }
                    }
                } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) {
                    result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value);
                }
                field.set(object, result);
            }
        } catch (InstantiationException e) {
            throw new JSONException(e.getMessage());
        } catch (IllegalAccessException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodException e) {
            throw new JSONException(e.getMessage());
        } catch (InvocationTargetException e) {
            throw new JSONException(e.getMessage());
        } catch (NoSuchMethodError e) {
            //  ?:
            //   ,     getFields()   .
            //  ? ? ?,   ? ?,  Android  ?  .
            throw new JSONException(e.getMessage());
        }
    }
    return object;
}