0

I am constructing a framework where I need to make copies of objects at runtime and I do not want to force those object classes to implement any interface, like Cloneable, or Prototype pattern, or anything else.

So, for that purpose I was planning to efficiently clone those objects using the functionality of sun.misc.Unsafe and its methods: allocateInstance(Class cls) and copyMemory(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes). But I cannot find any way to get the size of an object in bytes in the heap space.

I know that I can use reflection and copy from field to field. But that is not no efficient. Do you know any manner of getting the size of an object in the heap? Or any other way of efficiently copying an object?

Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94
  • 4
    You cannot copy an arbitrary object. Doing so can be dangerous. What are you trying to do? – SLaks Mar 07 '12 at 17:11
  • You could hurt yourself! – Zaki Mar 07 '12 at 17:18
  • I am trying to copy an object at runtime and I do not have any previous information about that object at compile-time. I cannot neither force it to implement any Interface, nor provide any Factory or anything else. – Miguel Gamboa Mar 07 '12 at 18:00
  • possible duplicate of [Java: recommended solution for deep cloning/copying an instance](http://stackoverflow.com/questions/2156120/java-recommended-solution-for-deep-cloning-copying-an-instance) – Bozho Mar 08 '12 at 07:56

2 Answers2

3

The right way to do this is really, honestly, to use the object's own methods, constructors, and factories to do the copy. clone is broken badly enough without abusing it like this.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Clones are there to be abused... O:-) – mcfinnigan Mar 07 '12 at 17:19
  • If you really want abusive, take an innocent class, subclass it implementing `Cloneable`. >:) – Tom Hawtin - tackline Mar 07 '12 at 17:32
  • Maybe I did not explain myself clear, but I am talking about an "unkown object". Some object, which you do not have any information at compile-time and that you cannot force to implement any specific interface. – Miguel Gamboa Mar 07 '12 at 17:55
  • Refactor your code until you _do_ have that information at compile time. There isn't really another option -- certainly there's _no_ way of doing this I would trust to work on an arbitrary `Object`. – Louis Wasserman Mar 07 '12 at 18:35
  • Once I am using the ASM bytecode instrumentation tool, then I can get the size of an Object trough the method `getObjectSize(Object objectToSize)` from the class `java.lang.instrument.Instrumentation`. Problem solved. – Miguel Gamboa Mar 07 '12 at 19:26
  • I would be absolutely shocked if that still didn't break anything. Many types depend on being able to control all instances of that type -- for example, enums, singleton types, and the like all _depend_ on knowing all instances of that type at compile time, and you'll break them all. Find some other way. Please. – Louis Wasserman Mar 07 '12 at 19:47
  • There is nothing to break! And, if you receive `Object` as an argument how can you determine its fields at compile time? – Miguel Gamboa Mar 08 '12 at 14:07
  • You _rewrite your project so you get a more specific type._ Would you like an example? Suppose you get a value of some `enum` type, and you clone it. Enum types use `==` for equality, so now your new value isn't actually equal to the original value. Sets, maps, and so on all break. – Louis Wasserman Mar 08 '12 at 17:33
  • I am developing a java instrumentation tool with ASM and I do not want to force users to implement or extend a specific type. So, the response to: “You rewrite your project so you get a more specific type.” is: I do not want to get a more specific type. – Miguel Gamboa Mar 09 '12 at 16:38
  • As long as you're okay with potentially breaking their code, that's all right then. – Louis Wasserman Mar 09 '12 at 17:57
0

Once I am using the ASM bytecode instrumentation tool, then I can get the size of an Object trough the method getObjectSize(Object objectToSize) from the class java.lang.instrument.Instrumentation. Problem solved.

Miguel Gamboa
  • 8,855
  • 7
  • 47
  • 94