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

Oh and I'm using Eclipse 3.1.1 and the latest AJDT and I've turned on hasMember in the advanced section of the AspectJ compiler.

/Jeppe

Jeppe Cramon wrote:
Thanks to all for their replies. I'm amazed by how fast you can get help :)

I tried your solution Adrain, but now it marks errors for all classes the extend IOption no matter if they implement the constructor or not.

My aspect looks like the following:
public aspect OptionAspect {
   
   interface IBadOptionSubtype {};

    declare parents : (IOption+ && !IOption && !IXOption && !IYOption && !IZOption) && !hasmethod(new(OptionType))
                             implements IBadOptionSubtype;

    declare error : staticinitialization(IOption+ && IBadOptionSubtype+)
        : "IOption implementations must provide a constructor which accepts an OptionType";
   
}

And an implementation looks like this:

public class MyXOption implements IXOption {
    public MyXOption(IXOption x) {
    }
}

Is it a bug or am I forgetting something?

/Jeppe

Adrian Colyer wrote:
You've asked an "easter egg" question :)

As discussed in the answers to this post, you can't match the "absence" of a join point, and therefore you can't do this statically in AspectJ.

But...AspectJ 5 contains an undocumented experimental X option : -XhasMember. It enables the compiler to support hasmethod(method_pattern) and hasfield(field_pattern) type patterns - but only within declare statements. It's experimental and undocumented because it may change, and because it doesn't yet take into account ITDs. Caveat emptor.

With that said, if you type the following into a file "OptionAspect.aj" :

public aspect OptionAspect {

    interface BadOptionSubtype {};

    declare parents : (IOption+ && !IOption) && !hasmethod(new(Option))
                             implements BadOptionSubtype;

    declare error : staticinitialization(IOption+ && BadOptionSubtype+)
        : "IOption implementations must provide a constructor which accepts an Option type";

}

class Option {}

interface IOption {}

class GoodIOption implements IOption {

    public GoodIOption(Option o) {}

}

class BadIOption implements IOption {

}

and compile it using : ajc -XhasMember OptionsAspect.aj

you should see the output:

C:\temp\OptionAspect.aj:23 [error] IOption implementations must provide a constructor which accepts an Option type
class BadIOption implements IOption {
      ^^^^^^^^^
        staticinitialization(void BadIOption.<clinit>())
        see also: C:\temp\OptionAspect.aj:8::0

1 error

Did I say caveat emptor? :)

On 11/01/06, Jeppe Cramon <jeppe@xxxxxxxxx> wrote:
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



--
-- Adrian
adrian.colyer@xxxxxxxxx

_______________________________________________ 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