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,

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
>
>


Back to the top