[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
|
[aspectj-users] Help with a Dependency Passing Aspect
|
- From: "Matt Kendall" <matt_kendall@xxxxxxxxx>
- Date: Thu, 17 May 2007 12:19:03 -0400
- Delivered-to: aspectj-users@eclipse.org
- Dkim-signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=RUu/c4R4V7TT9Gh/H0NHurL/ZW8xybpLovEKWL4r6nM9Y2hGkI58wdbvYtTJuWbUY3S3ffsp2s0rfcT6TAwN+bs/kfmFBvmkAUFdTw7SgzN1erGfnPpntgGgZzLI4I6qoIPu3yCaFj6ndywiQee0HxMg1nQpEBkeSKAfjZiaS/g=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=bxKVC4/oFA27rN2V+iB98okxLhWuk9nlLXLgC2D8HSGxj2RRhWCHkOy/fwBBmzj6u9EjZPLktA0QdC1hC8xgvpQgcLwR0dLA24KhLTSiGqB13Wr1GMMUU4qFGHQJdCkjw9FHMX2CqmHyHlIasvoDScfPrBsQfX/JxidVDTt66tA=
I've been trying to figure out this aspect for the past few hours.
Hopefully someone can help since I'm new to AOP. I'm working on a
project where I have several Foo objects that are the root of object
graphs. The members of each Foo graph need to access Foo on a regular
basis, but they need to access the Foo to which they belong (not a
global singleton). If I wanted to maintain a whole lot of extra code,
I'd write something like this to accomplish what I want:
class Foo {
public FooBar getSomeResource() {
return someResource;
}
public Bar anExample() {
Bar bar = BarFactory.createABar(this); // Creates and returns a Bar
// that
has a reference to this Foo.
return bar;
}
}
class Bar {
private Foo root;
public Bar(Foo root) {
this.root = root;
}
public FooBarFoo anotherExample() {
return FooBarFooFactory.createAFooBarFoo(root);
}
}
Hopefully that makes some amount of sense. My original tactic, which
worked quite well at first, was to use the following aspect:
public aspect FooInitializer {
pointcut withinFoo(Foo foo): within(Foo) && this(foo);
pointcut initialize(FooWorker bar, Foo foo):
initialization(FooWorker.new(..)) && target(bar) &&
cflow(withinFoo(foo));
before(FooWorker bar, Foo foo): initialize(bar, foo) {
bar.initialize(foo);
}
}
Basically, any class that needed an instance of Foo in order to do its
work just implemented the FooWorker interface and the dependency was
injected. Unfortunately, that aspect won't work if a member object of
Foo escapes its cflow. For example, using the code from above:
Foo foo = new Foo();
Bar bar = foo.anExample();
FooBarFoo fbf = foo.anotherExample(); // Will not be initialized
because it wasn't
// created in
the cflow of Foo.
So, what I'm looking for is a way to *pass* a dependency to any object
created by an object that knows about said dependency. Note that I
cannot just look for calls
to constructors directly, since the member object may be created in a
factory or the database layer.
Hopefully that makes some sort of sense. Thanks for reading!
Matt