Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Is there a common syntax for a compile time "if" pointcut?

> What I want is to be able to configure an aspect that will declare a
> warning, error or ignore some condition like calls to System.out.

I like the idea of configurable levels because declare error/warning would 
be most useful if you could change the volume at will.  There's something

There's no if/switch, but you can use a pointcut and duplicate error/warning:

class EL { // EL: error level (for brevity here)
  pointcut errorsOn() : ...
  pointcut warningsOn() : ...
}

aspect A {
  // for each pointcut/message...
  declare error : EL.errorsOn() && foo() : "foo";
  declare warning : EL.warningsOn() && foo() : "foo";

  ...
}

Then use, e.g., "within(*)" as "always" and no definition as "never":

Only warnings:
  pointcut errorsOn();
  pointcut warningsOn() : within(*);

Only errors: 
  pointcut errorsOn() : within(*); 
  pointcut warningsOn();

Neither:
  pointcut errorsOn();
  pointcut warningsOn();

It's not pretty, but it could work.

You make make an enhancement request for Xlint-style support for
compile/weave-time declaration of the error level:

   declare message : {pointcut} : {text};

   is like declare error and declare warning, except that it uses
   the "message" error level, which is by default "warning", but
   can be set explicitly to "ignore" "error" or "warning" using the
   [system property or flag or some such]

not sure if we could extend the syntax to permit naming:

   declare message {name} : {pointcut} : {text};

   {same as above, except that message level can be set by name}


Wes

> ------------Original Message------------
> From: Scott Hayward <shayward@xxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Wed, Jun-1-2005 9:45 AM
> Subject: [aspectj-users] Is there a common syntax for a compile time "if" pointcut?
>
> Hi,
> 
> I'm trying to write an aspect that will declare either a warning or 
> error,
> depending on the value of some constant. However, the "if" pointcut
> designator cannot be used with declare warning/error because it 
> evaluates
> at run time. I need something at compile time.
> 
> What I want is to be able to configure an aspect that will declare a
> warning, error or ignore some condition like calls to System.out.
> 
> First, I define a set of constants in a class:
> 
>       /** This constant defines whether a static aspect should generate 
> an
> error */
>       public static final String CFG_ERROR = "ERROR";
>       /** This constant defines whether a static aspect should generate 
> a
> warning */
>       public static final String CFG_WARN = "WARNING";
>       /** This constant defines whether a static aspect should be 
> ignored
> */
>       public static final String CFG_IGNORE = "IGNORE";
> 
>       /** This constant configures whether to generate an error, 
> warning or
> ignore */
>       public static final String CFG_STATIC_TEST_XYZ = CFG_ERROR;
> 
> Now I want to write aspects like this:
> 
>       /** This pointcut determines if the static test condition is 
> found */
>       public pointcut staticTestXYZ() : <<some pointcut, like writing 
> to
> System.out>> ;
> 
>       /** Declare an warning if configured to do so */
>       declare warning : staticTestXYZ() &&
>             <<need compile time test for CFG_STATIC_TEST_XYZ=CFG_WARN>> 
> :
>                   "I'm warning you not to do XYZ";
> 
>       /** Declare an error if configured to do so */
>       declare error : staticTestXYZ() &&
>             <<need compile time test for 
> CFG_STATIC_TEST_XYZ=CFG_ERROR>> :
>                   "You are not permitted to do XYZ";
> 
> 
> Is there a standard syntax for doing things like this?
> 
> 
> Thanks,
> 
> Scott.
> _______________________________________________________
> Scott Hayward
> Advisory IT Specialist
> IBM Canada Ltd., Pacific Development Centre
> 4611 Canada Way, Burnaby, BC, V5G 4X3 CANADA
> Dir (604) 297-3096            Fax (604) 297-3030      4 / PA1 / 4611 / 
> BURN
> email:  shayward@xxxxxxxxxx    Web: http://www.can.ibm.com/pdc
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 



Back to the top