Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:05:13+00:00 2026-06-09T23:05:13+00:00

I have found the clone() method of Object in Java: protected native Object clone()

  • 0

I have found the clone() method of Object in Java:

 protected native Object clone() throws CloneNotSupportedException;

Is the source of this method available? Maybe in OpenJDK?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:05:14+00:00Added an answer on June 9, 2026 at 11:05 pm

    From jdk/src/share/native/java/lang/Object.c

    static JNINativeMethod methods[] = {
        {"hashCode",    "()I",                    (void *)&JVM_IHashCode},
        {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},
        {"notify",      "()V",                    (void *)&JVM_MonitorNotify},
        {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},
        {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},
    };
    

    Meaning its a function pointer(probably done so they could implement platform-specific native code)

    doing a grep for JVM_Clone produces, among other things:

    (from hotspot/src/share/vm/prims/jvm.cpp)

    JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
      JVMWrapper("JVM_Clone");
      Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
      const KlassHandle klass (THREAD, obj->klass());
      JvmtiVMObjectAllocEventCollector oam;
    
    #ifdef ASSERT
      // Just checking that the cloneable flag is set correct
      if (obj->is_javaArray()) {
        guarantee(klass->is_cloneable(), "all arrays are cloneable");
      } else {
        guarantee(obj->is_instance(), "should be instanceOop");
        bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
        guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
      }
    #endif
    
      // Check if class of obj supports the Cloneable interface.
      // All arrays are considered to be cloneable (See JLS 20.1.5)
      if (!klass->is_cloneable()) {
        ResourceMark rm(THREAD);
        THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
      }
    
      // Make shallow object copy
      const int size = obj->size();
      oop new_obj = NULL;
      if (obj->is_javaArray()) {
        const int length = ((arrayOop)obj())->length();
        new_obj = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
      } else {
        new_obj = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
      }
      // 4839641 (4840070): We must do an oop-atomic copy, because if another thread
      // is modifying a reference field in the clonee, a non-oop-atomic copy might
      // be suspended in the middle of copying the pointer and end up with parts
      // of two different pointers in the field.  Subsequent dereferences will crash.
      // 4846409: an oop-copy of objects with long or double fields or arrays of same
      // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead
      // of oops.  We know objects are aligned on a minimum of an jlong boundary.
      // The same is true of StubRoutines::object_copy and the various oop_copy
      // variants, and of the code generated by the inline_native_clone intrinsic.
      assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");
      Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj,
                                   (size_t)align_object_size(size) / HeapWordsPerLong);
      // Clear the header
      new_obj->init_mark();
    
      // Store check (mark entire object and let gc sort it out)
      BarrierSet* bs = Universe::heap()->barrier_set();
      assert(bs->has_write_region_opt(), "Barrier set does not have write_region");
      bs->write_region(MemRegion((HeapWord*)new_obj, size));
    
      // Caution: this involves a java upcall, so the clone should be
      // "gc-robust" by this stage.
      if (klass->has_finalizer()) {
        assert(obj->is_instance(), "should be instanceOop");
        new_obj = instanceKlass::register_finalizer(instanceOop(new_obj), CHECK_NULL);
      }
    
      return JNIHandles::make_local(env, oop(new_obj));
    JVM_END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found many great answers to this from a year ago (when it
I have found this link: http://www.jfree.org/forum/viewtopic.php?f=3&t=6314 Its back in 2007 where they agree that
I have found already some similar discussion on this, but would like to investigate
i have found an very good method for retrieving any result set from the
This method is just changing the id for any player_id that is found. Reek
I have scoured the net, and found many people asking this, yet none have
Maybe this question has been anwsered, maybe not. I want to update an object
I found this wonderful clone function for javascript ( http://my.opera.com/GreyWyvern/blog/show.dml/1725165 ), but it breaks
EDIT: This method actually works great and I asked it then found the solution
I have searched and found another with quite close question but the result was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.