Re: How to let the methods of a class only can be invoked by a special package(the class not in the package)?
Jebel.Star@gmail.com writes:
How can I make the A.GetInstance() only can be invoked by the B
Any static measure is insufficient, because new classes
can be generated and loaded at run-time. Such classes would
not have been inspected by a static measure.
Therefore, the measure must be dynamic. The answers I am aware
of have implied that ?A.GetInstance()? will somehow check its
caller. Such answers do not actually address how to /prevent/
the invocation, because at the time when ?A.GetInstance()?
checks its caller it already /has been/ invoked, so it is too
late to prevent it. The question was to prevent this, not to
detect and report it, once it has happend.
Therefore, it might not be possible to do this at all.
One could use a patched JVM to do this.
However, there might be multiple ?B? classes (one for each
class loader). One also needs to specify which of them should
be allowed to call ?A.GetInstance()?. For this, one needs
to identify the class loaders involved.