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

The following are Jave code examples for showing how to use setDouble() 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: Pluto-Android   File: DaoUtils.java   View Source Code Vote up 6 votes
private static final <T> void setField(Cursor cursor, int columnIndex,
		T instance, Field field) throws IllegalArgumentException,
		IllegalAccessException {
	Class<?> type = field.getType();
	field.setAccessible(true);
	
	int index = cursor.getColumnIndex(field.getName());
	
	if (type.equals(int.class) || type.equals(Integer.class)) {
		field.setInt(instance, cursor.getInt(index));
	} else if (type.equals(String.class)) {
		field.set(instance, cursor.getString(index));
	} else if (type.equals(long.class) || type.equals(Long.class)) {
		field.setLong(instance, cursor.getLong(index));
	} else if (type.equals(double.class) || type.equals(Double.class)) {
		field.setDouble(instance, cursor.getDouble(index));
	}else if (type.equals(Date.class)) {
		long datetime = cursor.getLong(index);
		if(datetime>0){
			field.set(instance, new Date(datetime));
		}
	}
}
 
Example 2
Project: BaseClient   File: Value.java   View Source Code Vote up 6 votes
public void setValue(double value) {
    this.value = value;
    Field[] arrfield = this.mod.getClass().getDeclaredFields();
    int n = arrfield.length;
    int n2 = 0;
    while (n2 < n) {
        Field field = arrfield[n2];
        field.setAccessible(true);
        if (field.isAnnotationPresent(Val.class) && field.getName().equalsIgnoreCase(this.name)) {
            try {
                field.setDouble(this.mod, value);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        ++n2;
    }
    ValueManager.save();
}
 
Example 3
Project: eds   File: AbstractEventPublisher.java   View Source Code Vote up 5 votes
protected void setProperties(Object configBean, String id){
  Class clazz = configBean.getClass();
  while(clazz != Object.class){
    Field[] fields = clazz.getDeclaredFields();
    for(Field f : fields){
      String fieldName = f.getName();
      String fieldValue = ClientConfig.getProperty(id + "." + fieldName);
      if(fieldValue != null){
        f.setAccessible(true);
        Class cls = f.getType();
        try {
          if(cls == int.class || cls == Integer.class){
            f.setInt(configBean, Integer.parseInt(fieldValue));
           }else if(cls == String.class){
            f.set(configBean, fieldValue);
          }else if(cls == long.class || cls == Long.class){
            f.setLong(configBean, Long.parseLong(fieldValue));
          }else if(cls == boolean.class || cls == Boolean.class){
            f.setBoolean(configBean, Boolean.parseBoolean(fieldValue));
          }else if(cls == double.class || cls == Double.class){
            f.setDouble(configBean, Double.parseDouble(fieldValue));
          }
          else{
            LOG.error("skip not supported config bean field type ! "+fieldName+", "+cls);
          }
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }

      }
    }

    clazz = clazz.getSuperclass();
  }
}
 
Example 4
Project: ZentrelaRPG   File: ReflectCommand.java   View Source Code Vote up 5 votes
@Override
public void executeConsole(CommandSender sender, String[] args) {
    try {
        if (args.length == 3) {
            PlayerDataRPG pd = plugin.getPD(plugin.getServer().getPlayer(args[0]));
            if (pd == null) {
                sender.sendMessage("Could not get data for player " + args[0]);
            } else {
                Field f = PlayerDataRPG.class.getDeclaredField(args[1]);
                f.setAccessible(true);
                Object o = f.get(pd);
                Class<?> c = f.getType();
                if (c == int.class || c == Integer.class) {
                    f.setInt(pd, Integer.parseInt(args[2]));
                } else if (c == long.class || c == Long.class) {
                    f.setLong(pd, Long.parseLong(args[2]));
                } else if (c == double.class || c == Double.class) {
                    f.setDouble(pd, Double.parseDouble(args[2]));
                } else if (c == String.class) {
                    f.set(pd, args[2]);
                } else if (c == boolean.class) {
                    f.setBoolean(pd, Boolean.parseBoolean(args[2]));
                }
                sender.sendMessage("Updated value for " + args[0] + " from " + o + " to " + f.get(pd));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Project: MagicBox   File: DoubleReader.java   View Source Code Vote up 5 votes
@Override
public void read(Bundle bundle, Object to, StateField field) throws IllegalAccessException {
    Field propertyField = field.getField();
    propertyField.setAccessible(true);
    propertyField.setDouble(to,bundle.getDouble(field.getBundleKey()));

}
 
Example 6
Project: ETF-Java   File: ReflectionUtils.java   View Source Code Vote up 4 votes
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);
		}
	}
}
 
Example 7
Project: crush   File: Instrument.java   View Source Code Vote up 4 votes
public void uniformGains(Field field) throws IllegalAccessException {
    for(Channel channel : this) field.setDouble(channel, 1.0);		
}
 
Example 8
Project: VoidApi   File: SerializableConfig.java   View Source Code Vote up 4 votes
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());
		}
	}
}
 
Example 9
Project: azeroth   File: ExcelReader.java   View Source Code Vote up 4 votes
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;
    }
}
 
Example 10
Project: polymorphia   File: MappedField.java   View Source Code Vote up 4 votes
@Override
public <T> void decodeInternal(BsonReader reader, T instance, Field field) throws IllegalAccessException {
    field.setDouble(instance, reader.readDouble());
}