[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Re: Accessing a protected class...
|
Matthew Webster wrote:
You can target an inner class with an aspect regardless of its visibilty as
long as you can name it. Anonymous inner classes are a problem. See the
example below:
public class Outer {
public void test () {
new Inner().test();
new StaticInner().test();
}
public static void main(String[] args) {
new Outer().test();
}
protected class Inner {
public void test() {
System.out.println("? " + getClass().getName() +
".test()");
}
}
protected static class StaticInner {
public void test() {
System.out.println("? " + getClass().getName() +
".test()");
}
}
}
public aspect Logging {
pointcut test () :
execution(* test(..))
&& (within(Outer.Inner) || within(Outer.StaticInner));
before () : test () {
System.err.println(thisJoinPoint);
}
}
Running Outer gives the following console output:
? Outer$Inner.test()
execution(void Outer.Inner.test())
? Outer$StaticInner.test()
execution(void Outer.StaticInner.test())
Notice Java reflection gives the "external" class name with a "$" while
AspectJ uses the internal name with a ".".
Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester, SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/
But anonymous inner classes aren't really a problem, because java
specifies no naming restriction, and their names really don't denote
anything. The only way I could want to identify instances of these
classes is by the interfaces they implement; one can easily do this:
public class Inners {
public static void main(String[] args) {
new Inners().go();
}
static void out(Object s) { System.out.println(s); }
void go() {
new Utterable() { public void speak() {out("moo");}
}.speak(); // cow
new Utterable() { public void speak() {out("baaaaaaah");}
}.speak(); // goat
}
interface Utterable { void speak(); }
static aspect Animals {
before(): execution(* *(..)) && this(Utterable) {
out(thisJoinPoint);
}
}
}
/* Outputs:
jdp@laptop ~/src/aspectj
$ ajc Inners.java; ajava Inners
execution(void Inners.1.speak())
moo
execution(void Inners.2.speak())
baaaaaaah
*/
Jeff
--
Jeffrey Palm --> http://www.ccs.neu.edu/home/jpalm