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?

Hi Martin,
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.

For example :

public aspect InjectInt {

  public interface WithInt {
  }

  // a Field
  public int WithInt.intField = 1;
 
  // a Method
  public void WithInt.doSomething() {
     System.out.println(intfield);
  }

  // Inject the interface to all classes in the "domain" package
  declare parents : com.mycompany.domain.* implements WithInt;

}

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 "com.mycompany.domain.*" you can place any "Type Pattern", so you can inject the interface on all classes annotated with an annotation, every class that extends another class or implements one or more interfaces, and any other combination you can think of.

Simone

2010/4/22 Martin Schafföner <the.shapht@xxxxxxxxxxxxxx>
This time it's not a compiler bug, but a plain old user question.

I'm currently investigating how useful AspectJ is in converting/extending a
legacy code basis. I'm trying to roll out new logging mechanisms with
minimal code changes, especially to avoid boiler-plate code.

What I want to do is to inject static members into a multitude of classes
selected by a type pattern. Basically I want to have a marker annotation
@MakeLoggable, and all types on which this annotation is declared should get
a private static member. I dream of something like this:

public @interface MakeLoggable {}

Public aspect AutoLogging {
 Private static Logger (@MakeLoggable *).LOGGER /* = init code */;
}

This doesn't compile, it seems I can only declare the LOGGER member on one
type exactly, not on several at once. And I cannot find from the
documentation how I can express my wish in valid AspectJ code :-)

Is it possible at all, and if so, how?

Martin




_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top