Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4027808

SetObjectField modifies a final field value

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.5

      Description



      Name: laC46010 Date: 01/24/97



      Java doesn't allow to change value of final fields.
      JNI functions of the kind Set<Type>Field behave different ways.

      All other functions except SetObjectField don't change
      a value of a final field. (Note that it would be good if they could
      diagnose this violation somehow.)

      SetObjectField function changes a value of a final field (See test below.)

      To run the test, set environment variables JH (Java home) and CC (C compiler)
      and run "jnirun" script.

      > jnirun
      Final int field has not been changed
      Final Object field has been changed


      ::::::::::::::
      jnirun
      ::::::::::::::
      #!/usr/bin/csh

      #setenv JH /export/ld32/jdk_1.1
      #setenv CC /export/ld4/set/dist/sparc-S2/SC4.2/bin/cc

      $JH/bin/javac -d . *.java
      $CC -KPIC -G -I$JH/include -I$JH/include/solaris -o libtest.so *.c
      setenv LD_LIBRARY_PATH ".:$LD_LIBRARY_PATH"
      setenv CLASSPATH .
      $JH/bin/java javasoft.sqe.tests.vm.sofl001.sofl00101.sofl00101
      ::::::::::::::
      sofl00101.c
      ::::::::::::::
      /* Ident: %Z%%M% %I% %E% */
      /* Copyright %G% Sun Microsystems, Inc. All Rights Reserved */

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include "jni.h"

      #ifdef __cplusplus
      extern "C" {
      #endif

      #ifndef JNI_ENV_ARG
      #ifdef __cplusplus
      #define JNI_ENV_ARG(x,y) y
      #define JNI_ENV_PTR(x) x
      #else
      #define JNI_ENV_ARG(x,y) x,y
      #define JNI_ENV_PTR(x) (*x)
      #endif
      #endif

      JNIEXPORT void JNICALL
      Java_javasoft_sqe_tests_vm_sofl001_sofl00101_sofl00101_sofl00101
      (JNIEnv *env, jobject obj, jint ival, jobject oval)
       {
      jclass jc;
      jfieldID jfi;

      jc = JNI_ENV_PTR(env) -> GetObjectClass(JNI_ENV_ARG(env,obj));
      jfi = JNI_ENV_PTR(env) -> GetFieldID(JNI_ENV_ARG(env,jc),"obj_f_fld","Ljava/lang/Object;");
      JNI_ENV_PTR(env) -> SetObjectField(JNI_ENV_ARG(env,obj),jfi,oval);
      jfi = JNI_ENV_PTR(env) -> GetFieldID(JNI_ENV_ARG(env,jc),"int_f_fld","I");
      JNI_ENV_PTR(env) -> SetIntField(JNI_ENV_ARG(env,obj),jfi,ival);
       }
      #ifdef __cplusplus
      }
      #endif
      ::::::::::::::
      sofl00101.java
      ::::::::::::::
      // Ident: %Z%%M% %I% %E%
      // Copyright %G% Sun Microsystems, Inc. All Rights Reserved

      package javasoft.sqe.tests.vm.sofl001.sofl00101;
      import java.io.PrintStream;

      public class sofl00101 {

        public final Object obj_f_fld = new char[12];
        public final int int_f_fld = 1;
        private static Object ob;

        public static void main(String argv[])
        {
          sofl00101 tob = new sofl00101();
          ob = "vv";
          tob.sofl00101(2, ob);
          if ( tob.int_f_fld == 2 ) {
            System.out.println("Final int field has changed");
          } else {
            System.out.println("Final int field has not changed");
          }
          if ( tob.obj_f_fld.equals(ob) ) {
            System.out.println("Final Object field has changed");
          } else {
            System.out.println("Final Object field has not changed");
          }
        }

        public native void sofl00101(int ival, Object oval);

        static {
          try {
            System.loadLibrary("test");
          } catch ( UnsatisfiedLinkError e ) {
            System.loadLibrary("jckjni");
          }
        }
      }
      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            sliangsunw Sheng Liang (Inactive)
            Reporter:
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: