added the auto-generation of hashcode, equals and toString methods

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@380217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-02-23 20:28:46 +00:00
parent 8ba8da2388
commit 2ce853a42d
2 changed files with 83 additions and 6 deletions

View File

@ -17,8 +17,10 @@
package org.apache.activemq.openwire.tool; package org.apache.activemq.openwire.tool;
import org.codehaus.jam.JClass; import org.codehaus.jam.JClass;
import org.codehaus.jam.JProperty;
import java.io.File; import java.io.*;
import java.util.Iterator;
/** /**
* *
@ -35,5 +37,52 @@ public abstract class OpenWireCSharpClassesScript extends OpenWireClassesScript
return super.run(); return super.run();
} }
public String makeHashCodeBody() throws Exception {
if (simpleName.endsWith("Id")) {
StringWriter buffer = new StringWriter();
PrintWriter out = new PrintWriter(buffer);
out.println(" int answer = 0;");
Iterator iter = getProperties().iterator();
while (iter.hasNext()) {
JProperty property = (JProperty) iter.next();
out.println(" answer = (answer * 37) + HashCode(" + property.getSimpleName() + ");");
}
out.println(" return answer;");
return buffer.toString();
}
return null;
}
public String makeEqualsBody() throws Exception {
if (simpleName.endsWith("Id")) {
StringWriter buffer = new StringWriter();
PrintWriter out = new PrintWriter(buffer);
Iterator iter = getProperties().iterator();
while (iter.hasNext()) {
JProperty property = (JProperty) iter.next();
String name = property.getSimpleName();
out.println(" if (! Equals(this." + name + ", that." + name + ")) return false;");
}
out.println(" return true;");
return buffer.toString();
}
return null;
}
public String makeToStringBody() throws Exception {
StringWriter buffer = new StringWriter();
PrintWriter out = new PrintWriter(buffer);
out.println(" return GetType().Name + \"[\"");
Iterator iter = getProperties().iterator();
while (iter.hasNext()) {
JProperty property = (JProperty) iter.next();
String name = property.getSimpleName();
out.println(" + \" " + name + "=\" + " + name);
}
out.println(" + \" ]\";");
return buffer.toString();
}
} }

View File

@ -69,13 +69,41 @@ namespace OpenWire.Client.Commands
""" """
} }
def text = makeHashCodeBody()
if (text != null) out <<
"""
public override int GetHashCode() {
$text
}
"""
text = makeEqualsBody()
if (text != null) out <<
"""
public override bool Equals(object that) {
if (that is ${className}) {
return Equals((${className}) that);
}
return false;
}
public virtual bool Equals(${className} that) {
$text
}
"""
text = makeToStringBody()
if (text != null) out << """
public override string ToString() {
$text
}
"""
out << """ out << """
// TODO generate Equals method
// TODO generate GetHashCode method
// TODO generate ToString method
public override byte GetDataStructureType() { public override byte GetDataStructureType() {
return ID_${jclass.simpleName}; return ID_${jclass.simpleName};
} }