5 Replies Latest reply: Jan 21, 2011 5:40 PM by jschellSomeoneStoleMyAlias RSS

    Object passing then assiging a proper address value

    832857
      I want to know how to find a way of implementing a JNI code that after passing an _"null object"_ to C site then assinging this object a proper instance in C site. For instance

      I have classess:

      class Param{
      String value;
      }

      class From {

      Param param;

      }


      From from;
      public static void native objectSetting(from)


      /******************************************/
        • 1. Re: Object passing then assiging a proper address value
          832857
          Here is the C and Java code that I have coded, as you may see from that following code that, the object i have passed "from" is not assigned so still be null. However return value is not null.

          public class ObjectPassing {
               public static native From parceThenAssign(From from);
               
               static {
                    System.loadLibrary("JniObjectPassing");
               }

               public static void main(String[] args) {
                    From from = null;
                    From fromReturn = null;
                    fromReturn=parceThenAssign(from);
                    if(fromReturn==null){
                         System.out.println("fromReturn is null");
                    }else{
                         System.out.println("fromReturn Value: "+fromReturn.value);
                    }
                    if(from==null){
                         System.out.println("From is null");
                    }else{
                         System.out.println("From value: "+from.value);
                    }
               }

          }//ObjectPassing

          public class From {

               public From() {
                    value = "user2";
               }

               String value;
               Param param;

          }//From

          JNIEXPORT jobject JNICALL Java_ObjectPassing_parceThenAssign
          (JNIEnv *env, jclass cls, jobject obj){

          jclass fromClass;
          jmethodID cid;
          jobject objLocal;

          printf("Constructing From Header\n");

          fromClass=(*env)->FindClass(env, "From");
          if (fromClass == NULL) {
          printf("fromClass is not created");
          return NULL;
          }

          cid = (*env)->GetMethodID(env, fromClass,"<init>", "()V");
          if (cid == NULL) {
          printf("Could not created contructor of From");
          return NULL;
          }

          obj=objLocal=(*env)->NewObject(env,fromClass,cid);


          /* Free local references */
          (*env)->DeleteLocalRef(env, fromClass);

          return objLocal;

          }


          ///output

          fromReturn Value: user2
          From is null
          Constructing From Header
          • 2. Re: Object passing then assiging a proper address value
            796440
            user13808465 wrote:
            I want to know how to find a way of implementing a JNI code that after passing an _"null object"_
            There's no such thing as a null object.
            to C site then assinging this object a proper instance in C site. For instance

            I have classess:

            class Param{
            String value;
            }

            class From {

            Param param;

            }


            From from;
            public static void native objectSetting(from)


            /******************************************/
            If you want the Java to be like this:
            From from = null
            objectSetting(from);
            // now from is not null
            it's not possible.
            • 3. Re: Object passing then assiging a proper address value
              832857
              I think there might be indirect solution, I mean the class(lets say Main.java) which contains From can be passed as a parameter then in C site From object can be assigned by calling setFrom method of Main.
              • 4. Re: Object passing then assiging a proper address value
                796440
                user13808465 wrote:
                I think there might be indirect solution,
                I really don't know what you're trying to accomplish. So far, everything you've said centers on how you're trying to accomplish it--that is, you've been focusing on implementation details, not on the real task at hand.

                So, all I can say is what I said before:
                Foo foo = something;
                whatever.bar(foo);
                // now foo has a different value
                This is impossible in Java. End of story.
                • 5. Re: Object passing then assiging a proper address value
                  jschellSomeoneStoleMyAlias
                  user13808465 wrote:
                  I think there might be indirect solution, I mean the class(lets say Main.java) which contains From can be passed as a parameter then in C site From object can be assigned by calling setFrom method of Main.
                  No. Because 'from' is a local variable.

                  You can create a wrapper class. It has a data member 'from'. You pass in an instance (not null) of the wrapper class. Your JNI code creates an instance and assigns it to the member of the instance. On exit the wrapper class has a member which is not null.