Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Inter-Type Declaration Help Needed

A variant that I just wrote (does use the hasmethod feature...)

--- A.java ----- 8< -----
interface A {public void ma();}

interface B {public void mb();}

// Implementor of B
class C implements B {
  public void mb() {}
}

aspect X {
  // Give implementors of A a default implementation of the method
  public void A.ma() {}

  // For any type that is not B and has a method defined that was
declared in B, make it implement A
  declare parents: !B && hasmethod(* B.*(..)) implements A;
}
-----8<------

$ ajc -1.5 -XhasMember -showWeaveInfo A.java
Extending interface set for type 'C' (A.java) to include 'A' (A.java)

Type 'C' (A.java) has intertyped method from 'X' (A.java:'void A.ma()')

Type 'A' (A.java) has intertyped method from 'X' (A.java:'void A.ma()')


On 12/12/2007, Bhaskar Maddala <maddalab@xxxxxxxxx> wrote:
> My attempt at trying to implement what I understand from your
> question, note though I am a newbie
>
> public interface Foo {
>     public void doFoo();
> }
>
> public interface Bar {
>     public void doBar();
> }
>
> public class FooImpl implements Foo {
>     public void doFoo() {
>         System.out.println("you got fooed");
>     }
> }
>
> public class BarImpl implements Bar {
>     public void doBar() {
>         System.out.println("you got barred");
>     }
> }
>
> import foo.Foo;
> import bar.Bar;
> import bar.BarImpl;
>
> public aspect FooBar {
>     declare parents : Foo extends Bar;
>
>     public void Foo.doBar() {
>        Bar bar = new BarImpl();
>        bar.doBar();
>     }
> }
>
> import bar.Bar;
> import foo.Foo;
> import foo.FooImpl;
>
> public class Main {
>     public static void main(String[] args) {
>         Foo foo = new FooImpl();
>         foo.doFoo();
>         ((Bar)foo).doBar();
>     }
> }
>
> Result of running Main
>
> you got fooed
> you got barred
>
> Hope that helps
>
> Bhaskar
>
>
>
>
> On Dec 12, 2007 11:40 AM, Kevin Shale <Shale@xxxxxx> wrote:
> > Hi, I am a relative newbie to AspectJ and I have a problem to solve.
> >
> > In Java I have two interfaces A and B which are not related to each other via extends.
> >
> > Each interface has a number of concrete implementation classes.
> >
> > I am looking for a way to use AspectJ to force every class which implements interface B to also implement interface A with methods defined in the aspect.
> >
> > How would I go about programming this?
> >
> > Best Regards
> > Kevin
> > _______________________________________________________________________
> > Jetzt neu! Schützen Sie Ihren PC mit McAfee und WEB.DE. 3 Monate
> > kostenlos testen. http://www.pc-sicherheit.web.de/startseite/?mc=022220
> >
> > _______________________________________________
> > 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