Where to Find Source Code for Java.Lang Native Methods

Where to find source code for java.lang native methods?

For JDK6 you can download the source from java.net. For java.lang the story begins at j2se/src/share/native/java/lang/, and then search... JDK7 rearranges the directory structure a little.

Some methods, such as Object.hashCode, may be implemented by hotspot instead or in addition to through JNI/Java.

JDK6 is freely licensed through the Java Research License (JRL) and Java Internal Use License (JIUL). JDK7 and OpenJDK6 is licensed under GPL 2 with CLASSPATH exception (roughly speaking you can link to it without catching the GNU virus). I am not a lawyer.

(BTW: The real lawyers would like to point out that I am still an employee of Sun Microsystems. Sun UK is no more. It is now Oracle.)

Update (Adding location for Thread.c):
http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/native/java/lang/Thread.c

How to find the native method from the JVM source code?

JNI implementation of the above methods is in JDK repository, not in HotSpot, see src/share/native/java/lang/ClassLoader.c

However, these methods eventually call back to JVM through HotSpot-specific
JVM_DefineClassWithSource API. The implementation of this JVM function is in HotSpot repository, see src/share/vm/prims/jvm.cpp

Native methods' implementation in jdk source code?

Searching the OpenJDK Mercurial Repository online is indeed always a bit difficult. There are some mirrors of the JDK source code on GitHub, which has a far better search functionality - for example:

https://github.com/openjdk-mirror/jdk7u-jdk/search?utf8=%E2%9C%93&q=isReachable0

Quite often you can "guess" from parts of the paths of the result where you have to look in the original OpenJDK repo, here, this would be

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/82b276590b85/src/windows/native/java/net/Inet4AddressImpl.c#l416

(for Windows - other implementations can be found by browsing into the corresponding paths of the respective OS)

Java native method source code

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);
}
}

Where is the source code for native java.lang.math methods in JDK 7?

The Oracle-recommended reference implementation at OpenJDK has the native math methods in the following folder location:

jdk1.6\src\jdk\src\share\native\java\lang\fdlibm\src\*.c

Is it possible to find the source for a Java native method?

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

Where can you find the native code in the JDK?


http://java.sun.com/j2se/1.5.0/source_license.html
http://download.java.net/jdk6/source/
http://openjdk.java.net/
http://www.gnu.org/software/classpath/

Here is a link to the FileInputStream code... there is also code specific to Windows and presumably other platforms inthe approriate directories.

http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/0a5b87833562/src/share/native/java/io/FileInputStream.c

Is the source code of native methods available?

Yes, see os::javaTimeMillis in /src/os/solaris/vm/os_solaris.cpp to see the Solaris implementation for instance.

There are a few layers of wrappers to get there though, see JVM_CurrentTimeMillis in /src/share/vm/prims/jvm.cpp



Related Topics



Leave a reply



Submit