Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] capturing methods without a call to another method

It's very easy to enforce your policy with around advice:

//[warning: uncompiled]
Object[] around() : execution(*[] *(..)){
	Object[] original = proceed();
	return original.clone();
}

However, this advice runs the risk of double-cloning if the method already clones the array. Also, a method could allocate a local array which you might not want cloned...

As to detection, well, that's a bit more difficult, since you would have to keep state about whether a given array had had clone called on it at some point during the cflow of a method. You might be able to do this with a percflow() aspect, but the question you would want to ask is: why not just enforce the policy automatically with advice (as above)?

Cheers,
Nick


On Aug 12, 2004, at 8:30 AM, Rohith Ajjampur wrote:

Hello all,

I am trying to implement a policy of cloning an array before retuning a refence
to it in a method across an API, here is my test code with an aspect:

public aspect HelloWorldAspect {
    pointcut hello(): !within(HelloWorldAspect);

    pointcut method(): execution(*[] *(..));

    pointcut cloning(): call(* java.lang.Object.clone());

    before(): cflow(method()) && cloning() && hello() {
        print("Before : ", thisEnclosingJoinPointStaticPart);
    }

    after(): cflow(method())&& cloning() && hello() {
        print("After : ", thisEnclosingJoinPointStaticPart);
    }
}

public class World {
 private Integer[] intArray = new Integer[2];

    public Integer[] returnArrayWithCloning() {
     for (int i = 0; i < intArray.length; i++) {
        intArray[i] = new Integer(i++);
     }
     return (Integer[])intArray.clone();
    }

    public Integer[] returnArrayWithoutCloning() {
     return intArray;
    }
}

With the above pointcuts and advices i get all the calls to the method
World.returnArrayWithCloning(), that is quiet clear, but i would like to get a list of all the methods that return an array without calling its clone() method.
Please suggest me how to get this result.

Regards,
Rohith.


_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-users


Nicholas Lesiecki
Software Craftsman, specializing in J2EE,
Agile Methods, and aspect-oriented programming

Books:
* Mastering AspectJ: http://tinyurl.com/66vf
* Java Tools for Extreme Programming: http://tinyurl.com/66vt

Articles on AspectJ:
* http://tinyurl.com/66vu and http://tinyurl.com/66vv



Back to the top