예제 #1
0
JNIEXPORT void JNICALL
Java_org_apache_harmony_test_stress_jni_strings_StringsTest12_init(JNIEnv* env,
                                                                   jclass c) {
  StringsTest_init(env,c);
}

JNIEXPORT jint JNICALL
Java_org_apache_harmony_test_stress_jni_strings_StringsTest12_nativeMethod(JNIEnv *env,
                                                                           jobject this,
                                                                           jint len,
                                                                           jint maxCnt) {
  int i,cnt;
  jchar** arrays;
  
  //allocate cnt arrays of type jchar
  cnt=StringsTest_alloc_2d_array(env,len,maxCnt,&arrays);
  if(cnt==-1) {
    printf("Native code: Cannot allocate temporary arrays\n");
    return 0;
  }

  //perform calculations on allocated arrays
  printf("Native code: Performing calculations on %d strings\n",cnt);
  for(i=0;i<cnt;i++) {
    const jchar* tmp;
    jstring string,res;
    jboolean isCopy;
    int critical=1;
    string=(*env)->NewString(env,arrays[i],len);
    if(string==NULL) {
      printf("Native code: Cannot allocate java.lang.String object\n");
예제 #2
0
JNIEXPORT jint JNICALL
Java_org_apache_harmony_test_stress_jni_strings_StringsTest4_nativeMethod(
    JNIEnv *env,
    jobject thisObject,
    jint len,
    jint maxCnt) {

  int i;
  int cnt;
  jchar* calc_res;
  jchar** arrays;
  
  StringsTest_alloc_arrays(env, len, &calc_res, NULL);

  if (calc_res == NULL) {
    printf("Native code: Cannot allocate temporary array\n");
    return 0;
  }

  // Allocate cnt arrays of type jchar
  cnt = StringsTest_alloc_2d_array(env, len, maxCnt, &arrays);

  if (cnt == -1) {
    printf("Native code: Cannot allocate temporary arrays\n");
    return 0;
  }

  // Perform calculations on allocated arrays
  printf("Native code: Performing calculations on %d strings\n", cnt);

  for (i = 0; i < cnt; i++) {
    const jchar* tmp;
    jstring string;                 // Current string to process
    jstring res;                    // Calculation result string
    int critical = 1;               // Flag to determine if GetStringCritical
                                    // works as expected

    jboolean isCopy;
    StringsTest_do_calc(arrays[i], calc_res, len);
    string=(*env)->NewString(env, arrays[i], len);

    if (string == NULL) {
      printf("Native code: Cannot allocate java.lang.String object\n");
      StringsTest_free_2d_array(env, arrays, cnt);
      StringsTest_free_arrays(env, calc_res, NULL);
      return 0;
    }
    res = (*env)->CallStaticObjectMethod(env, clazz, mid, string);
    if((*env)->ExceptionCheck(env)) {
      printf("Native code: Exception in Java method\n");
      StringsTest_free_2d_array(env,arrays,cnt);
      StringsTest_free_arrays(env,calc_res,NULL);
      return 0;
    }
    tmp = (*env)->GetStringCritical(env, res, &isCopy);

    if (tmp == NULL) {
      printf("Native code: GetStringCritical() failed\n");
      StringsTest_free_2d_array(env, arrays, cnt);
      StringsTest_free_arrays(env, calc_res, NULL);
      return 0;
    }

    if (isCopy == JNI_TRUE) {
        (*env)->ReleaseStringCritical(env, res, tmp);
        tmp = (*env)->GetStringChars(env, res, NULL);

        if (tmp == NULL) {
          printf("Native code: GetStringChars() failed\n");
          StringsTest_free_2d_array(env, arrays, cnt);
          StringsTest_free_arrays(env, calc_res, NULL);
          return 0;
        }
        critical = 0;
    }

    if (!StringsTest_do_compare(calc_res, tmp, len)) {
      if (critical) {
        (*env)->ReleaseStringCritical(env, res, tmp);
      } else {
        (*env)->ReleaseStringChars(env, res, tmp);
      }
      printf("Native code: String comparison failed\n");
      StringsTest_free_2d_array(env, arrays, cnt);
      StringsTest_free_arrays(env, calc_res, NULL);
      return 0;
    }

    if (critical) {
      (*env)->ReleaseStringCritical(env, res, tmp);
    } else {
      (*env)->ReleaseStringChars(env, res, tmp);
    }

    (*env)->DeleteLocalRef(env, res);
    (*env)->DeleteLocalRef(env, string);
  }
  printf("Native code: Done calculations on %d strings\n",cnt);

  StringsTest_free_2d_array(env, arrays, cnt);
  StringsTest_free_arrays(env, calc_res, NULL);

  return 1;
}