private final void map(Object value, Object result, java.lang.reflect.Field member) throws IllegalAccessException { Class<?> mType = member.getType(); if (mType.isPrimitive()) { if (mType == byte.class) { member.setByte(result, (Byte) value); } else if (mType == short.class) { member.setShort(result, (Short) value); } else if (mType == int.class) { member.setInt(result, (Integer) value); } else if (mType == long.class) { member.setLong(result, (Long) value); } else if (mType == float.class) { member.setFloat(result, (Float) value); } else if (mType == double.class) { member.setDouble(result, (Double) value); } else if (mType == boolean.class) { member.setBoolean(result, (Boolean) value); } else if (mType == char.class) { member.setChar(result, (Character) value); } } else { member.set(result, value); } }
private boolean unpackPrimitive(final Field afield, InputObjectState os) { try { // TODO arrays if (afield.getType().equals(Boolean.TYPE)) afield.setBoolean(_theObject, os.unpackBoolean()); else if (afield.getType().equals(Byte.TYPE)) afield.setByte(_theObject, os.unpackByte()); else if (afield.getType().equals(Short.TYPE)) afield.setShort(_theObject, os.unpackShort()); else if (afield.getType().equals(Integer.TYPE)) afield.setInt(_theObject, os.unpackInt()); else if (afield.getType().equals(Long.TYPE)) afield.setLong(_theObject, os.unpackLong()); else if (afield.getType().equals(Float.TYPE)) afield.setFloat(_theObject, os.unpackFloat()); else if (afield.getType().equals(Double.TYPE)) afield.setDouble(_theObject, os.unpackDouble()); else if (afield.getType().equals(Character.TYPE)) afield.setChar(_theObject, os.unpackChar()); else return false; } catch (final IOException ex) { ex.printStackTrace(); return false; } catch (final Exception ex) { ex.printStackTrace(); return false; } return true; }
/** * 把查询结果填充到vo中 * * @param rs 查询结果,由调用负责游标 * @param t vo * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ private void fillVO(ResultSet rs, T t) throws IllegalArgumentException, IllegalAccessException, SQLException { for (Field f : listTableFields(t.getClass())) { String name = findFieldName(f); Object object = rs.getObject(name); if (object != null) { if (!f.isAccessible()) { f.setAccessible(true); } if (isBoolean(f)) { f.setBoolean(t, rs.getBoolean(name)); } else if (isInt(f)) { f.setInt(t, rs.getInt(name)); } else if (isLong(f)) { f.setLong(t, rs.getLong(name)); } else if (isString(f)) { f.set(t, rs.getString(name)); } else if (isDate(f)) { f.set(t, rs.getTimestamp(name)); } else if (isByte(f)) { f.setByte(t, rs.getByte(name)); } else if (isChar(f)) { f.setChar(t, rs.getString(name).charAt(0)); } else if (isDouble(f)) { f.setDouble(t, rs.getDouble(name)); } else if (isFloat(f)) { f.setFloat(t, rs.getFloat(name)); } else { f.set(t, object); } } } }
public void deserialize(JsonObject jo, Object t, Field field) throws IllegalAccessException { if (jo.has(field.getName())) { Character c = jo.getAsJsonPrimitive(field.getName()).getAsCharacter(); field.setChar(t, c); } }
public static void toggleField(Field field, Object obj, boolean on) throws IllegalAccessException, InstantiationException { field.setAccessible(true); if (field.getType() == String.class) { field.set(obj, on ? TEST_STRING_VAL1 : TEST_STRING_VAL2); } else if (field.getType() == boolean.class) { field.setBoolean(obj, on ? true : false); } else if (field.getType() == short.class) { field.setShort(obj, on ? (short) 1 : (short) 0); } else if (field.getType() == long.class) { field.setLong(obj, on ? 1 : 0); } else if (field.getType() == float.class) { field.setFloat(obj, on ? 1 : 0); } else if (field.getType() == int.class) { field.setInt(obj, on ? 1 : 0); } else if (field.getType() == Integer.class) { field.set(obj, on ? 1 : 0); } else if (field.getType() == byte.class) { field.setByte(obj, on ? (byte) 1 : (byte) 0); } else if (field.getType() == char.class) { field.setChar(obj, on ? (char) 1 : (char) 0); } else if (field.getType() == double.class) { field.setDouble(obj, on ? 1 : 0); } else if (field.getType() == BigDecimal.class) { field.set(obj, on ? new BigDecimal(1) : new BigDecimal(0)); } else if (field.getType() == Date.class) { field.set(obj, on ? new Date() : new Date(0)); } else if (field.getType().isEnum()) { field.set(obj, field.getType().getEnumConstants()[on ? 1 : 0]); } else if (Object.class.isAssignableFrom(field.getType())) { field.set(obj, field.getType().newInstance()); } else { fail("Don't know how to set a " + field.getType().getName()); } }
public TestResult mapRow(ResultSet rs, int rowNum) throws SQLException { TestResult result = new TestResult(); // 反射构建PatientInfo实体 Field[] fields = result.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String name = field.getName(); String type = field.getType().getName(); try { if (type.equals(String.class.getName())) { field.set(result, rs.getString(name)); } else if (type.equals(int.class.getName())) { field.setInt(result, rs.getInt(name)); } else if (type.equals(Date.class.getName())) { field.set(result, new java.util.Date(rs.getTimestamp(name).getTime())); } else if (type.equals(char.class.getName())) { String sampleType = rs.getString(name); if (sampleType != null && sampleType.length() > 0) field.setChar(result, sampleType.charAt(0)); } else { field.set(result, rs.getObject(name)); } /*} catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace();*/ } catch (Exception e) { // e.printStackTrace(); } field.setAccessible(false); } return result; }
private Object setField(ResultSet rs, Object info) { Field[] fields = info.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String name = field.getName(); String type = field.getType().getName(); try { if (type.equals(String.class.getName())) { field.set(info, rs.getString(name)); } else if (type.equals(int.class.getName())) { field.setInt(info, rs.getInt(name)); } else if (type.equals(Date.class.getName())) { field.set(info, new java.util.Date(rs.getTimestamp(name).getTime())); } else if (type.equals(long.class.getName())) { field.setLong(info, rs.getLong(name)); } else if (type.equals(char.class.getName())) { String sampleType = rs.getString(name); if (sampleType != null && sampleType.length() > 0) field.setChar(info, sampleType.charAt(0)); } else { field.set(info, rs.getObject(name)); } /*} catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace();*/ } catch (Exception e) { } field.setAccessible(false); } return info; }
public static void assertMeetsHashCodeContract(Class<?> classUnderTest, String[] fieldNames) { try { Field[] fields = getFieldsByNameOrAll(classUnderTest, fieldNames); for (int i = 0; i < fields.length; i++) { Object o1 = classUnderTest.newInstance(); int initialHashCode = o1.hashCode(); Field field = fields[i]; field.setAccessible(true); if (field.getType() == String.class) { field.set(o1, TEST_STRING_VAL1); } else if (field.getType() == boolean.class) { field.setBoolean(o1, true); } else if (field.getType() == short.class) { field.setShort(o1, (short) 1); } else if (field.getType() == long.class) { field.setLong(o1, (long) 1); } else if (field.getType() == float.class) { field.setFloat(o1, (float) 1); } else if (field.getType() == int.class) { field.setInt(o1, 1); } else if (field.getType() == byte.class) { field.setByte(o1, (byte) 1); } else if (field.getType() == char.class) { field.setChar(o1, (char) 1); } else if (field.getType() == double.class) { field.setDouble(o1, (double) 1); } else if (field.getType().isEnum()) { field.set(o1, field.getType().getEnumConstants()[0]); } else if (Object.class.isAssignableFrom(field.getType())) { field.set(o1, field.getType().newInstance()); } else { fail("Don't know how to set a " + field.getType().getName()); } int updatedHashCode = o1.hashCode(); assertFalse( "The field " + field.getName() + " was not taken into account for the hashCode contract ", initialHashCode == updatedHashCode); } } catch (InstantiationException e) { e.printStackTrace(); throw new AssertionError("Unable to construct an instance of the class under test"); } catch (IllegalAccessException e) { e.printStackTrace(); throw new AssertionError("Unable to construct an instance of the class under test"); } catch (NoSuchFieldException e) { e.printStackTrace(); throw new AssertionError("Unable to find field in the class under test"); } }
protected void readFields0( final Object object, final Class classs, final ObjectInputStream objectInputStream) throws IllegalAccessException { final Set<Field> serializableFields = ReflectionHelper.buildSerializableFields(object, classs); // serialize fields in alphabetical order final Iterator<Field> iterator = serializableFields.iterator(); while (iterator.hasNext()) { final Field field = iterator.next(); field.setAccessible(true); final Class fieldType = field.getType(); if (fieldType.equals(Boolean.TYPE)) { field.setBoolean(object, objectInputStream.readBoolean()); continue; } if (fieldType.equals(Byte.TYPE)) { field.setByte(object, objectInputStream.readByte()); continue; } if (fieldType.equals(Short.TYPE)) { field.setShort(object, objectInputStream.readShort()); continue; } if (fieldType.equals(Integer.TYPE)) { field.setInt(object, objectInputStream.readInt()); continue; } if (fieldType.equals(Long.TYPE)) { field.setLong(object, objectInputStream.readLong()); continue; } if (fieldType.equals(Float.TYPE)) { field.setFloat(object, objectInputStream.readFloat()); continue; } if (fieldType.equals(Double.TYPE)) { field.setDouble(object, objectInputStream.readDouble()); continue; } if (fieldType.equals(Character.TYPE)) { field.setChar(object, objectInputStream.readChar()); continue; } field.set(object, objectInputStream.readObject()); } final Class superType = classs.getSuperclass(); if (false == superType.equals(Object.class)) { this.readFields(object, superType, objectInputStream); } }
public static void setField(Field f, Object o, String t) throws NumberFormatException, IllegalArgumentException, IllegalAccessException { Class type = f.getType(); if (String.class.equals(type)) { f.set(o, t); } else if (Double.class.equals(type)) { f.set(o, Double.valueOf(t)); } else if (Float.class.equals(type)) { f.set(o, Float.valueOf(t)); } else if (Long.class.equals(type)) { f.set(o, Long.valueOf(t)); } else if (Integer.class.equals(type)) { f.set(o, Integer.valueOf(t)); } else if (Character.class.equals(type)) { f.set(o, Character.valueOf(t.charAt(0))); } else if (Short.class.equals(type)) { f.set(o, Short.valueOf(t)); } else if (Byte.class.equals(type)) { f.set(o, Byte.valueOf(t)); } else if (Boolean.class.equals(type)) { f.set(o, Boolean.valueOf(t)); } else if (Double.TYPE.equals(type)) { f.setDouble(o, Double.parseDouble(t)); } else if (Float.TYPE.equals(type)) { f.setFloat(o, Float.parseFloat(t)); } else if (Long.TYPE.equals(type)) { f.setLong(o, Long.parseLong(t)); } else if (Integer.TYPE.equals(type)) { f.setInt(o, Integer.parseInt(t)); } else if (Character.TYPE.equals(type)) { f.setChar(o, t.charAt(0)); } else if (Short.TYPE.equals(type)) { f.setShort(o, Short.parseShort(t)); } else if (Byte.TYPE.equals(type)) { f.setByte(o, Byte.parseByte(t)); } else if (Boolean.TYPE.equals(type)) { f.setBoolean(o, Boolean.parseBoolean(t)); } }
void setChar(Object obj, char c) throws IllegalArgumentException, IllegalAccessException { javaField.setChar(obj, c); }
@Override protected <T> void handleClonePrimitiveField( T obj, T copy, CloneDriver driver, FieldModel<T> f, IdentityHashMap<Object, Object> referencesToReuse) { Field field = f.getField(); try { Class<?> clazz = f.getFieldClass(); if (f.isPrivate()) { if (java.lang.Boolean.TYPE == clazz) { field.setBoolean(copy, field.getBoolean(obj)); } else if (java.lang.Byte.TYPE == clazz) { field.setByte(copy, field.getByte(obj)); } else if (java.lang.Character.TYPE == clazz) { field.setChar(copy, field.getChar(obj)); } else if (java.lang.Short.TYPE == clazz) { field.setShort(copy, field.getShort(obj)); } else if (java.lang.Integer.TYPE == clazz) { field.setInt(copy, field.getInt(obj)); } else if (java.lang.Long.TYPE == clazz) { field.setLong(copy, field.getLong(obj)); } else if (java.lang.Float.TYPE == clazz) { field.setFloat(copy, field.getFloat(obj)); } else if (java.lang.Double.TYPE == clazz) { field.setDouble(copy, field.getDouble(obj)); } else { throw new IllegalStateException("Expected primitive but was :" + clazz.getName()); } } else { if (java.lang.Boolean.TYPE == clazz) { f.getFieldAccess().putBooleanValue(copy, field.getBoolean(obj)); } else if (java.lang.Byte.TYPE == clazz) { f.getFieldAccess().putByteValue(copy, field.getByte(obj)); } else if (java.lang.Character.TYPE == clazz) { f.getFieldAccess().putCharValue(copy, field.getChar(obj)); } else if (java.lang.Short.TYPE == clazz) { f.getFieldAccess().putShortValue(copy, field.getShort(obj)); } else if (java.lang.Integer.TYPE == clazz) { f.getFieldAccess().putIntValue(copy, field.getInt(obj)); } else if (java.lang.Long.TYPE == clazz) { f.getFieldAccess().putLongValue(copy, field.getLong(obj)); } else if (java.lang.Float.TYPE == clazz) { f.getFieldAccess().putFloatValue(copy, field.getFloat(obj)); } else if (java.lang.Double.TYPE == clazz) { f.getFieldAccess().putDoubleValue(copy, field.getDouble(obj)); } else { throw new IllegalStateException("Expected primitive but was :" + clazz.getName()); } } } catch (IllegalArgumentException e) { throw new IllegalStateException( "Problem performing clone for field {" + field.getName() + "} of object {" + System.identityHashCode(obj) + "}: " + e.getMessage(), e); } catch (IllegalAccessException e) { throw new IllegalStateException( "Problem performing clone for field {" + field.getName() + "} of object {" + System.identityHashCode(obj) + "}: " + e.getMessage(), e); } }
/** * Tries to store a key=value pair into the class or object's field. * * @param seckey Section and key in one string. This have to be pre-parsed to confirm with the * naming convention of the config class. * @param value Value string to be stored. * @param into Class to store into. * @param object Object to store into if there is non-static fields. * @throws IllegalArgumentException If there is a parsing or storing problem. */ private static synchronized void storeKeyValue( String seckey, String value, Class<?> into, Object object) throws IllegalArgumentException { // long_key consists of section name/key name ... Field field = null; Class<?> type = null; String tmp; try { field = into.getDeclaredField(seckey); type = field.getType(); if (type.isEnum()) { tmp = value.replaceAll("-", "_"); // - is invalid enum stuff... replace with _. field.set(object, Enum.valueOf((Class<? extends Enum>) type, tmp)); } else if (type.isAssignableFrom(Integer.TYPE)) field.setInt(object, Integer.parseInt(value)); else if (type.isAssignableFrom(Long.TYPE)) field.setLong(object, Long.parseLong(value)); else if (type.isAssignableFrom(Float.TYPE)) field.setFloat(object, Float.parseFloat(value)); else if (type.isAssignableFrom(Double.TYPE)) field.setDouble(object, Double.parseDouble(value)); else if (type.isAssignableFrom(Byte.TYPE)) field.setByte(object, Byte.parseByte(value)); else if (type.isAssignableFrom(String.class)) field.set(object, value); else if (type.isAssignableFrom(Character.TYPE)) { tmp = value; if (tmp.length() == 2) { tmp = tmp.replaceAll("\\t", "\t"); tmp = tmp.replaceAll("\\n", "\n"); tmp = tmp.replaceAll("\\r", "\r"); tmp = tmp.replaceAll("\\\"", "\""); tmp = tmp.replaceAll("\\\'", "\'"); } if (tmp.length() != 1) throw new NumberFormatException(); field.setChar(object, tmp.charAt(0)); } else if (type.isAssignableFrom(Boolean.TYPE)) { tmp = value.toLowerCase(); tmp = tmp.replace("yes", "true"); tmp = tmp.replace("on", "true"); tmp = tmp.replace("1", "true"); field.setBoolean(object, Boolean.valueOf(tmp)); } else { // "unknown" type, lets try a default from String constructor. field.set(object, type.getConstructor(String.class).newInstance(value)); } } catch (SecurityException e) { throw new IllegalArgumentException( "unsupported option " + into.getName() + "." + seckey + ", security protected"); } catch (NoSuchFieldException e) { throw new IllegalArgumentException( "unsupported option " + into.getName() + "." + seckey + ", no such field"); /**/ } catch (NumberFormatException e) { throw new IllegalArgumentException( "illegal number format on " + into.getName() + "." + seckey + ", " + value + " is not " + (type.isAssignableFrom(Integer.TYPE) ? "an " : "a ") + type.getName()); /**/ } catch (IllegalArgumentException e) { // System.err.println("IllegalArgumentException "+e.getMessage()); if (e.getMessage().startsWith("No enum")) { throw new IllegalArgumentException( "unsupported enum value on field " + into.getName() + ", value \"" + value + "\" not valid"); } throw new IllegalArgumentException( "unsupported option field " + into.getName() + "." + seckey + ", illegal argument when setting value"); /**/ } catch (IllegalAccessException e) { throw new IllegalArgumentException( "unsupported option field " + into.getName() + "." + seckey + ", access denied"); /**/ } catch (NoSuchMethodException e) { throw new IllegalArgumentException( "unsupported field type " + field.getClass().getName() + ", no suitable constructor"); /**/ } catch (InstantiationException e) { throw new IllegalArgumentException( "unsupported field type " + field.getClass().getName() + ", instantiation error"); /**/ } catch (InvocationTargetException e) { throw new IllegalArgumentException( "unsupported field type " + field.getClass().getName() + ", unable to invocate constructor"); /**/ } catch (EnumConstantNotPresentException e) { System.err.println("EnumConstantNotPresentException " + e.getMessage()); throw new IllegalArgumentException( "unsupported enum value on field " + into.getName() + ", constant \"" + value + "\" not present"); } catch (Exception e) { throw new IllegalArgumentException( "unsupported field type " + field.getClass().getName() + ", unknown error in setting value"); /**/ } }
public static void assertMeetsEqualsContract(Class<?> classUnderTest, String[] fieldNames) { Object o1; Object o2; try { // Get Instances o1 = classUnderTest.newInstance(); o2 = classUnderTest.newInstance(); assertTrue("Instances with default constructor not equal (o1.equals(o2))", o1.equals(o2)); assertTrue("Instances with default constructor not equal (o2.equals(o1))", o2.equals(o1)); Field[] fields = getFieldsByNameOrAll(classUnderTest, fieldNames); for (int i = 0; i < fields.length; i++) { // Reset the instances o1 = classUnderTest.newInstance(); o2 = classUnderTest.newInstance(); Field field = fields[i]; field.setAccessible(true); if (field.getType() == String.class) { field.set(o1, TEST_STRING_VAL1); } else if (field.getType() == boolean.class) { field.setBoolean(o1, true); } else if (field.getType() == short.class) { field.setShort(o1, (short) 1); } else if (field.getType() == long.class) { field.setLong(o1, (long) 1); } else if (field.getType() == float.class) { field.setFloat(o1, (float) 1); } else if (field.getType() == int.class) { field.setInt(o1, 1); } else if (field.getType() == byte.class) { field.setByte(o1, (byte) 1); } else if (field.getType() == char.class) { field.setChar(o1, (char) 1); } else if (field.getType() == double.class) { field.setDouble(o1, (double) 1); } else if (field.getType().isEnum()) { field.set(o1, field.getType().getEnumConstants()[0]); } else if (Object.class.isAssignableFrom(field.getType())) { field.set(o1, field.getType().newInstance()); } else { fail("Don't know how to set a " + field.getType().getName()); } assertFalse( "Instances with o1 having " + field.getName() + " set and o2 having it not set are equal", o1.equals(o2)); field.set(o2, field.get(o1)); assertTrue( "After setting o2 with the value of the object in o1, the two objects in the field are not equal", field.get(o1).equals(field.get(o2))); assertTrue( "Instances with o1 having " + field.getName() + " set and o2 having it set to the same object of type " + field.get(o2).getClass().getName() + " are not equal", o1.equals(o2)); if (field.getType() == String.class) { field.set(o2, TEST_STRING_VAL2); } else if (field.getType() == boolean.class) { field.setBoolean(o2, false); } else if (field.getType() == short.class) { field.setShort(o2, (short) 0); } else if (field.getType() == long.class) { field.setLong(o2, (long) 0); } else if (field.getType() == float.class) { field.setFloat(o2, (float) 0); } else if (field.getType() == int.class) { field.setInt(o2, 0); } else if (field.getType() == byte.class) { field.setByte(o2, (byte) 0); } else if (field.getType() == char.class) { field.setChar(o2, (char) 0); } else if (field.getType() == double.class) { field.setDouble(o2, (double) 1); } else if (field.getType().isEnum()) { field.set(o2, field.getType().getEnumConstants()[1]); } else if (Object.class.isAssignableFrom(field.getType())) { field.set(o2, field.getType().newInstance()); } else { fail("Don't know how to set a " + field.getType().getName()); } if (field.get(o1).equals(field.get(o2))) { // Even though we have different instances, they are equal. // Let's walk one of them // to see if we can find a field to set Field[] paramFields = field.get(o1).getClass().getDeclaredFields(); for (int j = 0; j < paramFields.length; j++) { paramFields[j].setAccessible(true); if (paramFields[j].getType() == String.class) { paramFields[j].set(field.get(o1), TEST_STRING_VAL1); } } } assertFalse( "After setting o2 with a different object than what is in o1, the two objects in the field are equal. " + "This is after an attempt to walk the fields to make them different", field.get(o1).equals(field.get(o2))); assertFalse( "Instances with o1 having " + field.getName() + " set and o2 having it set to a different object are equal", o1.equals(o2)); } } catch (InstantiationException e) { e.printStackTrace(); throw new AssertionError("Unable to construct an instance of the class under test"); } catch (IllegalAccessException e) { e.printStackTrace(); throw new AssertionError("Unable to construct an instance of the class under test"); } catch (SecurityException e) { e.printStackTrace(); throw new AssertionError("Unable to read the field from the class under test"); } catch (NoSuchFieldException e) { e.printStackTrace(); throw new AssertionError("Unable to find field in the class under test"); } }