It looks like your OO solution would work just fine, if you aren't
trying to affect instances of LibClass. That is, if you can
instantiate objects of your new subclasses everywhere a LibClass object
is required. Is that the case? (Note that dependency injection
solutions like Spring are a good way to instantiate what you want and
wire everything together.)
If you really have to affect LibClass instances, then you can use
around advice on the public methods to intercept them and add behavior.
However, this can't be used to change protected member access to
public. I don't know of a way to do that with AJ.
dean
Marcin Zduniak wrote:
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
--
Dean Wampler, Ph.D.
dean at aspectprogramming.com
http://www.aspectprogramming.com
http://www.contract4j.org
I want my tombstone to say:
Unknown Application Error in Dean Wampler.exe.
Application Terminated.
[Okay] [Cancel]
|