7 Replies Latest reply: Jan 11, 2007 6:05 AM by Kabir Khan RSS

    Array access

    Kabir Khan Master

      Hi Chiba,

      Just got back from meeting everybody in Las Vegas. A new thing a lot of people need is interception of access to elements in arrays.

      http://jira.jboss.com/jira/browse/JBAOP-265

      I think this could work similar to how we replace field access at the moment via CodeConverter.replaceFieldRead() and CodeConverter.replaceFieldWrite(). I'd like to add (or even better have added by you ;-) some more methods to CodeConverter called something like replaceArrayFieldRead() and replaceArrayFieldWrite() to basically achieve the following

      public class Test{
       long[] array;
      
       //Methods added by AOP
       static long read_array(Object target, int index){
       return array[index];
       }
      
       static void write_array(Object target, int index, long value){
       array[index] = value;
       }
      }
      


      Then users of the array would get transformed by the codeconverter from:

      Test test = new Test()
      
      //test.array[5] = 10; Becomes:
      Test.write_array(test, 5, 10);
      
      //long l = test.array[5]; Becomes:
      long l = Test.readArray(test, 5);
      


      Is this a hard feature to implement? Is it possible at all?

      Thanks,

      Kabir

        • 1. Re: Array access
          Shigeru Chiba Expert

          Implementing this spec. would be tricky. Because:

          test.array[5] = 10;
          


          is compiled into:

          long[] a = test.array;
          a[5] = 10;
          


          It would be difficult to recognize "a" is "test.array" when Javassist transforms "a[5] = 10".




          • 2. Re: Array access
            Ben Wang Master

            So in other words, neither AspectJ (or asm) can do this? Just to confirm.

            • 3. Re: Array access
              Brian Stansberry Master

              Jonas Boner told me they can track changes to arrays. He said how but I had no idea what he meant and was doing a presentation at the time so couldn't really follow up.

              • 4. Re: Array access
                Jason Greene Master

                 

                "chiba" wrote:
                Implementing this spec. would be tricky. Because:

                test.array[5] = 10;
                


                is compiled into:

                long[] a = test.array;
                a[5] = 10;
                


                It would be difficult to recognize "a" is "test.array" when Javassist transforms "a[5] = 10".




                It seems this isn't the case for the sun compiler:

                20: getfield #2; //Field array:[J
                23: iconst_5
                24: lconst_1
                25: lastore

                Or did you mean that a user can assign it to a local var and manipulate it there?

                In that case, couldnt you store a reference map from local vars to arrays you are interested in?

                -Jason


                • 5. Re: Array access
                  Kabir Khan Master

                  Hi Chiba,

                  I heard from Emmanuel that you'd discussed array interception with him and that it might be possible, but with some limitations.

                  Could you please explain the limitations a bit?

                  In particular Emmanuel mentioned differences between different compilers and reassignment of the array variables. As a first cut, I think it would be fine to not allow reassignment of variables, but the Hibernate and POJOCache guys will need to verify if this works for them.

                  Thanks,

                  Kabir

                  • 6. Re: Array access
                    Eliot Moss Newbie

                    I have implemented an Editor for the array fetch and store bytescodes.

                    There is one, possibly significant, limitation at present. For object arrays,
                    it does not know the type. The reason is that this is not trivially
                    apparent from the bytecodes (unlike calls, for example, where you a
                    signature, and fields give their declared type).

                    I have begun writing the code necessary to propagate type information
                    (similar to what is needed in a bytecode verifier, but limited just to this
                    purpose), but won't have the chance to finish it for a while.

                    Note that this just intercepts every usage at a particular code point.

                    • 7. Re: Array access
                      Kabir Khan Master

                      Hi,

                      I've already implemented this http://www.jboss.com/index.html?module=bb&op=viewtopic&t=98175

                      but would be curious to see your approach