6 Replies Latest reply on Apr 8, 2008 3:38 PM by 843810

    Acquiring ObjectReference for a certain object (field,...)


      I need to change values of fields , but to do so i need the field's equivelent ObjectReference Object , how can i get it ?

      right now i am doing the following:
      field.declaringType().classObject().setValue(field, v);

      but it just hangs

      what am i missing ?

      Edited by: 3ashmawy on Apr 7, 2008 2:42 PM
        • 1. Re: Acquiring ObjectReference for a certain object (field,...)
          You should be able to find the answer for this in the jdb source code that I pointed you at earlier.
          • 2. Re: Acquiring ObjectReference for a certain object (field,...)
            I took a loot at it , In Class LValue under debug.expr Its getting the ObjectReference of a Value and then calls from it setValue .. here is the code i am refering to:

            ObjectReference obj = (ObjectReference)value;
            void setValue0(Value val) throws ParseException,
            ClassNotLoadedException {
            if (matchingMethod != null) {
            throw new ParseException("Cannot assign to a method invocation");
            obj.setValue(matchingField, val);
            jdiValue = val;

            I tried that but it output an error indicating that value cannot be casted to objectreference.

            I want to understand the concept in general , i thought it would be as simple as ((ObjectReference)(field)).setValue(field, value) but its not , An explanation of what i should do would be greatly appreciated because this problem i am trying to solve for 3 days now and there isnt enought documentation to explain how this is done :S
            • 3. Re: Acquiring ObjectReference for a certain object (field,...)
              Try to see this class com.sun.tools.example.debug.expr in the jpda examples provided by the Jdk.
              Exactly in th LValue class.

              Edited by: asmainf on 08/04/2008 12:17 ?
              • 4. Re: Acquiring ObjectReference for a certain object (field,...)
                What i have observed from the code of LValue that Everything is derived from value meaning the following:

                ObjectReference obj = (ObjectReference)(value);
                ReferenceType refType = obj.referenceType();

                Since when i try to cast value into ObjectReference it throws an exception saying it couldnt be casted (and that value contained a newly entered value and mirrored of the virtual machine). Therefore that could only mean that at runtime something changed that allow value to be casted to ObjectReference when maybe the VirtualMachine creates a reference to the specified value.

                The code im trying to write that changes the specified field is written in ClassPrepareEventHandler() so at that time did the virtual machine created references to the fields declared with value = null ? because the fields arent initialized yet.

                Also i tried to cast value to object reference in fieldWatchEvent() (when the field is modified ) because the first trial to cast it in ClassPrepareEvent() failed.

                Not to mention that i tried to get the value of a field in ClassPrepareEvent() as follows:
                ReferenceType ref = field.declaringType();
                and that failed and outputed an error java.lang.IllegalArgumentException: Attempt to use non-static field with ReferenceType

                Obviously i am missing the concept in all of this , so a correction would be greatly appreciated
                • 5. Re: Acquiring ObjectReference for a certain object (field,...)
                  I successfully accomplished changing a local variable value from its frame , but still nothing on fields, I have a feeling that the Object that have access to change a field value is Class's ObjectReference (if there is an ObjectReference object for the class itself ...) but i can only get the class's ReferenceType
                  • 6. Re: Acquiring ObjectReference for a certain object (field,...)
                    Ok Now i could get and set the value of Fields but from ModificationWatchPointEvent event.object() . But at that point it would have already began handling modifications of fields , i would like to acquire the Class's Object Reference after the class has been prepared ...