java Java本机方法源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12594046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:28:12  来源:igfitidea点击:

Java native method source code

java

提问by Hyman

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.

哪里可以下载java本地方法源代码?例如,我想知道 的源代码System.arraycopy(),但我找不到。

回答by lichengwu

You can download OpenJdk source code here.

您可以在此处下载 OpenJdk 源代码。

In the folder jdk\src\shareyou can get source code.

在文件夹中,jdk\src\share您可以获得源代码。

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

jdk\src\share\native是用 c 和 c++ 编写的 natice 方法源。

  1. jdk\src\linuxsource for linux.
  2. jdk\src\windowssource for windows.
  3. jdk\src\solarissouce for solaris.
  4. jd\src\sharecommon source.
  1. jdk\src\linuxlinux的源码。
  2. jdk\src\windowsWindows 的源代码。
  3. jdk\src\solaris用于solaris 的源代码。
  4. jd\src\share共同来源。

eg: System.arrayCopy();

例如:System.arrayCopy();

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

int 文件hotspot\src\share\vm\oops\objArrayKlass.cpp第 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);
  }
}

回答by epsalon

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.

本机方法并由您使用的虚拟机以不同方式实现。这种方法没有一种实现,实际上不同的代码可能会在不同的架构或虚拟机上执行。