The Backyard - DumpObjectBug Diff
- Added parts are displayed like this.
- Deleted parts are displayed
like this.
CodeZineに掲載したプログラムにバグがありました。
http://codezine.jp/a/article/aid/95.aspx
!!パッチ
diff -u DumpObject.java~ DumpObject.java
--- DumpObject.java~ Mon Jun 13 01:15:38 2005
+++ DumpObject.java Thu Nov 16 22:05:22 2006
@@ -29,6 +29,25 @@
public DumpObject(Object o) {
target = o;
}
+
+ class EmbededKey {
+ Class cls;
+ Object obj;
+ EmbededKey(Class c, Object o) {
+ obj = o;
+ cls = c;
+ }
+ public boolean equals(Object o) {
+ if (o instanceof EmbededKey) {
+ return ((EmbededKey)o).cls == cls && ((EmbededKey)o).obj == obj;
+ }
+ return false;
+ }
+ public int hashCode() {
+ return cls.hashCode() ^ obj.hashCode();
+ }
+ }
+
/**
* オブジェクトを文字列化する。
* @return オブジェクトの文字列表現
@@ -63,8 +82,9 @@
}
buffer.append(']');
} else {
- if (embeded.containsKey(o)) {
- buffer.append('(').append(embeded.get(o)).append(')');
+ EmbededKey ekey = new EmbededKey(c, o);
+ if (embeded.containsKey(ekey)) {
+ buffer.append('(').append(embeded.get(ekey)).append(')');
} else {
buffer.append(dumpObject(c, o, level));
}
@@ -82,7 +102,7 @@
} catch (NoSuchMethodException e) { // never
return o.toString();
}
- embeded.put(o, o.toString());
+ embeded.put(new EmbededKey(c, o), o.toString());
StringBuffer buffer = new StringBuffer();
buffer.append(o.toString());
buffer.append(dumpFields(c, o, level));
!!テストと結果
!!!継承クラス
static class DerivedSimpleClass extends SimpleClass {
private String strValue2 = "string2";
}
// 継承オブジェクト
public void testDerivedSimpleClass() throws Exception {
DerivedSimpleClass o = new DerivedSimpleClass();
DumpObject dob = new DumpObject(o);
assertEquals(o + LF + "strValue2=\"string2\"" + LF +
o + LF + "strValue=\"string\"" + LF +
"intValue=32" + LF + "b=[ 1, 2, 3, 4, 5, ]",
dob.toString());
}
!!!インナークラス
static class Outer {
class Inner {
private String inner = "inner";
}
Inner createInner() {
return new Inner();
}
private String outer = "outer";
}
public void testInnerClass() throws Exception {
Outer o = new Outer();
Outer.Inner i = o.createInner();
DumpObject dob = new DumpObject(i);
// とりあえず表示してみる
System.out.println(dob.toString());
}
出力
com.example.tool.DumpObjectTest$Outer$Inner@6bade9
inner="inner"
this$1=com.example.tool.DumpObjectTest$Outer@b5dac4
outer="outer"
this$0=testInnerClass(com.example.tool.DumpObjectTest)
http://codezine.jp/a/article/aid/95.aspx
!!パッチ
diff -u DumpObject.java~ DumpObject.java
--- DumpObject.java~ Mon Jun 13 01:15:38 2005
+++ DumpObject.java Thu Nov 16 22:05:22 2006
@@ -29,6 +29,25 @@
public DumpObject(Object o) {
target = o;
}
+
+ class EmbededKey {
+ Class cls;
+ Object obj;
+ EmbededKey(Class c, Object o) {
+ obj = o;
+ cls = c;
+ }
+ public boolean equals(Object o) {
+ if (o instanceof EmbededKey) {
+ return ((EmbededKey)o).cls == cls && ((EmbededKey)o).obj == obj;
+ }
+ return false;
+ }
+ public int hashCode() {
+ return cls.hashCode() ^ obj.hashCode();
+ }
+ }
+
/**
* オブジェクトを文字列化する。
* @return オブジェクトの文字列表現
@@ -63,8 +82,9 @@
}
buffer.append(']');
} else {
- if (embeded.containsKey(o)) {
- buffer.append('(').append(embeded.get(o)).append(')');
+ EmbededKey ekey = new EmbededKey(c, o);
+ if (embeded.containsKey(ekey)) {
+ buffer.append('(').append(embeded.get(ekey)).append(')');
} else {
buffer.append(dumpObject(c, o, level));
}
@@ -82,7 +102,7 @@
} catch (NoSuchMethodException e) { // never
return o.toString();
}
- embeded.put(o, o.toString());
+ embeded.put(new EmbededKey(c, o), o.toString());
StringBuffer buffer = new StringBuffer();
buffer.append(o.toString());
buffer.append(dumpFields(c, o, level));
!!テストと結果
!!!継承クラス
static class DerivedSimpleClass extends SimpleClass {
private String strValue2 = "string2";
}
// 継承オブジェクト
public void testDerivedSimpleClass() throws Exception {
DerivedSimpleClass o = new DerivedSimpleClass();
DumpObject dob = new DumpObject(o);
assertEquals(o + LF + "strValue2=\"string2\"" + LF +
o + LF + "strValue=\"string\"" + LF +
"intValue=32" + LF + "b=[ 1, 2, 3, 4, 5, ]",
dob.toString());
}
!!!インナークラス
static class Outer {
class Inner {
private String inner = "inner";
}
Inner createInner() {
return new Inner();
}
private String outer = "outer";
}
public void testInnerClass() throws Exception {
Outer o = new Outer();
Outer.Inner i = o.createInner();
DumpObject dob = new DumpObject(i);
// とりあえず表示してみる
System.out.println(dob.toString());
}
出力
com.example.tool.DumpObjectTest$Outer$Inner@6bade9
inner="inner"
this$1=com.example.tool.DumpObjectTest$Outer@b5dac4
outer="outer"