Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Question about Generic Aspects

Hi Adrian,

Well, writing that last email got me thinking more about this and I think I might have answered my own question!! The following seems to work, which was slightly unexpected:

public aspect Assoc1 {
 interface HasTest {}	
	
declare parents : Test implements HasTest;
 public int HasTest.test = 0;
}

public aspect Assoc2 {
 interface HasTest {}	
	
declare parents : Test implements HasTest;
 public int HasTest.test = 1;
}

public class Test {
    public static void main(String args[]) {
	Test x = new Test();
	Assoc2.HasTest y = x;
	System.out.println("x.test = " + y.test);
    }
}

So, I presume that this will also work for the genric aspects as well?

Cheers,

Dave


David Pearce wrote:
Hi Adrian,

Adrian Colyer wrote:

The inter-type declarations refered to in that paragraph are
inter-type method, field, and constructor declarations. The declare
family are exempt from this restriction :)


Thanks for that!

So, I have another question about generic aspects. Given what you've said I can do the following:

 > aspect AddStudents<T> {
 >
> interface HasStudents {} >
 >  declare parents : X implements HasStudents;
> > List<Student> HasStudents.students = new ArrayList<Student>();
 > }

To add a field to any class which is the type parameter of a concrete instance of AddStudents. So,

 > aspect FooStudents extends AddStudents<Foo> {}

Adds students to all instances of Foo. I kind of imagine that this will work in much the same way as C++ templates (i.e. some specialised aspect AddStudents__Foo is created with T replaced by Foo and FooStudents extending this). Anyway, i also presume that if I have another aspect extending AddStudents where T=Foo then there will be a compile time error?

Right, so the case that I'm really interested is the following:

 > public aspect Assoc<X,Y> {
> interface HasXs {} > interface HasYs {} > > declare parents : Y implements HasXs;
 >  declare parents : X implements HasYs;
> > public List<Y> HasYs.ys = new ArrayList<Y>();
 >  public List<X> HasXs.xs = new ArrayList<X>();
 > }

So, what I'm doing is effectively creating a bidirectional association between X and Y. The thing is, I need to choose a name for xs and ys. This is annoying because it means I cannot have the following:

 > aspect FooBarAssoc extends Assoc<Foo,Bar> {}
 > aspect FooCarAssoc extends Assoc<Foo,Car> {}

Since, the ys name will clash. So, my question (at last!) is: is there anyway around this ? I guess what I really want to write is:

 >  public List<Y> HasYs.XY_ys = new ArrayList<Y>();
 >  public List<X> HasXs.XY_xs = new ArrayList<X>();

Where XY is just textually replaced with the actually type parameters.

Cheers,

Dave

On 09/09/05, David Pearce <david.pearce@xxxxxxxxxxxxx> wrote:

Hi all,

I have been reading the Aspectj 1.5 note book and have a question about
generic aspects, which I'm hoping someone can help me with.  The
documentation says:

"The type parameter variables from a generic aspect declaration may be
used in place of a type within any member of the aspect, except for
within inter-type declarations."

But, it seems that the example goes on to do just this. Namely:

> public abstract aspect ParentChildRelationship<Parent,Child> {
> ...
>  declare parents: Parent implements ParentHasChildren<Child>;
> ...
> }

So, I'm wondering whether I just missed something or, if not, which way
around it should be.

Thanks,

David J. Pearce

--
Lecturer in Computer Science,
School of Mathematics, Statistics and Computer Science,
Victoria University of Wellington,
PO Box 600,
Wellington,
New Zealand.

Office: Cotton 231
Telephone: +64 4 463 5833
URL: http://www.mcs.vuw.ac.nz/~djp
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users








--
Lecturer in Computer Science,
School of Mathematics, Statistics and Computer Science,
Victoria University of Wellington,
PO Box 600,
Wellington,
New Zealand.

Office: Cotton 231
Telephone: +64 4 463 5833
URL: http://www.mcs.vuw.ac.nz/~djp


Back to the top