7 Replies Latest reply on Jan 27, 2006 6:49 AM by jmarranz

    How to value a byte array

    dechamps

      Hi,

      I begin to use Javassist and need some help.
      I would like to add and value in the bytecode of class A a field
      private final static byte[] x;
      Of course I have also to insert a method public static getX(){return x;}

      The creation of the field looks like:
      CtClass A=....
      String fs="private final static byte[] x;"
      CtField f=CtField.make(fs,A);
      byte[] ca=computeRandom();// for example
      ...and here I'm stucked..
      Can you give an example that shows how x can be valued with the content of ca ?
      Many thanks

      Dominique

        • 1. Re: How to value a byte array
          chiba

          Well, the value of fs should be something like:

          String fs = "private static byte[] x = { 1, 2, 3 }";

          Unfortunately, "{ 1, 2, 3 }" has not been supported yet,
          you must use insertBefore in CtConstructor to
          append the code like:

          { x[0] = 1; x[1] = 2; x[2] = 3; }

          • 2. Re: How to value a byte array
            dechamps

            Ok I'll try, hoping it will cover my need. I want to "write" only once in the bytecode a private field whose value (an encoded and compressed one) can't be calculated at time of code writing

            String fs = "private static byte[] x = { 1, 2, 3 }";
            FYI:I have tried this above and it fails (don't remenber the message), so I have had a look in your Parser code and see a String that might send a warning message about this unsupported feature, but it doesn't raise it?

            • 3. Re: How to value a byte array
              chiba

              If I'm correct, static final fields can be initialized
              in a static class initializer, that is,

              class C {
              static { .... }
              :
              }


              • 4. Re: How to value a byte array
                dechamps

                Bonjour,

                You are right about static initializer. So I have done an insert before of
                String build with a lot of b[O]=1,b[1]=........b[2002=0
                That's ok, it works.
                But the code of my class become a huge one
                Before insertion java bytecode=1377 bytes
                After insertion of 2 byte arrays (size=2002+1061 bytes) the java bytecode size=62394 bytes!
                I have also tested directly in the java source the {0,34,78...}, the resulting size is quite the same.

                I decided to use String (initialized with an hexa decimal encoding of my compressed bytes) instead of byte arrays then the java bytecode size=7123 bytes
                Better, even not perfect
                Thanks you

                • 5. Re: How to value a byte array
                  chiba

                  Yes, array initialization is quite inefficient in Java.
                  I think your technique using String is a right way.

                  • 6. Re: How to value a byte array
                    jmarranz

                    I have a trick to solve this problem, create a utility class like:

                    class ByteArray
                    {
                    private byte[] array;
                    public ByteArray(int size)
                    {
                    array = new byte[size];
                    }
                    public ByteArray set(int index,byte b)
                    {
                    array[index]=b;
                    return this;
                    }
                    public byte[] getArray()
                    {
                    return array;
                    }
                    }


                    Then this code now works:

                    private static byte[] x = new ByteArray(3).set(0,1).set(1,2).set(2,3).getArray();

                    ok, is not marvellous and not very efficient, but works :)

                    Jose M. Arranz

                    • 7. Re: How to value a byte array
                      jmarranz

                      With minor modifications , it may be simpler

                      class ByteArray
                      {
                      private byte[] array;
                      private int counter = 0;
                      public ByteArray(int size)
                      {
                      array = new byte[size];
                      }
                      public ByteArray add(byte b)
                      {
                      array[counter]=b;
                      counter++;
                      return this;
                      }
                      public byte[] getArray()
                      {
                      return array;
                      }
                      }

                      private static byte[] x = new ByteArray(3).add(1).add(2).add(3).getArray();

                      Jose M. Arranz