真の深いクローンを行うことができるはずのジェネリッククローン関数を作成しようとしていました。私はこのリンク、How to Deep clone in javascriptを見に来て、そこから機能を取った。GWTアプリケーションで使用されるJavascript generic clone()メソッド
私は直接Javascriptを使ってみると、そのコードはうまく動作します。私はコードを少し変更して、GWTのJSNIコードを入れようとしました。
クローン機能:
deepCopy = function(item)
{
if (!item) {
return item;
} // null, undefined values check
var types = [ Number, String, Boolean ], result;
// normalizing primitives if someone did new String('aaa'), or new Number('444');
types.forEach(function(type) {
if (item instanceof type) {
result = type(item);
}
});
if (typeof result == "undefined") {
alert(Object.prototype.toString.call(item));
alert(item);
alert(typeof item);
if (Object.prototype.toString.call(item) === "[object GWTJavaObject]") {
alert('1st');
result = [];
alert('2nd');
item.forEach(function(child, index, array) {//exception thrown here
alert('inside for each');
result[index] = deepCopy(child);
});
} else if (typeof item == "GWTJavaObject") {
alert('3rd');
if (item.nodeType && typeof item.cloneNode == "function") {
var result = item.cloneNode(true);
} else if (!item.prototype) {
result = {};
for (var i in item) {
result[i] = deepCopy(item[i]);
}
} else {
if (false && item.constructor) {
result = new item.constructor();
} else {
result = item;
}
}
} else {
alert('4th');
result = item;
}
}
return result;
}
そして、リストには、この関数に渡していますが、このようなものです:
List<Integer> list = new ArrayList<Integer>();
list.add(new Integer(100));
list.add(new Integer(200));
list.add(new Integer(300));
List<Integer> newList = (List<Integer>) new Attempt().clone(list);
Integer temp = new Integer(500);
list.add(temp);
if (newList.contains(temp))
Window.alert("fail");
else
Window.alert("success");
しかし、私はこれを実行すると、私は直後のクローン機能でnullポインタ例外を取得alert("2nd")
行。
助けてください。
P.S:ここでは、任意のオブジェクトを複製するために使用できるジェネリッククローンメソッドを取得しようとしています。
[GWTでのディープクローン]の可能複製(http://stackoverflow.com/questions/14258486で逃げることができ/ deep-clone-in-gwt) –