I think you're right, it could be pretty hairy. We could traverse the
call hierarchy as I've mentioned in a reply to Karan's mail.
Incidentally, the false-positive case you mention with the if statement
- would it be a problem adding the @DependsOn annotation in this case?
Although that code would never get executed, technically we are
referring to the other singleton.
I'll have a play about with some JDT code over the next few days and try
and get a better idea of what's involved.
Jon
Dain Sundstrom wrote:
It's not that hard to write some ASM code to detect that simple kind
of usage, but more complex usage would be difficult to impossible.
For example:
@PostConstruct
public void startup() {
initBlue();
}
private void initBlue() {
blue.doSomeWork();
}
Now imagine how complex you can make that code and you quickly come to
the conclusion that it is almost impossible to detect reliably. Of
course if you are only interested somewhat accurate results, you could
use a simple detect algorithm, but you will miss some usages and get
some false positives (for example if (false) blue.doSomeWork()).
-dain
On Aug 7, 2008, at 12:57 PM, David Blevins wrote:
Along those lines what would be really cool is if the tool could add
the @DependsOn automatically for a user.
A singleton needs a @DependsOn declaration only when referring to
(invoking) another singleton in either its @PostConstruct or
@PreDestroy method. I don't know if it's possible, but if there was
some way spot that and add the @DependsOn for them, that'd be really
cool.
As an example, the bean has this:
@Singleton
@Startup
public class RedBean {
@EJB(beanName="BlueBean")
private BlueLocal blue
@PostConstruct
public void startup() {
}
}
and all is fine, but when they add this to the @PostConstruct method...
@PostConstruct
public void startup() {
blue.doSomeWork();
}
At that point you need to add the @DependsOn like so:
@Singleton
@Startup
@DependsOn("BlueBean")
public class RedBean {
...
}
Don't know if that's possible, but would be cool.
-David