636 Klass* klass = obj->klass();
637 JvmtiVMObjectAllocEventCollector oam;
638
639 #ifdef ASSERT
640 // Just checking that the cloneable flag is set correct
641 if (obj->is_array()) {
642 guarantee(klass->is_cloneable(), "all arrays are cloneable");
643 } else {
644 guarantee(obj->is_instance(), "should be instanceOop");
645 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
646 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
647 }
648 #endif
649
650 // Check if class of obj supports the Cloneable interface.
651 // All arrays are considered to be cloneable (See JLS 20.1.5)
652 if (!klass->is_cloneable()) {
653 ResourceMark rm(THREAD);
654 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
655 }
656
657 // Make shallow object copy
658 const int size = obj->size();
659 oop new_obj_oop = NULL;
660 if (obj->is_array()) {
661 const int length = ((arrayOop)obj())->length();
662 new_obj_oop = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
663 } else {
664 new_obj_oop = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
665 }
666
667 HeapAccess<>::clone(obj(), new_obj_oop, size);
668
669 Handle new_obj(THREAD, new_obj_oop);
670 // Caution: this involves a java upcall, so the clone should be
671 // "gc-robust" by this stage.
672 if (klass->has_finalizer()) {
673 assert(obj->is_instance(), "should be instanceOop");
674 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
675 new_obj = Handle(THREAD, new_obj_oop);
|
636 Klass* klass = obj->klass();
637 JvmtiVMObjectAllocEventCollector oam;
638
639 #ifdef ASSERT
640 // Just checking that the cloneable flag is set correct
641 if (obj->is_array()) {
642 guarantee(klass->is_cloneable(), "all arrays are cloneable");
643 } else {
644 guarantee(obj->is_instance(), "should be instanceOop");
645 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
646 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
647 }
648 #endif
649
650 // Check if class of obj supports the Cloneable interface.
651 // All arrays are considered to be cloneable (See JLS 20.1.5)
652 if (!klass->is_cloneable()) {
653 ResourceMark rm(THREAD);
654 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
655 }
656 assert(!obj->klass_is_value_type(), "Disallowed on value type");
657
658 // Make shallow object copy
659 const int size = obj->size();
660 oop new_obj_oop = NULL;
661 if (obj->is_array()) {
662 const int length = ((arrayOop)obj())->length();
663 new_obj_oop = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
664 } else {
665 new_obj_oop = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
666 }
667
668 HeapAccess<>::clone(obj(), new_obj_oop, size);
669
670 Handle new_obj(THREAD, new_obj_oop);
671 // Caution: this involves a java upcall, so the clone should be
672 // "gc-robust" by this stage.
673 if (klass->has_finalizer()) {
674 assert(obj->is_instance(), "should be instanceOop");
675 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
676 new_obj = Handle(THREAD, new_obj_oop);
|