Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Question about interface inheritance and introduction

Thanks for your response, Andy.

On Fri, Aug 14, 2009 at 2:28 PM, Andy Clement<andrew.clement@xxxxxxxxx> wrote:
[snip]
> Once
>
>   declare parents: (@Barable *) implements HasBar;
>
> causes MyBarable to implement HasBar, the hierarchy for MyBarable includes
> HasFoo - so:
>
>   declare parents: (@Fooable *) implements HasFoo;
>
> doesn't match because HasFoo is already there (since HasBar extends
> HasFoo).

Got it.

[snip]
> ITD fields/methods on interfaces are put into the 'top most implementor' of
> the interface that is visible.  In the HasFoo>HasBar>MyBarable case then
> MyBarable is the top most implementor where they can be added.
>

> Your output was:
> [snip]
> what did you want it to do?
>
It behaved as expected, which was part of my confusion.

Here's the change that got me what I wanted:

public aspect HasFooIntro {
        // this is new
	private static interface InnerHasFoo extends HasFoo {}
	
	declare parents: (@Fooable *) implements InnerHasFoo;

	private boolean InnerHasFoo.returnNull = false;
	
	public Foo InnerHasFoo.getFoo() {
		Foo foo = returnNull ? null : new Foo();
		returnNull = !returnNull;
		return foo;
	}
}

public aspect HasBarIntro {
	// this is new
	private static interface InnerHasBar extends HasBar {}
	
	declare parents: (@Barable *) implements InnerHasBar;

	public Bar InnerHasBar.getBar() {
		return getFoo() == null ? new Bar() : null;
	}

// remove this method so that a @Barable is required to implement it
//	public Foo InnerHasBar.getFoo() {
//		return new Foo();
//	}
}

@Barable
public class MyBarable {
        // added due to interface requirement
	public Foo getFoo() {
		return new Foo();
	}
}

Now, if a class is @Barable, then it has to provide an implementation
of getFoo() as per the interface contract, because one isn't
introduced.

Thanks for your help!

-matthew


Back to the top