Java Code Examples for java.lang.reflect.Field.getByte()

The following are Jave code examples for showing how to use getByte() of the java.lang.reflect.Field class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: TestInstanceCloneUtils.java   View Source Code Vote up 6 votes
int getVal(Field f) {
    try {
        if (f.getType() == int.class) {
            return f.getInt(this);
        } else if (f.getType() == short.class) {
            return (int)f.getShort(this);
        } else if (f.getType() == byte.class) {
            return (int)f.getByte(this);
        } else if (f.getType() == long.class) {
            return (int)f.getLong(this);
        }
    } catch(IllegalAccessException iae) {
        throw new RuntimeException("Setting fields failed");
    }
    throw new RuntimeException("unexpected field type");
}
 
Example 2
Project: CustomWorldGen   File: TypeAdapters.java   View Source Code Vote up 5 votes
public static byte getByte(Object instance, Field f)
{
    try {
        return f.getByte(instance);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 3
Project: ETF-Java   File: ReflectionUtils.java   View Source Code Vote up 4 votes
public static Object getField(Object instance, Field field) throws IllegalAccessException {
	if (UNSAFE != null) {
		if (int.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getInt(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (long.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getLong(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (double.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getDouble(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (void.class.equals(field.getType())) {
			return null;
		} else if (float.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getFloat(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (byte.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getByte(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (char.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getChar(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (boolean.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getBoolean(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else if (short.class.equals(field.getType())) {
			return ((sun.misc.Unsafe) UNSAFE).getShort(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		} else {
			return ((sun.misc.Unsafe) UNSAFE).getObject(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field));
		}
	} else { //Fallback if unsafe isn't available
		field.setAccessible(true);
		if (int.class.equals(field.getType())) {
			return field.getInt(instance);
		} else if (long.class.equals(field.getType())) {
			return field.getLong(instance);
		} else if (double.class.equals(field.getType())) {
			return field.getDouble(instance);
		} else if (void.class.equals(field.getType())) {
			return null;
		} else if (float.class.equals(field.getType())) {
			return field.getFloat(instance);
		} else if (byte.class.equals(field.getType())) {
			return field.getByte(instance);
		} else if (char.class.equals(field.getType())) {
			return field.getChar(instance);
		} else if (boolean.class.equals(field.getType())) {
			return field.getBoolean(instance);
		} else if (short.class.equals(field.getType())) {
			return field.getShort(instance);
		} else {
			return field.get(instance);
		}
	}
}
 
Example 4
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get id length property by reflection.
 *
 * @param entry Password Entry instance
 * @return id length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getIdLength(PasswordPinEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "idLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}
 
Example 5
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get username length property by reflection.
 *
 * @param entry Password entry instance
 * @return username length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getUsernameLength(PasswordPinEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "userNameLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}
 
Example 6
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get password length property by reflection.
 *
 * @param entry Password entry instance
 * @return password length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getPasswordLength(PasswordPinEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "passwordLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}
 
Example 7
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get id length property by reflection.
 *
 * @param entry Password Entry instance
 * @return id length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getIdLength(PasswordEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "idLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}
 
Example 8
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get username length property by reflection.
 *
 * @param entry Password entry instance
 * @return username length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getUsernameLength(PasswordEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "userNameLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}
 
Example 9
Project: javacard-tutorial   File: PasswordEntryTest.java   View Source Code Vote up 3 votes
/**
 * Get password length property by reflection.
 *
 * @param entry Password entry instance
 * @return password length
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
private byte getPasswordLength(PasswordEntry entry) throws NoSuchFieldException, IllegalAccessException {
    Field f = TestUtils.getField(entry.getClass(), "passwordLength");
    if (f == null)
        throw new NoSuchFieldException();
    return f.getByte(entry);
}