diff --git a/src/builtin.cpp b/src/builtin.cpp index 271288f6fd..44209e5f4c 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -101,10 +101,20 @@ Java_java_lang_Object_clone(Thread* t, jclass, jobject o) { ENTER(t, Thread::ActiveState); - object clone = make(t, objectClass(t, *o)); - memcpy(reinterpret_cast(clone) + 1, - reinterpret_cast(*o) + 1, - (baseSize(t, *o, objectClass(t, *o)) - 1) * BytesPerWord); + object class_ = objectClass(t, *o); + unsigned size = baseSize(t, *o, class_) * BytesPerWord; + object clone; + + if (classArrayElementSize(t, class_)) { + clone = static_cast(allocate(t, size)); + memcpy(clone, *o, size); + } else { + clone = make(t, objectClass(t, *o)); + memcpy(reinterpret_cast(clone) + 1, + reinterpret_cast(*o) + 1, + size - BytesPerWord); + } + return makeLocalReference(t, clone); }