7 Replies Latest reply on Jan 11, 2007 6:05 AM by kabirkhan

    Array access

    kabirkhan

      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
          chiba

          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

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

            • 3. Re: Array access
              brian.stansberry

              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

                 

                "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
                  kabirkhan

                  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
                    eliotmoss

                    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
                      kabirkhan

                      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