Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-dev] Restricting type from user defined pointcut

As a follow up, the "hold your nose" version gets even worse in the case I'm
dealing with, where the expression is bound in a cflow:

...
    public pointcut inRequest(RequestContext requestContext) :
        cflow(requestExecution(requestContext))

    //worse redundancy:
    public pointcut inDerivedRequest(DerivedRequestContext drc) : 
        cflow(requestExecution(*) && this(drc));

-----Original Message-----
From: aspectj-dev-bounces@xxxxxxxxxxx
[mailto:aspectj-dev-bounces@xxxxxxxxxxx] On Behalf Of Ron Bodkin
Sent: Wednesday, August 31, 2005 2:01 PM
To: 'AspectJ developer discussions'
Subject: [aspectj-dev] Restricting type from user defined pointcut

I would like to be able to test if a variable bound in a named pointcut is
an instance of a subtype. For example, with

public abstract aspect Base {
    public pointcut requestExecution(RequestContext requestContext) :
        execution(* RequestContext.execute(..)) && this(requestContext);
}

I'd like to write a pointcut to say "a requestExecution where the bound
variable is an instance of the subclass DerivedRequestContext." The
"natural" thing to do fails:

    public pointcut derivedRequestExecution() : 
        requestExecution(DerivedRequestContext);

C:\devel\scratch\inner\Base.aj:9 [error] incompatible type, expected
RequestContext found DerivedRequestContext
requestExecution(DerivedRequestContext);
                 ^^^^^^^^^^^^

1 error

Now usually people faced with this situation will just hold their nose and
write redundant code (which needs to be coupled to the details of how the
used pointcut binds the variable):

    public pointcut derivedRequestExecution() : 
        requestExecution(*) && this(DerivedRequestContext);

It is actually possible to write a modular version of this with AspectJ
pointcuts, but it's verbose and ugly:

    private pointcut derivedRequestExecutionParam(RequestContext
requestContext) : 
        requestExecution(requestContext) && if(requestContext instanceof
DerivedRequestContext);

   public pointcut derivedRequestExecutionOk() :
derivedRequestExecutionParam(*);

In essence, I think it would be very useful if user defined pointcuts can
filter on bound values just like the system defined pointcuts for this,
target, and args. Is it too hard to implement this? Or do people think not
allowing this is genuinely the right behavior? I find the asymmetry
troubling, and the existing support for modularly doing this kludgely.
Ramnivas reminded me that Adrian blogged about this a year ago to say
essentially that this is the expected behavior (see
http://www.aspectprogrammer.org/blogs/adrian/2004/05/effective_aspec.html).



Appendix (complete code sample)
public abstract aspect Base {
    public pointcut requestExecution(RequestContext requestContext) :
        execution(* RequestContext.execute(..)) && this(requestContext);

    public pointcut derivedRequestExecution() : 
        requestExecution(DerivedRequestContext);

    private pointcut derivedRequestExecutionParam(RequestContext
requestContext) : 
        requestExecution(requestContext) && if(requestContext instanceof
DerivedRequestContext);

   public pointcut derivedRequestExecutionOk() :
derivedRequestExecutionParam(*);

}

    abstract class RequestContext {
        public abstract Object execute();
    }

    abstract class DerivedRequestContext extends RequestContext {
    }

Ron Bodkin
Chief Technology Officer
New Aspects of Software
w: (415) 824-4690


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



Back to the top