0

I am aware arrays have a fixed size field. I can edit this in reflection to increase it's value. But, I don't think that's the only thing I would have to edit the array size via reflection. I cannot seem to find the source for the array class online of [L.

Current progress:

Object[] foo = new Object[0];
Field field = foo.getClass().getField("size");

This results in a NoSuchFieldException.

I was trying to hack Java similar to how enums were hacked. Although I don't know where everything is stored. I do have access to the reflection factory: https://www.niceideas.ch/roller2/badtrash/entry/java_create_enum_instances_dynamically

Tom
  • 16,842
  • 17
  • 45
  • 54

1 Answers1

1

It is not possible to (safely) change the size of a Java array without reallocating it.

The length "field" of an array is not a field at all. Syntactically, it is a special case. At the implementation level, it is a 32-bit word in the array object's header. You cannot change the value in the word using reflection. And if you figured out some other way to change it, you would be inviting trouble (JVM crashes) because:

  • the length is what stops you writing beyond the end of the array,
  • the memory beyond the end of the array is most likely used to represent other objects, and
  • various low-level things (such as the JIT compiler and the garbage collectors) assume (correctly!) that the length of an array object cannot change, and may cache it in a native code variable or a register.

[trying to use reflection] .... results in a field not found exception.

That is because length is not a field; see above.

I cannot seem to find the source for the static array class online of [L

(That is the type name for long[]) You won't find it. It doesn't exist. There is no Java source code for any Java array type.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216