26

Where can I download the java native method source code? For example, I want to know the source code of System.arraycopy(), but I can't find.

Kara
  • 6,115
  • 16
  • 50
  • 57
Jack
  • 5,540
  • 13
  • 65
  • 113
  • 2
    Native methods and implemented differently by the Virtual Machine you are using. There is no one implementation of this method, and in fact different code may be executed on different architectures or VMs. – epsalon Sep 26 '12 at 03:12
  • @epsalon I would think that qualifies as an answer for this :) – eis Sep 26 '12 at 03:16
  • Does Open-JDK may have this source code?I'm very desired want to have a look of these native source code,I think these should be C/C++ source code,Thanks – Jack Sep 26 '12 at 03:16
  • For source code of different JVMs, have a look at [this answer](http://stackoverflow.com/a/2026360/365237) – eis Sep 26 '12 at 03:21

2 Answers2

36

You can download OpenJdk source code here.

In the folder jdk\src\share you can get source code.

jdk\src\share\native is the natice method souce write in c and c++.

  1. jdk\src\linux source for linux.
  2. jdk\src\windows source for windows.
  3. jdk\src\solaris souce for solaris.
  4. jd\src\share common source.

eg: System.arrayCopy();

int file hotspot\src\share\vm\oops\objArrayKlass.cpp line 168:

void objArrayKlass::copy_array(arrayOop s, int src_pos, arrayOop d,
                           int dst_pos, int length, TRAPS) {
assert(s->is_objArray(), "must be obj array");

if (!d->is_objArray()) {
  THROW(vmSymbols::java_lang_ArrayStoreException());
}

// Check is all offsets and lengths are non negative
if (src_pos < 0 || dst_pos < 0 || length < 0) {
  THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
}
// Check if the ranges are valid
if  ( (((unsigned int) length + (unsigned int) src_pos) > (unsigned int) s->length())
   || (((unsigned int) length + (unsigned int) dst_pos) > (unsigned int) d->length()) )   {
  THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
}

// Special case. Boundary cases must be checked first
// This allows the following call: copy_array(s, s.length(), d.length(), 0).
// This is correct, since the position is supposed to be an 'in between point', i.e., s.length(),
// points to the right of the last element.
if (length==0) {
  return;
}
if (UseCompressedOops) {
  narrowOop* const src = objArrayOop(s)->obj_at_addr<narrowOop>(src_pos);
  narrowOop* const dst = objArrayOop(d)->obj_at_addr<narrowOop>(dst_pos);
  do_copy<narrowOop>(s, src, d, dst, length, CHECK);
} else {
  oop* const src = objArrayOop(s)->obj_at_addr<oop>(src_pos);
  oop* const dst = objArrayOop(d)->obj_at_addr<oop>(dst_pos);
  do_copy<oop> (s, src, d, dst, length, CHECK);
  }
}
lichengwu
  • 4,277
  • 6
  • 29
  • 42
  • I'm not sure that source for example System.arraycopy() would be there. I'm looking at the package in `openjdk\jdk\src\share\native\java\lang\System.c`, and it just includes the header file `java_lang_System.h` without having the information, so you're sure it's there? – eis Sep 26 '12 at 03:47
  • upvoted for the update, thanks! – eis Sep 26 '12 at 04:40
  • From the indicated download's readme, run `hg clone http://hg.openjdk.java.net/jdk7/jdk7 openjdk7 && ./openjdk7/get_source.sh` to actually get the source – ceyko Apr 30 '13 at 14:31
3

Native methods and implemented differently by the Virtual Machine you are using. There is no one implementation of this method, and in fact different code may be executed on different architectures or VMs.

epsalon
  • 2,294
  • 12
  • 19