JNI - Object creation with incorrect constructor

Piotr Chmielewski piotr.ch.dev at gmail.com
Fri Mar 10 15:23:22 UTC 2017


When working on code with JNI Invocation API, I stumbled upon curious case.

Following code describes use of incorrect constructor in type when 
creating new object.


JNIEnv* env = ...;

jclass cls_Object = env->FindClass("java/lang/Object");
jmethodID constructor_Object = env->GetMethodID(cls_Object,"<init>","()V");
jclass cls_Date = env->FindClass("java/util/Date");
jmethodID method_Date_toString = 
env->GetMethodID(cls_Date,"toString","()Ljava/lang/String;");

jobject obj_Date = env->NewObject(cls_Date,constructor_Object);
jobject obj_toString = env->CallObjectMethod(obj_Date,method_Date_toString);

jstring str = static_cast<jstring>(obj_toString);
jsize length = env->GetStringLength(str);
char* dateString = new char[length];
jsize start = 0;
env->GetStringUTFRegion(jvmStr,start,length,data);
std::cout << "Date: " << dateString << std::endl;
delete[] dateString;


This code works and prints "Thu Jan 01 01:00:00 CET 1970".

Looks like "valid?" Date object is created using Object constructor - 
partially created object.

To understand the behavior, I checked OpenJDK sources for HotSpot. 
Implementation of "NewObject" allocates space for object and executes 
method on allocated space.

Additionally, based on sources of "java.util.Date", memory for object 
"obj_Date" is allocated and its values are set to default values. Date 
then uses field "long fastTime" in toString method. This results as 
object was created with "new Date(0)" instead "new Date()";

Main question: Is it a bug or by design, constructor's jmethodID is not 
checked to belong to type when creating object ?



More information about the jdk9-dev mailing list