Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Dynamic inter-type declarations

Lets say we have some library (we cant modify its source code, or
instrument it). Part of this library is class:

public class LibClass {
  public  LibClass() {
  }
  protected void someProtectedMethod() {
  }
  public void somePublicMethod() {
  }
}

In some code that uses this library user of our library is either
instantiating class  LibClass directly by:

1)
LibClass lib = new  LibClass();

or subclassing it by :

2)
class SomeExtendedLibClass extends LibClass {
  public void somePublicMethod() {
	super.somePublicMethod();
	// additional code
  }
}

LibClass lib = new  SomeExtendedLibClass();

Now we want add some new functionality in code that is using this
library (precisely: we want to intercept invocations of all public and
protected method of LibClass and additionally change visibility of
protected method into public), but we can't manipulate  LibClass code
directly. Solution is to wrap all users code something like this:

1)
LibClass lib = new  LibClass() {
  public void someProtectedMethod() {
	super.someProtectedMethod();
	// additional code
  }
  public void somePublicMethod() {
	super.somePublicMethod();
	// additional code
  }
}

2)
class WrappedExtendedLibClass extends SomeExtendedLibClass {
  public void someProtectedMethod() {
	super.someProtectedMethod();
	// additional code
  }
  public void somePublicMethod() {
	super.somePublicMethod();
	// additional code
  }
}
LibClass lib = new  WrappedExtendedLibClass();

Question: is it possible to write such wrapping techniques in AspectJ
and how ? I've read about inter-type declarations in AspectJ, but as
far as I read it can't be used to extend user's class, because we
don't know names of this class (eg. SomeExtendedLibClass, but there
could be many other subclasses) in advance (during aspect code
writing). The solution for my problem might be intercepting all
creation of classes LibClass and its subclasses, and changing this
creation into creation of my own wrapping class (with correct parent
class).

Thanks !

Regards,
  Marcin Zduniak

Back to the top