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

The following are Jave code examples for showing how to use getShort() 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: BedrockProxy   File: MessageIdentifier.java   View Source Code Vote up 6 votes
/**
 * @param id the ID of the packet.
 * @return the packet's name based on it's ID.
 */
public static String getName(int id) {
    for (Field field : MessageIdentifier.class.getDeclaredFields()) {
        if (field.getType().equals(short.class)) {
            try {
                short packetId = field.getShort(null);
                if (packetId == id) {
                    return field.getName();
                }
            } catch (ReflectiveOperationException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
 
Example 2
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 3
Project: CustomWorldGen   File: TypeAdapters.java   View Source Code Vote up 5 votes
public static short getShort(Object instance, Field f)
{
    try {
        return f.getShort(instance);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 4
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 5
Project: LightSIP   File: SIPObject.java   View Source Code Vote up 4 votes
/**
 * An introspection based equality predicate for SIPObjects.
 *@param other the other object to test against.
 */
public boolean equals(Object other) {
    if (!this.getClass().equals(other.getClass()))
        return false;
    SIPObject that = (SIPObject) other;
    Class myclass = this.getClass();
    Class hisclass = other.getClass();
    while (true) {
        Field[] fields = myclass.getDeclaredFields();
        if (!hisclass.equals(myclass))
            return false;
        Field[] hisfields = hisclass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            Field g = hisfields[i];
            // Only print protected and public members.
            int modifier = f.getModifiers();
            if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                continue;
            Class fieldType = f.getType();
            String fieldName = f.getName();
            if (fieldName.compareTo("stringRepresentation") == 0) {
                continue;
            }
            if (fieldName.compareTo("indentation") == 0) {
                continue;
            }
            try {
                // Primitive fields are printed with type: value
                if (fieldType.isPrimitive()) {
                    String fname = fieldType.toString();
                    if (fname.compareTo("int") == 0) {
                        if (f.getInt(this) != g.getInt(that))
                            return false;
                    } else if (fname.compareTo("short") == 0) {
                        if (f.getShort(this) != g.getShort(that))
                            return false;
                    } else if (fname.compareTo("char") == 0) {
                        if (f.getChar(this) != g.getChar(that))
                            return false;
                    } else if (fname.compareTo("long") == 0) {
                        if (f.getLong(this) != g.getLong(that))
                            return false;
                    } else if (fname.compareTo("boolean") == 0) {
                        if (f.getBoolean(this) != g.getBoolean(that))
                            return false;
                    } else if (fname.compareTo("double") == 0) {
                        if (f.getDouble(this) != g.getDouble(that))
                            return false;
                    } else if (fname.compareTo("float") == 0) {
                        if (f.getFloat(this) != g.getFloat(that))
                            return false;
                    }
                } else if (g.get(that) == f.get(this))
                    continue;
                else if (f.get(this) == null && g.get(that) != null)
                    return false;
                else if (g.get(that) == null && f.get(this) != null)
                    return false;
                else if (!f.get(this).equals(g.get(that)))
                    return false;
            } catch (IllegalAccessException ex1) {
                System.out.println("accessed field " + fieldName);
                System.out.println("modifier  " + modifier);
                System.out.println("modifier.private  " + Modifier.PRIVATE);
                InternalErrorHandler.handleException(ex1);
            }
        }
        if (myclass.equals(SIPObject.class))
            break;
        else {
            myclass = myclass.getSuperclass();
            hisclass = hisclass.getSuperclass();
        }
    }
    return true;
}