> I have two classes from two different packages:
>
[quoted text clipped - 13 lines]
> doing a complete "stack walk" from myMethod2() (so, only one stack
> inspection up should be sufficient)?
If you have control of the source of Class1, not just Class2, you can
use serialization on a private object. Note: myMth2 accepts (recursive)
calls from any methods that it calls.
package pkg1;
import pkg2.Cls2;
public final class Cls1 {
private Object srlzr=new Object();
void myMth1(Cls2 whom) {
...
synchronized(srlzr) {
// Only pkg1.Cls1.myMth1 synchronizes on srlzr.
whom.myMth2(this, ...);
}
...
}
public void isItReallyYou() {
// Throws Exception if not synchronized on srlzr.
// If caller synchronized on srlzr, does nothing.
srlzr.notify();
}
}
--------------------
package pkg2;
import pkg1.Cls1;
public class Cls2 {
public void myMth2(Cls1 which, ...) {
// isItReallyYou throws an Exception unless myMth2
// was called from pkg1.Cls1.myMth1.
try {
which.isItReallyYou();
} catch (IllegalMonitorStateException m) {
throw SecurityException();
}
...
}
}
--Mike Amling