Exemplo n.º 1
0
  static {
    log.debug("Populating descriptor map");
    descriptorMap = new HashMap<Short, String>();
    typeDescriptorMap = new HashMap<Class<? extends com.google.protobuf.GeneratedMessage>, Short>();
    Field[] fields = MessageDescriptor.class.getDeclaredFields();
    log.debug("Found" + fields.length + " fields");
    for (Field f : fields) {
      if (!Modifier.isStatic(f.getModifiers())) {
        log.debug("Found instance field: " + f.getName());
        continue;
      }
      if (!f.getType().equals(short.class)) {
        log.debug("Found non-short field: " + f.getName());
        continue;
      }

      try {
        descriptorMap.put(f.getShort(null), f.getName());
        log.debug("Mapping " + f.getShort(null) + " to " + f.getName());
      } catch (IllegalArgumentException ex) {
        log.error("Unable to construct MessageType map.", ex);
      } catch (IllegalAccessException ex) {
        log.error("Unable to construct MessageType map", ex);
      }
    }

    typeDescriptorMap.put(ProtocolBuffer.HandshakeData.class, HANDSHAKE_DATA);
    typeDescriptorMap.put(ProtocolBuffer.CipherCapabilities.class, CIPHER_CAPABILITIES);
    typeDescriptorMap.put(ProtocolBuffer.RegisterKeyReq.class, REGISTER_KEY_REQ);
    typeDescriptorMap.put(ProtocolBuffer.RegisterLocationReq.class, REGISTER_LOCATION_REQ);
  }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 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++;
   //		}
 }
Exemplo n.º 4
0
  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));
    }
  }
Exemplo n.º 5
0
  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_writeShort(Parcel paramParcel, Field paramField, Object paramObject) {
   try {
     paramParcel.writeInt(paramField.getShort(paramObject));
     return;
   } catch (Exception paramParcel) {
     v.e("MicroMsg.MCacheItem", "exception:%s", new Object[] {be.f(paramParcel)});
   }
 }
  /**
   * 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");
  }
Exemplo n.º 8
0
  private static Map readFonts(NodeList fontList, HSSFWorkbook workbook)
      throws ExcelTransformerException {

    if (LOG.isLoggable(Level.FINE)) {
      LOG.entering(
          SimpleExcelRenderer.class.getName(), "readFonts", String.valueOf(fontList.getLength()));
    }

    Map results = new HashMap();

    for (int i = 0; i < fontList.getLength(); i++) {
      Element fontNode = (Element) fontList.item(i);
      String name = fontNode.getAttribute("name");
      HSSFFont font = workbook.createFont();

      NodeList children = fontNode.getChildNodes();
      for (int j = 0; j < children.getLength(); j++) {
        Node child = children.item(j);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
          Element childE = (Element) child;
          String value = XmlUtils.getElementText(childE);

          if (childE.getNodeName().equals("family")) {
            font.setFontName(value);
          } else {
            try {
              Field field = HSSFFont.class.getField(value);

              if (childE.getNodeName().equals("boldweight")) {
                font.setBoldweight(field.getShort(null));
              }
            } catch (Throwable th) {
              throw new ExcelTransformerException(
                  "An error occurred while processing " + value + " on the excel font.");
            }
          }
        }
      }

      results.put(name, font);
    }

    if (LOG.isLoggable(Level.FINE)) {
      LOG.exiting(SimpleExcelRenderer.class.getName(), "readFonts");
    }
    return results;
  }
Exemplo n.º 9
0
 /**
  * 创建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();
   }
 }
Exemplo n.º 10
0
 @Override
 public Object get(Object owner) {
   try {
     // This is needed because until JDK 9 the Reflection API
     // does not use the same caching as used for auto-boxing.
     // See https://bugs.openjdk.java.net/browse/JDK-5043030 for details.
     // The code below can be removed when we move to JDK 9.
     // double and float are intentionally not handled here because
     // the JLS § 5.1.7 does not define caching for boxed values of
     // this types.
     Class<?> type = field.getType();
     if (type.isPrimitive()) {
       if (type == Boolean.TYPE) {
         return Boolean.valueOf(field.getBoolean(owner));
       } else if (type == Byte.TYPE) {
         return Byte.valueOf(field.getByte(owner));
       } else if (type == Character.TYPE) {
         return Character.valueOf(field.getChar(owner));
       } else if (type == Integer.TYPE) {
         return Integer.valueOf(field.getInt(owner));
       } else if (type == Long.TYPE) {
         return Long.valueOf(field.getLong(owner));
       } else if (type == Short.TYPE) {
         return Short.valueOf(field.getShort(owner));
       }
     }
     return field.get(owner);
   } catch (Exception e) {
     throw new PropertyAccessException(
         String.format(
             Locale.ROOT,
             "Error accessing field [%s] by reflection for persistent property [%s#%s] : %s",
             field.toGenericString(),
             containerClass.getName(),
             propertyName,
             owner),
         e);
   }
 }
Exemplo n.º 11
0
  @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);
        }
    }
  }
 short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException {
   return javaField.getShort(obj);
 }
Exemplo n.º 13
0
  @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);
    }
  }
Exemplo n.º 14
0
  /**
   * 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;
  }
Exemplo n.º 15
0
 /**
  * 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;
 }