本文整理匯總了Java中java.lang.reflect.Field.setShort方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.setShort方法的具體用法?Java Field.setShort怎麽用?Java Field.setShort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.setShort方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setVal
import java.lang.reflect.Field; //導入方法依賴的package包/類
void setVal(Field f, int i) {
try {
if (f.getType() == int.class) {
f.setInt(this, i);
return;
} else if (f.getType() == short.class) {
f.setShort(this, (short)i);
return;
} else if (f.getType() == byte.class) {
f.setByte(this, (byte)i);
return;
} else if (f.getType() == long.class) {
f.setLong(this, i);
return;
}
} catch(IllegalAccessException iae) {
throw new RuntimeException("Getting fields failed");
}
throw new RuntimeException("unexpected field type");
}
示例2: reflectConsume
import java.lang.reflect.Field; //導入方法依賴的package包/類
public void reflectConsume(NativeKeyEvent e) {
try {
Field f = NativeInputEvent.class.getDeclaredField("reserved");
f.setAccessible(true);
f.setShort(e, (short) 0x01);
System.out.print("[ OK ]\n");
} catch (Exception ex) {
System.out.print("[ !! ]\n");
ex.printStackTrace();
}
}
示例3: createInterfaceAddress
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Returns an InterfaceAddress instance with its fields set the the values
* specificed.
*/
static InterfaceAddress createInterfaceAddress(
InetAddress address, InetAddress broadcast, short prefixlength) {
try {
Class<InterfaceAddress> IAClass = InterfaceAddress.class;
InterfaceAddress ia;
Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
ctr.setAccessible(true);
Field addressField = IAClass.getDeclaredField("address");
addressField.setAccessible(true);
Field broadcastField = IAClass.getDeclaredField("broadcast");
broadcastField.setAccessible(true);
Field maskLengthField = IAClass.getDeclaredField("maskLength");
maskLengthField.setAccessible(true);
ia = ctr.newInstance();
addressField.set(ia, address);
broadcastField.set(ia, broadcast);
maskLengthField.setShort(ia, prefixlength);
return ia;
} catch (NoSuchFieldException nsfe) {
nsfe.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
return null;
}
示例4: setField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static void setField(Object instance, Field field, Object value) throws IllegalAccessException {
if (value == null && field.getType().isPrimitive()) {
return;
}
if (UNSAFE != null) {
if (int.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putInt(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (int) value);
} else if (long.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putLong(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (long) value);
} else if (double.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putDouble(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (double) value);
} else if (void.class.equals(field.getType())) {
} else if (float.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putFloat(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (float) value);
} else if (byte.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putByte(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (byte) value);
} else if (char.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putChar(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (char) value);
} else if (boolean.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putBoolean(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (boolean) value);
} else if (short.class.equals(field.getType())) {
((sun.misc.Unsafe) UNSAFE).putShort(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), (short) value);
} else {
((sun.misc.Unsafe) UNSAFE).putObject(instance, ((sun.misc.Unsafe) UNSAFE).objectFieldOffset(field), value);
}
} else { //Fallback if unsafe isn't available
field.setAccessible(true);
if (int.class.equals(field.getType())) {
field.setInt(instance, (int) value);
} else if (long.class.equals(field.getType())) {
field.setLong(instance, (long) value);
} else if (double.class.equals(field.getType())) {
field.setDouble(instance, (double) value);
} else if (void.class.equals(field.getType())) {
} else if (float.class.equals(field.getType())) {
field.setFloat(instance, (float) value);
} else if (byte.class.equals(field.getType())) {
field.setByte(instance, (byte) value);
} else if (char.class.equals(field.getType())) {
field.setChar(instance, (char) value);
} else if (boolean.class.equals(field.getType())) {
field.setBoolean(instance, (boolean) value);
} else if (short.class.equals(field.getType())) {
field.setShort(instance, (short) value);
} else {
field.set(instance, value);
}
}
}
示例5: read
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public void read(Bundle bundle, Object to, StateField field) throws IllegalAccessException {
Field propertyField = field.getField();
propertyField.setAccessible(true);
propertyField.setShort(to,bundle.getShort(field.getBundleKey()));
}
示例6: deserializePrimitiveField
import java.lang.reflect.Field; //導入方法依賴的package包/類
public void deserializePrimitiveField(Field f, Property prop, Object instance) throws IllegalArgumentException, IllegalAccessException
{
switch(fromJavaPrimitiveType(f.getType()))
{
case INTEGER:
{
if (f.getType().equals(Byte.TYPE))
{
f.setByte(instance, (byte) prop.getInt());
}
if (f.getType().equals(Short.TYPE))
{
f.setShort(instance, (short) prop.getInt());
}
if (f.getType().equals(Integer.TYPE))
{
f.setInt(instance, prop.getInt());
}
break;
}
case DOUBLE:
{
if (f.getType().equals(Float.TYPE))
{
f.setFloat(instance, (float) prop.getDouble());
}
if (f.getType().equals(Double.TYPE))
{
f.setDouble(instance, prop.getDouble());
}
if (f.getType().equals(Long.TYPE))
{
f.setLong(instance, (long) prop.getDouble());
}
break;
}
case BOOLEAN:
{
f.setBoolean(instance, prop.getBoolean());
break;
}
default:
{
f.set(instance, prop.getString());
}
}
}
示例7: getCellValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
private void getCellValue(Cell cell, Object o, Field field) throws IllegalAccessException, ParseException {
LOG.debug("cell:{}, field:{}, type:{}", cell.getCellTypeEnum(), field.getName(), field.getType().getName());
switch (cell.getCellTypeEnum()) {
case BLANK:
break;
case BOOLEAN:
field.setBoolean(o, cell.getBooleanCellValue());
break;
case ERROR:
field.setByte(o, cell.getErrorCellValue());
break;
case FORMULA:
field.set(o, cell.getCellFormula());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
if (field.getType().getName().equals(Date.class.getName())) {
field.set(o, cell.getDateCellValue());
} else {
field.set(o, format.format(cell.getDateCellValue()));
}
} else {
if (field.getType().isAssignableFrom(Integer.class) || field.getType().getName().equals("int")) {
field.setInt(o, (int) cell.getNumericCellValue());
} else if (field.getType().isAssignableFrom(Short.class) || field.getType().getName().equals("short")) {
field.setShort(o, (short) cell.getNumericCellValue());
} else if (field.getType().isAssignableFrom(Float.class) || field.getType().getName().equals("float")) {
field.setFloat(o, (float) cell.getNumericCellValue());
} else if (field.getType().isAssignableFrom(Byte.class) || field.getType().getName().equals("byte")) {
field.setByte(o, (byte) cell.getNumericCellValue());
} else if (field.getType().isAssignableFrom(Double.class) || field.getType().getName().equals("double")) {
field.setDouble(o, cell.getNumericCellValue());
} else if (field.getType().isAssignableFrom(String.class)) {
String s = String.valueOf(cell.getNumericCellValue());
if (s.contains("E")) {
s = s.trim();
BigDecimal bigDecimal = new BigDecimal(s);
s = bigDecimal.toPlainString();
}
//防止整數判定為浮點數
if (s.endsWith(".0")) { s = s.substring(0, s.indexOf(".0")); }
field.set(o, s);
} else {
field.set(o, cell.getNumericCellValue());
}
}
break;
case STRING:
if (field.getType().getName().equals(Date.class.getName())) {
field.set(o, format.parse(cell.getRichStringCellValue().getString()));
} else {
field.set(o, cell.getRichStringCellValue().getString());
}
break;
default:
field.set(o, cell.getStringCellValue());
break;
}
}
示例8: decodeInternal
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public <T> void decodeInternal(BsonReader reader, T instance, Field field) throws IllegalAccessException {
field.setShort(instance, (short) reader.readInt32());
}