Community
Participate
Working Groups
I have some advice that calls clone() on an advised object, inside around advise. The attached example always throws CloneNotSupportedException. Curiously though, the real program where I first saw this problem actually threw a NoSuchMethodError with this message "java.lang.Object.ajc$privMethod$org_contract4j_aspects_Contract4J$java_lang_Object$clone()Ljava/lang/Object;)". org.contract4j.aspects.Contract4J is the aspect in my Contract4J project that invokes clone on a test class that looks almost identical to the attached CloneTestClass.java. While I'm at it ;), here is one other wierd thing I've seen while debugging. I can't set a breakpoint inside the advice in CloneAdvisedClass.aj; execution just doesn't stop there.
Created attachment 31524 [details] Aspect that invokes clone() in advice
Created attachment 31525 [details] Class that is advised, which has a clone() method
Created attachment 31526 [details] JUnit test that drives everything.
From the javadoc for Object.clone(): The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation. --- So I changed your program such that CloneTestClass is marked 'implements Cloneable' and it seems to work now... On the mangled method thing - looks like something related to accessing a non public clone() method, possibly from advice... doing that just *sounds* like its going to cause trouble.
No response, so I presume Andy's reply was accepted. stalebug