Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Introduce static members into several classes at once?

> you can declare fields on multiple classes at once. You need to declare
> an interface, then declare the field/methods/whatever on that
> interface, then inject the interface on the target types.

That's what I tried and what worked:

public @interface MakeLoggable {}

public interface Loggable{
  Logger getLogger();
}

public aspect AutoLogging {
 declare parents: (@MakeLoggable *) implements Loggable;

 public Logger Loggable.mLogger;

 public Logger Loggable.getLogger()
{
  return mLogger; // okay, lazy initialization here
}
}

> It should work with static fields as well, but I've never used static
> ITDs so try playing with it a bit. Obviously, where i wrote

However, if I switch the mLogger injection to 

public static Logger Loggable.mLogger;

I get an error that I cannot introduce a static member into an interface.

So, can I get this done at all? If so, how?

Martin




Back to the top