Example usage for java.lang.reflect Field getByte

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

Introduction

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

Prototype

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

Source Link

Document

Gets the value of a static or instance byte field.

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:Main.java

/**
 * Finds a set of constant static byte field declarations in the class that have the given value
 * and whose name match the given pattern
 * @param cl class to search in//  www .  j a  v a2  s. c  o m
 * @param value value of constant static byte field declarations to match
 * @param regexPattern pattern to match against the name of the field     
 * @return a set of the names of fields, expressed as a string
 */
private static String findConstByteInClass(Class<?> cl, byte value, String regexPattern) {
    Field[] fields = cl.getDeclaredFields();
    Set<String> fieldSet = new HashSet<String>();
    for (Field f : fields) {
        try {
            if (f.getType() == Byte.TYPE && (f.getModifiers() & Modifier.STATIC) != 0
                    && f.getName().matches(regexPattern) && f.getByte(null) == value) {
                fieldSet.add(f.getName());
            }
        } catch (IllegalArgumentException e) {
            //  ignore
        } catch (IllegalAccessException e) {
            //  ignore
        }
    }
    return fieldSet.toString();
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {/* ww  w. j a  v  a2 s  .  c om*/
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

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

/**
 * Return the value of the given field in the given object.
 */// w w w.ja  v  a  2 s  .co m
public static byte getByte(Object target, Field field) {
    if (target == null || field == null)
        return (byte) 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getByte(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t, _loc.get("get-field", target, field));
    }
}

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

/**
 * Return the value of the given field in the given object.
 *//*www.  j a v a2s.  com*/
public static byte getByte(Object target, Field field) {
    if (target == null || field == null)
        return (byte) 0;
    makeAccessible(field, field.getModifiers());
    try {
        return field.getByte(target);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:org.evosuite.regression.ObjectFields.java

private static Object getFieldValue(Field field, Object p) {
    try {// ww w . ja  va2s .  c o m
        /*Class objClass = p.getClass();
        if(p instanceof java.lang.String){
           ((String) p).hashCode();
        }*/
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (fieldType.isPrimitive()) {
            if (fieldType.equals(Boolean.TYPE)) {
                return field.getBoolean(p);
            }
            if (fieldType.equals(Integer.TYPE)) {
                return field.getInt(p);
            }
            if (fieldType.equals(Byte.TYPE)) {
                return field.getByte(p);
            }
            if (fieldType.equals(Short.TYPE)) {
                return field.getShort(p);
            }
            if (fieldType.equals(Long.TYPE)) {
                return field.getLong(p);
            }
            if (fieldType.equals(Double.TYPE)) {
                return field.getDouble(p);
            }
            if (fieldType.equals(Float.TYPE)) {
                return field.getFloat(p);
            }
            if (fieldType.equals(Character.TYPE)) {
                return field.getChar(p);
            }
            throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!");
        }
        return field.get(p);
    } catch (IllegalAccessException exc) {
        throw new RuntimeException(exc);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (MAX_RECURSION != 0)
            MAX_RECURSION = 0;
        else
            throw new RuntimeErrorException(e);
        return getFieldValue(field, p);
    }
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static String get(Object obj, Field field) {

    try {/*  w  ww  . jav a 2 s .c  o m*/
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }

        if (field.getType() == int.class) {
            return String.valueOf(field.getInt(obj));

        } else if (field.getType() == long.class) {
            return String.valueOf(field.getLong(obj));

        } else if (field.getType() == double.class) {
            return String.valueOf(field.getDouble(obj));
        } else if (field.getType() == byte.class) {
            return String.valueOf(field.getByte(obj));
        } else if (field.getType() == boolean.class) {
            return String.valueOf(field.getBoolean(obj));
        } else if (field.getType() == String.class) {
            if (field.get(obj) == null) {
                return "";
            }
            return String.valueOf(field.get(obj));
        } else if (field.getType() == Date.class) {
            if (field.get(obj) == null) {
                return "";
            }
            return MiscDateUtils.getDateTime((Date) field.get(obj));
        }

    } catch (Exception e) {

    }
    return "";
}

From source file:net.larry1123.elec.util.test.config.AbstractConfigTest.java

public void byteTest(String fieldName, Field testField) {
    try {// www.  java  2 s.  c  o  m
        Assert.assertTrue((getPropertiesFile().getByte(fieldName) == testField.getByte(getConfigBase())));
    } catch (IllegalAccessException e) {
        assertFailFieldError(fieldName);
    }
}

From source file:com.nonninz.robomodel.RoboModel.java

void saveField(Field field, TypedContentValues cv) {
    final Class<?> type = field.getType();
    final boolean wasAccessible = field.isAccessible();
    field.setAccessible(true);/*from   ww  w . j a  v  a2s.com*/

    try {
        if (type == String.class) {
            cv.put(field.getName(), (String) field.get(this));
        } else if (type == Boolean.TYPE) {
            cv.put(field.getName(), field.getBoolean(this));
        } else if (type == Byte.TYPE) {
            cv.put(field.getName(), field.getByte(this));
        } else if (type == Double.TYPE) {
            cv.put(field.getName(), field.getDouble(this));
        } else if (type == Float.TYPE) {
            cv.put(field.getName(), field.getFloat(this));
        } else if (type == Integer.TYPE) {
            cv.put(field.getName(), field.getInt(this));
        } else if (type == Long.TYPE) {
            cv.put(field.getName(), field.getLong(this));
        } else if (type == Short.TYPE) {
            cv.put(field.getName(), field.getShort(this));
        } else if (type.isEnum()) {
            final Object value = field.get(this);
            if (value != null) {
                final Method method = type.getMethod("name");
                final String str = (String) method.invoke(value);
                cv.put(field.getName(), str);
            }
        } else {
            // Try to JSONify it (db column must be of type text)
            final String json = mMapper.writeValueAsString(field.get(this));
            cv.put(field.getName(), json);
        }
    } catch (final IllegalAccessException e) {
        final String msg = String.format("Field %s is not accessible", type, field.getName());
        throw new IllegalArgumentException(msg);
    } catch (final JsonProcessingException e) {
        Ln.w(e, "Error while dumping %s of type %s to Json", field.getName(), type);
        final String msg = String.format("Field %s is not accessible", type, field.getName());
        throw new IllegalArgumentException(msg);
    } catch (final NoSuchMethodException e) {
        // Should not happen
        throw new RuntimeException(e);
    } catch (final InvocationTargetException e) {
        // Should not happen
        throw new RuntimeException(e);
    } finally {
        field.setAccessible(wasAccessible);
    }
}

From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java

@Override
public final int hashCode() {

    int result = 0;

    Field[] fields = this.getClass().getDeclaredFields();

    for (Field field : fields) {
        // compare non-final, non-static and non-transient fields only
        if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())
                && !Modifier.isTransient(field.getModifiers())) {
            try {

                // skip arrays
                if (!field.getType().isArray() && field.get(this) != null) {
                    // for string take its length
                    if (field.getType().equals(String.class)) {
                        result ^= ((String) field.get(this)).length();
                    } else if (field.getType().equals(short.class) || field.getType().equals(Short.class)) {
                        result ^= field.getShort(this);
                    } else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
                        result ^= field.getInt(this);
                    } else if (field.getType().equals(float.class) || field.getType().equals(Float.class)) {
                        result ^= (int) field.getFloat(this);
                    } else if (field.getType().equals(double.class) || field.getType().equals(Double.class)) {
                        result ^= (int) field.getDouble(this);
                    } else if (field.getType().equals(long.class) || field.getType().equals(Long.class)) {
                        result ^= (int) field.getLong(this);
                    } else if (field.getType().equals(byte.class) || field.getType().equals(Byte.class)) {
                        result ^= field.getByte(this);
                    } else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
                        result ^= field.getBoolean(this) == Boolean.TRUE ? 1 : 0;
                    }/*  w ww.j  a  va2s .c o  m*/
                }
            } catch (Exception e) {
                log.error(e.toString());
                throw new RuntimeException("Exception caught while calculating HardwareAddress hashcode.", e);
            }
        }
    }
    return result;
}