In Java, an array IS AN Object. My question is... is an Object constructor called when new arrays is being created? We would like to use this fact to instrument Object constructor with some extra bytecode which checks length of array being constructed. Would that work?
A:
I don't think so because you can not derive a native array to overridethe constructor
Fernando Miguélez
2009-01-23 22:43:45
I am not sure I understand. Can you elaborate please? (I don't want to override the constructor... I want to put extra bytecode there. Object( ) constructor is called on every object creation. As array is an object too, I expect that Object() is also called in this case)
Peter Štibraný
2009-01-23 22:46:17
+3
A:
Per the JVM spec: "Arrays are created and manipulated using a distinct set of instructions." So, while arrays are instances of Objects, they aren't initialized the same way that other objects are (which you can see if you scroll up from that link anchor).
kdgregory
2009-01-23 22:54:33
Looks like constructor is not called after newarray :-(Thank you.
Peter Štibraný
2009-01-23 23:00:12
A:
You can use byte code manipulation to place the check where ever a new array is created.
Peter Lawrey
2009-01-23 22:56:22
You mean by instrumenting every method in every class? (We wanted to avoid this.)
Peter Štibraný
2009-01-23 22:58:39
A:
Nope. I found this on the AspectJ mailing list: http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg02975.html.
You probably ask about arrays for a reason, but if you can, why not switch to the collection classes. That will give you lots of opportunity to intercept and do validations.
St3fan
2009-01-23 23:26:16