/** * An introspection based equality predicate for GenericObjects. * * @other the other object to test against. */ public boolean equals(Object that) { if (!this.getClass().equals(that.getClass())) return false; Class myclass = this.getClass(); Field[] fields = myclass.getDeclaredFields(); Class hisclass = that.getClass(); 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; } if (fieldName.compareTo("inputText") == 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)) return true; else if (f.get(this) == null) return false; else if (g.get(that) == null) return false; else return f.get(this).equals(g.get(that)); } catch (IllegalAccessException ex1) { InternalErrorHandler.handleException(ex1); } } return false; }
/** * 设置预编译参数 * * @param ps 预编译 * @param index 序号 * @param t vo模型 * @param f 字段 * @throws IllegalArgumentException * @throws SQLException * @throws IllegalAccessException */ private void setParamter(PreparedStatement ps, int index, T t, Field f) throws IllegalArgumentException, SQLException, IllegalAccessException { if (!f.isAccessible()) { f.setAccessible(true); } if (isBoolean(f)) { ps.setBoolean(index, f.getBoolean(t)); } else if (isInt(f)) { ps.setInt(index, f.getInt(t)); } else if (isLong(f)) { ps.setLong(index, f.getLong(t)); } else if (isString(f)) { ps.setString(index, (String) f.get(t)); } else if (isDate(f)) { Object o = f.get(t); if (o == null) { ps.setDate(index, null); } else { ps.setTimestamp(index, new java.sql.Timestamp(((Date) o).getTime())); } } else if (isByte(f)) { ps.setByte(index, f.getByte(t)); } else if (isChar(f)) { ps.setInt(index, f.getChar(t)); } else if (isDouble(f)) { ps.setDouble(index, f.getDouble(t)); } else if (isFloat(f)) { ps.setFloat(index, f.getFloat(t)); } else { ps.setObject(index, f.get(t)); } }
public void buildThresholds(String sourceFieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { maxThreshold = 0; Class<?> c = objectStore.get(0).getClass(); // TODO: implement ability to access private fields Field field = c.getField(sourceFieldName); String type = field.getType().getName(); for (int i = 0; i != objectStore.size(); i++) { if (type.equals("long") || type.equals("java.lang.Long")) { maxThreshold += field.getLong(objectStore.get(i)); } else if (type.equals("double") || type.equals("java.lang.Double")) { maxThreshold += Math.round(field.getDouble(objectStore.get(i)) * 1000); } else if (type.equals("int") || type.equals("java.lang.Integer")) { maxThreshold += field.getInt(objectStore.get(i)); } else if (type.equals("short") || type.equals("java.lang.Short")) { maxThreshold += field.getShort(objectStore.get(i)); } else if (type.equals("byte") || type.equals("java.lang.Byte")) { maxThreshold += field.getByte(objectStore.get(i)); } else if (type.equals("float") || type.equals("java.lang.Float")) { maxThreshold += field.getFloat(objectStore.get(i)); } else { throw new AssertionError( "fieldName = " + c.getName() + "." + sourceFieldName + ", type = " + type); } thresholdStore.add(maxThreshold); } // TODO: is this a good workaround? // if (maxThreshold == 0) { // System.out.println(Common.printInfo() + ": maxThreshold = 0"); // maxThreshold++; // } }
private final void from(Object source, java.lang.reflect.Field member, Field<?> field) throws IllegalAccessException { Class<?> mType = member.getType(); if (mType.isPrimitive()) { if (mType == byte.class) { Utils.setValue(this, field, member.getByte(source)); } else if (mType == short.class) { Utils.setValue(this, field, member.getShort(source)); } else if (mType == int.class) { Utils.setValue(this, field, member.getInt(source)); } else if (mType == long.class) { Utils.setValue(this, field, member.getLong(source)); } else if (mType == float.class) { Utils.setValue(this, field, member.getFloat(source)); } else if (mType == double.class) { Utils.setValue(this, field, member.getDouble(source)); } else if (mType == boolean.class) { Utils.setValue(this, field, member.getBoolean(source)); } else if (mType == char.class) { Utils.setValue(this, field, member.getChar(source)); } } else { Utils.setValue(this, field, member.get(source)); } }
private boolean packPrimitive(final Field afield, OutputObjectState os) { try { /* * TODO deal with arrays of primitive types. * * Workaround - provide saveState and restoreState annotations. */ if (afield.getType().equals(Boolean.TYPE)) os.packBoolean(afield.getBoolean(_theObject)); else if (afield.getType().equals(Byte.TYPE)) os.packByte(afield.getByte(_theObject)); else if (afield.getType().equals(Short.TYPE)) os.packShort(afield.getShort(_theObject)); else if (afield.getType().equals(Integer.TYPE)) os.packInt(afield.getInt(_theObject)); else if (afield.getType().equals(Long.TYPE)) os.packLong(afield.getLong(_theObject)); else if (afield.getType().equals(Float.TYPE)) os.packFloat(afield.getFloat(_theObject)); else if (afield.getType().equals(Double.TYPE)) os.packDouble(afield.getDouble(_theObject)); else if (afield.getType().equals(Character.TYPE)) os.packChar(afield.getChar(_theObject)); else return false; } catch (final IOException ex) { return false; } catch (final Exception ex) { return false; } return true; }
public static void keep_writeFloat(Parcel paramParcel, Field paramField, Object paramObject) { try { paramParcel.writeFloat(paramField.getFloat(paramObject)); return; } catch (Exception paramParcel) { v.e("MicroMsg.MCacheItem", "exception:%s", new Object[] {be.f(paramParcel)}); } }
@Override public void load() throws Exception { clEntityObsidianTNT = Class.forName("mekanism.common.EntityObsidianTNT"); fEntityObsidianTNT_Fuse = clEntityObsidianTNT.getDeclaredField("fuse"); fMekanism_ObsidianTNTBlastRadius = Class.forName("mekanism.common.Mekanism").getDeclaredField("ObsidianTNTBlastRadius"); explosionRadius = fMekanism_ObsidianTNTBlastRadius.getFloat(null); }
public void read() { String type = field.getType().getName(); try { if (type.equals("double")) value = field.getDouble(objectReference); else if (type.equals("float")) value = field.getFloat(objectReference); else if (type.equals("int")) value = field.getInt(objectReference); else if (type.equals("byte")) value = field.getByte(objectReference); else throw new RuntimeException(); } catch (IllegalAccessException e) { throw new RuntimeException(e); } value = Math.round(value * getPrecisionMultiplier()) / getPrecisionMultiplier(); }
public static final float getFloatByName(final String pName) { for (final Field field : CreepConstants.class.getDeclaredFields()) { if (field.getName().equals(pName)) { try { return field.getFloat(field); } catch (IllegalArgumentException e) { Debug.e(e); } catch (IllegalAccessException e) { Debug.e(e); } } } return 0f; }
/** * 创建ContentValues对象通过Field对象 * * @param bean 数据结构 * @param field Field对象 * @param cv ContentValues对象 */ private void createContentValuesByFields(BaseDaoBean bean, Field field, ContentValues cv) { String key = field.getName(); Object keyValue = null; Class<?> clazz = field.getType(); try { if (BaseDBConst.KEY_ID.equals(field.getName())) { return; } keyValue = field.get(bean); if (clazz != null && keyValue != null) { if (clazz.isPrimitive()) { if (Double.TYPE == clazz) { cv.put(key, field.getDouble(bean)); } else if (Float.TYPE == clazz) { cv.put(key, field.getFloat(bean)); } else if (Integer.TYPE == clazz) { cv.put(key, field.getInt(bean)); } else if (Long.TYPE == clazz) { cv.put(key, field.getLong(bean)); } else if (Short.TYPE == clazz) { cv.put(key, field.getShort(bean)); } } else if (clazz == byte[].class) { cv.put(key, (byte[]) keyValue); } else if (Integer.class == clazz) { field.set(key, (Integer) keyValue); } else if (Long.class == clazz) { field.set(key, (Long) keyValue); } else if (Float.class == clazz) { field.set(key, (Float) keyValue); } else if (Double.class == clazz) { field.set(key, (Double) keyValue); } else if (Boolean.TYPE == clazz) { cv.put(key, (Boolean) keyValue); } else if (Byte.TYPE == clazz) { cv.put(key, (Byte) keyValue); } else if (String.class == clazz) { cv.put(key, (String) keyValue); } } } catch (IllegalArgumentException e) { Log.e(TAG, bean.getClass().getName() + "【" + key + "】的值是:" + keyValue); e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
public static float getSpeedFor(NPC npc) { EntityType entityType = npc.getBukkitEntity().getType(); Float cached = MOVEMENT_SPEEDS.get(entityType); if (cached != null) return cached; if (SPEED_FIELD == null) { MOVEMENT_SPEEDS.put(entityType, DEFAULT_SPEED); return DEFAULT_SPEED; } try { float speed = SPEED_FIELD.getFloat(((CraftEntity) npc.getBukkitEntity()).getHandle()); MOVEMENT_SPEEDS.put(entityType, speed); return speed; } catch (IllegalAccessException ex) { ex.printStackTrace(); return DEFAULT_SPEED; } }
// writes fields of a GlobalParcelable to a parcel // does not include the first parcelled item -- the class name protected static void dehydrate(GlobalParcelable model, Parcel out) throws IllegalArgumentException, IllegalAccessException { Log.i("GlobalParcelable", "dehydrating... " + model.getClass().toString()); // get the fields Field[] fields = model.getClass().getDeclaredFields(); // sort the fields so it is in deterministic order Arrays.sort(fields, compareMemberByName); // populate the fields for (Field field : fields) { field.setAccessible(true); if (field.getType().equals(int.class)) { out.writeInt(field.getInt(model)); } else if (field.getType().equals(double.class)) { out.writeDouble(field.getDouble(model)); } else if (field.getType().equals(float.class)) { out.writeFloat(field.getFloat(model)); } else if (field.getType().equals(long.class)) { out.writeLong(field.getLong(model)); } else if (field.getType().equals(String.class)) { out.writeString((String) field.get(model)); } else if (field.getType().equals(boolean.class)) { out.writeByte(field.getBoolean(model) ? (byte) 1 : (byte) 0); } else if (field.getType().equals(Date.class)) { Date date = (Date) field.get(model); if (date != null) { out.writeLong(date.getTime()); } else { out.writeLong(0); } } else if (GlobalParcelable.class.isAssignableFrom(field.getType())) { // why did this happen? Log.e( "GlobalParcelable", "GlobalParcelable F*ck up: " + " (" + field.getType().toString() + ")"); out.writeParcelable((GlobalParcelable) field.get(model), 0); } else { // wtf Log.e( "GlobalParcelable", "Could not write field to parcel: " + " (" + field.getType().toString() + ")"); } } }
/** * 向文件写入属性值 * * @param editor 指定的编辑器 * @param field 要写入的属性 * @param obj 取值对象 * @param pre key前缀 */ private void put(SharedPreferences.Editor editor, Field field, Object obj, String pre) { // 关闭属性访问权限检查 field.setAccessible(true); try { Log.v(LOG_TAG + "put", "field type is " + field.getType().getName()); Log.v(LOG_TAG + "put", "field name is " + field.getName()); Log.v(LOG_TAG + "put", "field value is " + field.get(obj)); // 是否需要加密 if (cipher != null && field.isAnnotationPresent(Encrypt.class)) { Log.v(LOG_TAG + "put", "field " + field.getName() + " use encrypt"); Object data = field.get(obj); if (data != null) { editor.putString(pre + "." + field.getName(), cipher.encrypt(String.valueOf(data))); } return; } // 根据属性类型选择操作 switch (field.getType().getName()) { case "java.lang.String": editor.putString(pre + "." + field.getName(), (String) field.get(obj)); break; case "int": editor.putInt(pre + "." + field.getName(), field.getInt(obj)); break; case "boolean": editor.putBoolean(pre + "." + field.getName(), field.getBoolean(obj)); break; case "float": editor.putFloat(pre + "." + field.getName(), field.getFloat(obj)); break; case "long": editor.putLong(pre + "." + field.getName(), field.getLong(obj)); break; } } catch (IllegalAccessException e) { Log.d(LOG_TAG + "put", "IllegalAccessException is " + e.getMessage()); } }
public float getScale() { try { Field mActualScaleField = null; Object targetObject = webView; if (Build.VERSION.SDK_INT < 14) { // before Ice cream sandwich mActualScaleField = WebView.class.getDeclaredField("mActualScale"); } else { Field zoomManagerField = WebView.class.getDeclaredField("mZoomManager"); zoomManagerField.setAccessible(true); targetObject = zoomManagerField.get(webView); mActualScaleField = Class.forName("android.webkit.ZoomManager").getDeclaredField("mActualScale"); } mActualScaleField.setAccessible(true); return mActualScaleField.getFloat(targetObject); } catch (Exception e) { throw new RuntimeException(e); } }
public float getValue() { float oldValue = value; try { if (useReflection) { try { value = field.getFloat(fieldObj); } catch (Exception e) { try { value = parseValueFromString(field.get(fieldObj)); } catch (Exception e2) { e2.printStackTrace(); System.err.println(e.getMessage()); } } } } catch (Exception e) { useReflection = false; e.printStackTrace(); } if (value != oldValue) updateString(); return value; }
/** * Compares the values of a field for two instances of a class. * * @param <T> type of the instances. Type must contain the specified field. * @param instance1 first instance to test * @param instance2 second instance to test * @param field field to test * @return true iff the primitive fields are equal * @throws IllegalAccessException * @throws IllegalArgumentException */ public static <T> boolean isPrimitiveFieldContentTheSame(T instance1, T instance2, Field field) throws IllegalArgumentException, IllegalAccessException { Class<?> type = field.getType(); if (type == byte.class) return (field.getByte(instance1) == field.getByte(instance2)); else if (type == short.class) return (field.getShort(instance1) == field.getShort(instance2)); else if (type == int.class) return (field.getInt(instance1) == field.getInt(instance2)); else if (type == long.class) return (field.getLong(instance1) == field.getLong(instance2)); else if (type == float.class) { float float1 = field.getFloat(instance1); float float2 = field.getFloat(instance2); return ((float1 == float2) || (Double.isNaN(float1) && Double.isNaN(float2))); } else if (type == double.class) { double double1 = field.getDouble(instance1); double double2 = field.getDouble(instance2); return ((double1 == double2) || (Double.isNaN(double1) && Double.isNaN(double2))); } else if (type == boolean.class) return (field.getBoolean(instance1) == field.getBoolean(instance2)); else if (type == char.class) return (field.getChar(instance1) == field.getChar(instance2)); else throw new RuntimeException("Field content is not primitive"); }
public static void printField(final Field field, boolean tabbed) { final String fieldName = String.format("%35s", field.getName()); try { String prefix = tabbed ? "" : " " + fieldName + " = "; String postfix = tabbed ? "\t" : "\n"; if (field.getType() == int.class) { TTY.print(prefix + field.getInt(null) + postfix); } else if (field.getType() == boolean.class) { TTY.print(prefix + field.getBoolean(null) + postfix); } else if (field.getType() == float.class) { TTY.print(prefix + field.getFloat(null) + postfix); } else if (field.getType() == String.class) { TTY.print(prefix + field.get(null) + postfix); } else if (field.getType() == Map.class) { Map<?, ?> m = (Map<?, ?>) field.get(null); TTY.print(prefix + printMap(m) + postfix); } else { TTY.print(prefix + field.get(null) + postfix); } } catch (IllegalAccessException e) { // do nothing. } }
private void putValueToEditor( SharedPreferences.Editor editor, String preferenceName, T model, Field field) { field.setAccessible(true); try { if (field.getType().equals(boolean.class)) { boolean booleanValue = field.getBoolean(model); editor.putBoolean(preferenceName, booleanValue); return; } if (field.getType().equals(int.class)) { int integerValue = field.getInt(model); editor.putInt(preferenceName, integerValue); return; } if (field.getType().equals(float.class)) { float floatValue = field.getFloat(model); editor.putFloat(preferenceName, floatValue); return; } if (field.getType().equals(long.class)) { long longValue = field.getLong(model); editor.putLong(preferenceName, longValue); return; } if (field.getType().equals(String.class)) { String stringValue = (String) field.get(model); editor.putString(preferenceName, stringValue); return; } Gson gson = new Gson(); editor.putString(preferenceName, gson.toJson(field.get(model))); } catch (IllegalAccessException e) { Log.wtf(TAG, "Exception during converting of Field's value to Preference", e); } }
@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); } }
/** * 从文件读取属性值 * * @param reader 读取器 * @param field 要读取的属性 * @param obj 目标对象 * @param pre key前缀 */ private void push(SharedPreferences reader, Field field, Object obj, String pre) { // 关闭属性访问权限检查 field.setAccessible(true); try { Log.v(LOG_TAG + "push", "field type is " + field.getType().getName()); Log.v(LOG_TAG + "push", "field name is " + field.getName()); // 是否需要解密 if (cipher != null && field.isAnnotationPresent(Encrypt.class)) { Log.v(LOG_TAG + "push", "field " + field.getName() + " need decrypt"); String cipherText = reader.getString(pre + "." + field.getName(), (String) field.get(obj)); if (cipherText != null) { String data = cipher.decrypt(cipherText); // 根据属性类型选择操作 switch (field.getType().getName()) { case "java.lang.String": field.set(obj, data); break; case "int": field.setInt(obj, Integer.parseInt(data)); break; case "boolean": field.setBoolean(obj, Boolean.parseBoolean(data)); break; case "float": field.setFloat(obj, Float.parseFloat(data)); break; case "long": field.setLong(obj, Long.parseLong(data)); break; } } return; } // 根据属性类型选择操作 switch (field.getType().getName()) { case "java.lang.String": field.set(obj, reader.getString(pre + "." + field.getName(), (String) field.get(obj))); break; case "int": field.setInt(obj, reader.getInt(pre + "." + field.getName(), field.getInt(obj))); break; case "boolean": field.setBoolean( obj, reader.getBoolean(pre + "." + field.getName(), field.getBoolean(obj))); break; case "float": field.setFloat(obj, reader.getFloat(pre + "." + field.getName(), field.getFloat(obj))); break; case "long": field.setLong(obj, reader.getLong(pre + "." + field.getName(), field.getLong(obj))); break; } Log.v(LOG_TAG + "push", "field value is " + field.get(obj)); } catch (IllegalAccessException e) { Log.d(LOG_TAG + "push", "IllegalAccessException is " + e.getMessage()); } }
/** 数据绑定 * */ private void bindValues(SQLiteStatement stmt, T entity) { for (int i = 0; i < mSetArgs.length; i++) { int stmtIndex = i + 1; try { Field field = mClazz.getDeclaredField(mSetArgs[i]); field.setAccessible(true); Type columnType = field.getType(); if (String.class.equals(columnType)) { String value = (String) field.get(entity); if (value == null) { stmt.bindNull(stmtIndex); } else { stmt.bindString(stmtIndex, value); } } else if (columnType.equals(Integer.class) || columnType.equals(int.class)) { int value = field.getInt(entity); stmt.bindLong(stmtIndex, value); } else if (columnType.equals(Float.class) || columnType.equals(float.class)) { float value = field.getFloat(entity); stmt.bindDouble(stmtIndex, value); } else if (columnType.equals(Double.class) || columnType.equals(double.class)) { double value = field.getDouble(entity); stmt.bindDouble(stmtIndex, value); } else if (columnType.equals(Long.class) || columnType.equals(long.class)) { long value = field.getLong(entity); stmt.bindLong(stmtIndex, value); } else { Object obj = field.get(entity); if (obj == null) { stmt.bindNull(stmtIndex); } else { byte[] value = SerializeHelper.serialize(obj); stmt.bindBlob(stmtIndex, value); } } } catch (NoSuchFieldException e) { e.printStackTrace(); stmt.bindNull(stmtIndex); continue; } catch (IllegalAccessException e) { e.printStackTrace(); stmt.bindNull(stmtIndex); continue; } catch (IllegalArgumentException e) { e.printStackTrace(); stmt.bindNull(stmtIndex); continue; } } if (mWhereArgs != null) { for (int i = 0; i < mWhereArgs.length; i++) { int columnIndex = mSetArgs.length + i + 1; stmt.bindString(columnIndex, mWhereArgValues[i]); } } }
float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException { return javaField.getFloat(obj); }
/** * An introspection based predicate matching using a template object. Allows for partial match of * two protocl Objects. * * @other the match pattern to test against. The match object has to be of the same type (class). * Primitive types and non-sip fields that are non null are matched for equality. Null in any * field matches anything. Some book-keeping fields are ignored when making the comparison. */ public boolean match(Object other) { if (other == null) return true; if (!this.getClass().equals(other.getClass())) return false; GenericObject that = (GenericObject) other; Class myclass = this.getClass(); Field[] fields = myclass.getDeclaredFields(); Class hisclass = other.getClass(); 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; } if (fieldName.compareTo("inputText") == 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 { Object myObj = f.get(this); Object hisObj = g.get(that); if (hisObj == myObj) return true; else if (hisObj != null && myObj == null) return false; else if (GenericObject.isMySubclass(myObj.getClass()) && !((GenericObject) myObj).match(hisObj)) return false; else if (hisObj instanceof java.lang.String && myObj instanceof java.lang.String) { if (((String) myObj).compareToIgnoreCase((String) hisObj) != 0) return false; } else if (GenericObjectList.isMySubclass(myObj.getClass()) && !((GenericObjectList) myObj).match(hisObj)) return false; } } catch (IllegalAccessException ex1) { InternalErrorHandler.handleException(ex1); } } return true; }
@SuppressWarnings("rawtypes") public void setData( Object obj, ByteBuffer byteBuffer, float[] floatValues, String[] stringValues, Indexes index) throws IllegalArgumentException, IllegalAccessException { Reporter r = null; if (BuildCraftCore.trackNetworkUsage) { if (!report.containsKey(clas.getName())) report.put(clas.getName(), new Reporter()); r = report.get(clas.getName()); r.clas = clas; } else r = new Reporter(); r.occurences++; for (Field f : shortFields) { byteBuffer.writeShort(f.getShort(obj)); r.bytes += 2; r.dataInt += 1; } for (Field f : intFields) { byteBuffer.writeInt(f.getInt(obj)); r.bytes += 4; r.dataInt += 1; } for (Field f : booleanFields) { byteBuffer.writeUnsignedByte(f.getBoolean(obj) ? 1 : 0); r.bytes += 1; r.dataInt += 1; } for (Field f : enumFields) { byteBuffer.writeUnsignedByte(((Enum) f.get(obj)).ordinal()); r.bytes += 1; r.dataInt += 1; } for (Field f : unsignedByteFields) { byteBuffer.writeUnsignedByte(f.getInt(obj)); r.bytes += 1; r.dataInt += 1; } for (Field f : floatFields) { floatValues[index.floatIndex] = f.getFloat(obj); index.floatIndex++; r.bytes += 4; r.dataFloat += 1; } for (Field f : doubleFields) { floatValues[index.floatIndex] = (float) f.getDouble(obj); index.floatIndex++; r.bytes += 4; r.dataFloat += 1; } for (Field f : stringFields) { stringValues[index.stringIndex] = (String) f.get(obj); r.bytes += stringValues[index.stringIndex].length(); index.stringIndex++; r.dataString += 1; } for (ClassMapping c : objectFields) { Object cpt = c.field.get(obj); if (cpt == null) { byteBuffer.writeUnsignedByte(0); for (int i = 0; i < c.sizeBytes; ++i) { byteBuffer.writeUnsignedByte(0); r.bytes += 1; r.dataInt += 1; } index.floatIndex += c.sizeFloat; index.stringIndex += c.sizeString; } else { byteBuffer.writeUnsignedByte(1); r.bytes += 1; r.dataInt += 1; c.setData(cpt, byteBuffer, floatValues, stringValues, index); } } for (Field f : doubleArrayFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { floatValues[index.floatIndex] = (float) ((double[]) f.get(obj))[i]; index.floatIndex++; r.bytes += 4; r.dataFloat += 1; } } for (Field f : shortArrayFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { byteBuffer.writeShort(((short[]) f.get(obj))[i]); r.bytes += 2; r.dataInt += 1; } } for (Field f : intArrayFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { byteBuffer.writeInt(((int[]) f.get(obj))[i]); r.bytes += 4; r.dataInt += 1; } } for (Field f : booleanArrayFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { byteBuffer.writeUnsignedByte(((boolean[]) f.get(obj))[i] ? 1 : 0); r.bytes += 1; r.dataInt += 1; } } for (Field f : unsignedByteFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { byteBuffer.writeUnsignedByte(((int[]) f.get(obj))[i]); r.bytes += 1; r.dataInt += 1; } } for (Field f : stringArrayFields) { TileNetworkData updateAnnotation = f.getAnnotation(TileNetworkData.class); for (int i = 0; i < updateAnnotation.staticSize(); ++i) { stringValues[index.stringIndex] = ((String[]) f.get(obj))[i]; r.bytes += stringValues[index.stringIndex].length(); index.stringIndex++; r.dataString += 1; } } for (ClassMapping c : objectArrayFields) { TileNetworkData updateAnnotation = c.field.getAnnotation(TileNetworkData.class); Object[] cpts = (Object[]) c.field.get(obj); for (int i = 0; i < updateAnnotation.staticSize(); ++i) if (cpts[i] == null) { byteBuffer.writeUnsignedByte(0); for (int j = 0; j < c.sizeBytes; ++j) { byteBuffer.writeUnsignedByte(0); r.bytes += 1; r.dataInt += 1; } index.floatIndex += c.sizeFloat; index.stringIndex += c.sizeString; } else { byteBuffer.writeUnsignedByte(1); r.bytes += 1; r.dataInt += 1; c.setData(cpts[i], byteBuffer, floatValues, stringValues, index); } } }
/** * Generic print formatting function: Does depth-first descent of the structure and recursively * prints all non-private objects pointed to by this object. <bf> Warning - the following generic * string routine will bomb (go into infinite loop) if there are any circularly linked structures * so if you have these, they had better be private! </bf> We dont have to worry about such things * for our structures (we never use circular linked structures). */ public String toString() { stringRepresentation = ""; Class myclass = getClass(); sprint(myclass.getName()); sprint("{"); sprint("inputText:"); sprint(inputText); Field[] fields = myclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[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) { // avoid nasty recursions... continue; } if (fieldName.compareTo("indentation") == 0) { // formatting stuff - not relevant here. continue; } sprint(fieldName + ":"); try { // Primitive fields are printed with type: value if (fieldType.isPrimitive()) { String fname = fieldType.toString(); sprint(fname + ":"); if (fname.compareTo("int") == 0) { int intfield = f.getInt(this); sprint(intfield); } else if (fname.compareTo("short") == 0) { short shortField = f.getShort(this); sprint(shortField); } else if (fname.compareTo("char") == 0) { char charField = f.getChar(this); sprint(charField); } else if (fname.compareTo("long") == 0) { long longField = f.getLong(this); sprint(longField); } else if (fname.compareTo("boolean") == 0) { boolean booleanField = f.getBoolean(this); sprint(booleanField); } else if (fname.compareTo("double") == 0) { double doubleField = f.getDouble(this); sprint(doubleField); } else if (fname.compareTo("float") == 0) { float floatField = f.getFloat(this); sprint(floatField); } } else if (Class.forName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType)) { if (f.get(this) != null) { sprint(((GenericObject) f.get(this)).toString(indentation + 1)); } else { sprint("<null>"); } } else if (Class.forName(SIP_PACKAGE + ".GenericObjectList").isAssignableFrom(fieldType)) { if (f.get(this) != null) { sprint(((GenericObjectList) f.get(this)).toString(indentation + 1)); } else { sprint("<null>"); } } else { // Dont do recursion on things that are not // of our header type... if (f.get(this) != null) { sprint(f.get(this).getClass().getName() + ":"); } else { sprint(fieldType.getName() + ":"); } sprint("{"); if (f.get(this) != null) { sprint(f.get(this).toString()); } else { sprint("<null>"); } sprint("}"); } } catch (IllegalAccessException ex1) { continue; // we are accessing a private field... } catch (Exception ex) { InternalErrorHandler.handleException(ex); } } sprint("}"); return stringRepresentation; }