Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How to declare an error when implementations of an interface doesn't supply a certain constructor

Hi Matthew,

2006/1/12, Matthew Webster <matthew_webster@xxxxxxxxxx>:
>
> Hidehiko,
>
> So let me qualify what I said. This is not possible with simple pointcuts
> and advice (declare error/warning) so you cannot police the rule at compile

Exactly.

> time. Obviously anything is possible at runtime and it would seem that SCoPE
> essentially allows user written advice to be executed during compilation
> through an if() pointcut.

Let me rephrase more precisely: SCoPE performs pointcut matching at
compile time including if-pointcut when it is "statically
determinable".  An if-pointcut is statically determinable in SCoPE
when the expression inside does not touch runtime values (e.g., the
ones obtained through variables bound by args() and calls to
JoinPoint.getArgs()).

Since the hasConstructor() relies only on static parts of join points
(and SCoPE can analyze such a property), the pointcut is thus matched
at compile time.  When you write it in declare error, it produces
errors at compile time.  And when you write it in an advice
declaration, the advice is woven into matching join point shadows
without runtime tests.

Hidehiko
>
> Matthew Webster
>  AOSD Project
>  Java Technology Centre, MP146
>  IBM Hursley Park, Winchester,  SO21 2JN, England
>  Telephone: +44 196 2816139 (external) 246139 (internal)
>  Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
> http://w3.hursley.ibm.com/~websterm/
>
> Please respond to Hidehiko Masuhara <masuhara@xxxxxxxxxxxxxxxxxxxxx>; Please
> respond to aspectj-users@xxxxxxxxxxx
>
> Sent by:        aspectj-users-bounces@xxxxxxxxxxx
>
> To:        Matthew Webster/UK/IBM@IBMGB
> cc:        aspectj-users@xxxxxxxxxxx, Hidehiko Masuhara
> <masuhara@xxxxxxxxxxxxxxxxxxxxx>
>
> Subject:        Re: [aspectj-users] How to declare an error when
> implementations of        an interface doesn't supply a certain constructor
>
>
> Hi Matthew,
>
>  This is possible because we can still check the required property of
>  declaring type at static initialization join points, rather than
>  checking at constructor join points.  The following code, which can be
>  compiled by ajc, demonstrates how we can check such a property (at
>  runtime because ajc does not allow to write if-pointcut in declare
>  error/warning).  The solution in SCoPE is to merely use the same
>  pointcut in the declare error.
>
>  Hidehiko
>
>  /*
>  $ java OptionAspect
>  class C2 should have a constructor which accepts an OptionType.
>  $
>  */
>  import org.aspectj.lang.JoinPoint;
>
>  interface IOption { }
>  class OptionType { }
>  class C1 implements IOption { C1(OptionType t) { } }
>  class C2 implements IOption { C2() { } }
>
>  public aspect OptionAspect {
>   static boolean hasConstructor(JoinPoint jp, Class[] parameterTypes) {
>     try { // test if the join point is declared in a class that
>           // has a constructor with specified parameter types.
>       jp.getSignature().getDeclaringType()
>         .getDeclaredConstructor(parameterTypes);
>       return true;
>     } catch (NoSuchMethodException e) {
>       return false; // when no such a constructor
>     }
>   }
>
>   pointcut initOfClassWithoutConstructor():
>     staticinitialization(IOption+) &&
>     if(!hasConstructor(thisJoinPoint, new Class[] {
> OptionType.class }));
>
>   before(): initOfClassWithoutConstructor() {
>     System.out.println
>       (thisJoinPoint.getSignature().getDeclaringType()
>        + " should have a constructor which accepts an OptionType.");
>   }
>
>   public static void main(String[] args) {
>     Class dummy1 = C1.class, dummy2 = C2.class; // force class
> initialization
>   }
>  }
>
>
>  2006/1/12, Matthew Webster <matthew_webster@xxxxxxxxxx>:
>  >
>  > Hidehiko,
>  >
>  > This will not work because you are trying to advise the _absence_ of a
> join
>  > point, i.e. the constructor with a particular signature, which is not
>  > possible. You can think of a declare error/warning as advice that
> executes
>  > at compile time. There is no joint point with which to associate the
> advice.
>  >
>  > Matthew Webster
>  >  AOSD Project
>  >  Java Technology Centre, MP146
>  >  IBM Hursley Park, Winchester,  SO21 2JN, England
>  >  Telephone: +44 196 2816139 (external) 246139 (internal)
>  >  Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
>  > http://w3.hursley.ibm.com/~websterm/
>  >
>  > Please respond to Hidehiko Masuhara <masuhara@xxxxxxxxxxxxxxxxxxxxx>;
> Please
>  > respond to aspectj-users@xxxxxxxxxxx
>  >
>  > Sent by:        aspectj-users-bounces@xxxxxxxxxxx
>  >
>  > To:        aspectj-users@xxxxxxxxxxx
>  > cc:
>  > Subject:        Re: [aspectj-users] How to declare an error when
>  > implementations of        an interface doesn't supply a certain
> constructor
>  >
>  >
>  > My apologies to for my previous message, which is accidentally sent to
>  >  the entire list.  But I thought that the problem might be solved by
>  >  our extended AspectJ compiler (called SCoPE), which allows you to
>  >  write declarations by using if-pointcut like:
>  >   declare error: staticinitialization(...) &&
>  >  if(thisJoinPoint.getSignature()....)
>  >
>  >  I'd like to send our solution again when we find the way to do it.
>  >  More information on SCoPE can be found from the follwing address:
>  >  http://www.graco.c.u-tokyo.ac.jp/ppp/projects/scope/
>  >
>  >  Best regards,
>  >
>  >  Hidehiko
>  >
>  >  2006/1/12, Jeppe Cramon <jeppe@xxxxxxxxx>:
>  >  > Hi
>  >  >
>  >  > I'm trying to declare an error when implementations of a certain
>  >  > interface doesn't supply a certain constructor.
>  >  > I'm both having difficulty limiting the scope to only implementations
> of
>  >  > the interface (IOpen+) seems too large a granularity and specifying
> the
>  >  > constructor.
>  >  >
>  >  > I've tried with this aspect:
>  >  >
>  >  > public aspect OptionAspect {
>  >  >     declare error: staticinitialization(IOption+) &&
>  >  > !execution(IOption+.new(OptionType)) : "IOption
>  > implementations must
>  >  > provide a constructor which accepts an OptionType";
>  >  > }
>  >  >
>  >  > Could you give me a pointer to what I'm missing/doing wrong?
>  >  >
>  >  > Thanks in advance :)
>  >  >
>  >  > /Jeppe
>  >  >
>  >  > _______________________________________________
>  >  > aspectj-users mailing list
>  >  > aspectj-users@xxxxxxxxxxx
>  >  >
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>  >  >
>  >  _______________________________________________
>  >  aspectj-users mailing list
>  >  aspectj-users@xxxxxxxxxxx
>  >  https://dev.eclipse.org/mailman/listinfo/aspectj-users
>  >
>  >
>  _______________________________________________
>  aspectj-users mailing list
>  aspectj-users@xxxxxxxxxxx
>  https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>


Back to the top