4 Replies Latest reply on Jun 6, 2008 5:45 PM by 843810

    Comparison of local variables and methods

    843810
      Hello all,

      I am comparing between local variable instances as a class is executing to determine whether local variables are either newly created or modified. I am using LocalVariable.equals() for the local variable currently being processed by the virtual machine and a list of local variables i am building throughout execution. It seems that LocalVariable.equals() return true if the local variable is initialized in the same class but different thread. Also it returns true if it was initialized in the same method but different invocation (example in recursion)

      Regards,

      Ahmed Ashmawy
        • 1. Re: Comparison of local variables and methods
          800575
          I guess the way to think about a LocalVariable is that it corresponds to the line on which the variable is declared in the .java file; it has nothing to do with execution, ie threads, stacks, recursion, ... Thus, the behavior you report is correct. Note that if a method contained two declarations with the same name, eg
          {
          String str;
          :
          }
          {
          String str;
          :

          then there would be two LocalVariables with name 'str', and they would not be .equals().
          • 2. Re: Comparison of local variables and methods
            843810
            The .equal() method for the class LocalVariable is defined to return true if the Object is a LocalVariable, if both LocalVariables are contained in the same method (as determined by Method.equals(java.lang.Object)), and if both LocalVariables mirror the same declaration within that method

            If i have a recursive method say recMethod()

            recMethod()
            {
            int localVariable;
            }

            the variable "localVariable" should be mirrored by a different object for each invocation of the method recMethod() and therefore they should not be equal.
            • 3. Re: Comparison of local variables and methods
              800575
              Well, as I said in my previous post, a LocalVariable represents a variable declaration in a method, not a slot on the stack frame of an executing instance of that method. Note, therefore, that LocalVariable has no getValue() method. Instead you have to get a StackFrame that represents an execution of a method and use one of the methods that gets values of LocalVariables from that StackFrame.
              • 4. Re: Comparison of local variables and methods
                843810
                Thanx , ill handle it by taking into consideration method entry and method exit events.