Exemple #1
0
 public virtual void DoToString(ClassMapping classMapping, StringWriter writer)
 {
     writer.WriteLine("    public override string ToString()");
     writer.WriteLine("    {");
     writer.WriteLine("        System.Text.StringBuilder sb = new System.Text.StringBuilder();");
     for (IEnumerator fields = classMapping.AllFields.GetEnumerator(); fields.MoveNext();)
     {
         FieldProperty field = (FieldProperty)fields.Current;
         // If nothing is stated about id then include it in toString()
         if (field.Identifier && field.GetMeta("use-in-tostring") == null)
         {
             writer.WriteLine("        sb.AppendFormat(\"{0}={{0}} \", {0});", field.fieldcase);
         }
         else if (field.GetMetaAsBool("use-in-tostring"))
         {
             writer.WriteLine("        sb.AppendFormat(\"{0}={{0}} \", {0});", field.fieldcase);
         }
     }
     writer.WriteLine("        return sb.ToString();");
     writer.WriteLine("    }");
     writer.WriteLine();
 }
Exemple #2
0
        public virtual void DoEqualsAndHashCode(string savedToClass, ClassMapping classMapping, StringWriter writer)
        {
            if (classMapping.MustImplementEquals())
            {
                writer.WriteLine("    public override bool Equals(object obj) {");
                writer.WriteLine("        if(this == obj) return true;");
                writer.WriteLine("        if((obj == null) || (obj.GetType() != this.GetType())) return false;");
                writer.WriteLine("        " + savedToClass + " castObj = (" + savedToClass + ") obj;");
                writer.Write("        return (castObj != null) ");
                int usedFields = 0;
                SupportClass.ListCollectionSupport idFields = new SupportClass.ListCollectionSupport();
                for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();)
                {
                    FieldProperty field = (FieldProperty)fields.Current;
                    if (field.GetMetaAsBool("use-in-equals"))
                    {
                        writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")");
                        usedFields++;
                    }
                    if (field.Identifier)
                    {
                        idFields.Add(field);
                    }
                }
                if (usedFields == 0)
                {
                    log.Warn("No properties has been marked as being used in equals/hashcode for " + classMapping.Name +
                             ". Using object identifier which is RARELY safe to use! See http://hibernate.org/109.html");
                    for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();)
                    {
                        FieldProperty field = (FieldProperty)fields.Current;
                        writer.Write(" && (this." + field.fieldcase + " == castObj." + field.fieldcase + ")");
                    }
                }
                writer.WriteLine(";");
                writer.WriteLine("    }");
                writer.WriteLine();

                writer.WriteLine("    public override int GetHashCode() {");
                writer.WriteLine("        int hash = 69;");
                //writer.Write("        return");

                for (IEnumerator fields = classMapping.Fields.GetEnumerator(); fields.MoveNext();)
                {
                    FieldProperty field = (FieldProperty)fields.Current;
                    if (field.GetMetaAsBool("use-in-equals"))
                    {
                        //writer.Write("\n            " + field.FieldName + ".GetHashCode() ^");
                        writer.WriteLine("        hash = 31 * hash + " + field.fieldcase + ".GetHashCode();");
                    }
                }
                if (usedFields == 0)
                {
                    for (IEnumerator fields = idFields.GetEnumerator(); fields.MoveNext();)
                    {
                        FieldProperty field = (FieldProperty)fields.Current;
                        //writer.Write("\n            " + field.FieldName + ".GetHashCode() ^");
                        writer.WriteLine("        hash = 31 * hash + " + field.fieldcase + ".GetHashCode();");
                    }
                }

                //writer.WriteLine(" 0;");
                writer.WriteLine("        return hash;");
                writer.WriteLine("    }");
                writer.WriteLine();
            }
        }