Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How can I use 'declare' to verify that a class implements a certain interface?

Hi -

See EI.java below: it puts the predicate in the type
pattern for the staticinitialization pointcut.  You were
correct to use staticinitialization; there is such a join
point in every class.  Using within() alone will pick out
all join points in an offending class, which is probably
not what you want.

hth - Wes

(P.S. - it works for marker interfaces, too.)

------------------------------------ compiling EI.java
 src $ aspectj-1.5 -1.5 EI.java
C:\home\wes\src\EI.java:7 [error]  E is I
@Entity class CE {}
              ^
        staticinitialization(void CE.<clinit>())
        see also: C:\home\wes\src\EI.java:10::0

1 error
---------------------- EI.java

@interface Entity {}
interface I {}

class C {}
class CI implements I {}
@Entity class CE {}
@Entity class CEI implements I {}
aspect A {
    declare error : 
        staticinitialization((@Entity *) && !(I+)) 
        : " E is I";
    // hmm not sure why this doesn't work
    //declare error : 
       //staticinitialization(@Entity !(I+)) : " E is I";
}

On Sun, 8 Jan 2006 09:51:49 -0600
 Dean Wampler <deanwampler@xxxxxxxxx> wrote:
> This appears to work:
> 
> public aspect ModelCheck {
> 	declare error:
> 		 within (@Entity mypackage.*) &&
> 		!within(mypackage.MyInterface+): "Must implement
> MyInterface";
> }
> 
> but note that this doesn't:
> 
> 	declare error:
> 		 within (@Entity !mypackage.mypackage.MyInterface+):
> "Must implement
> MyInterface";
> 
> As an alternative, you could be more more optimistic: ;)
> 
> declare parents: (@Entity *) implements MyInterface
> 
> Then you'll get a regular compiler error for those
> classes that
> "forget" to implement the interfaces methods. This may
> not meet your
> needs if the interface is an empty marker interface, in
> which case
> you'll get no errors!
> 
> dean
> 
> 
> On 1/8/06, Jeppe Cramon <jeppe@xxxxxxxxx> wrote:
> >
> > Hi
> >
> >  I've stumbled across the need to enforce a contract,
> using declare error,
> > that all classes that have the @Entity  annotation also
> needs to implement a
> > certain interface (here called MyInterface). Since
> this() can't be used with
> > declare, I've tried to use staticinitialization()
> instead, but it doesn't
> > work.
> >
> >  Does any one have a suggestion on how to implement
> such a check?
> >
> >  Here's the sample aspect:
> >  package com.betchallenge.model;
> >
> >  import com.myproject.MyInterface;
> >  import javax.persistence.Entity;
> >
> >  public aspect ModelCheck {
> >     declare error: within(@Entity *) &&
> > !staticinitialization(MyInterface):
> >         "An Entity must implement MyInterface";
> >  }
> >
> >  Thanks in advance :)
> >
> >  Best regards
> >
> >  Jeppe
> >
> >
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> >
> >
> 
> 
> --
> Dean Wampler
> http://www.aspectprogramming.com
> http://www.newaspects.com
> http://www.contract4j.org
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top