I wonder if it's possible to limit a method to be called by only one class in java.
public interface IAuditingEventHandler {
public void handleEvent(BaseEventType event);
}
public class EDAEventHandler implements IAuditingEventHandler {
//should be callabe only from MultiInstanceAuditingEventHandler
@Override
public void handleEvent(BaseEventType event)...
}
public class DBEventHandler implements IAuditingEventHandler {
//should be callabe only from MultiInstanceAuditingEventHandler
@Override
public void handleEvent(BaseEventType event)...
}
public class MultiInstanceAuditingEventHandler implements IAuditingEventHandler {
private final List<IAuditingEventHandler> eventHandlers;
public MultiInstanceAuditingEventHandler(List<IAuditingEventHandler> eventHandlers) {
this.eventHandlers = eventHandlers;
}
// can be called from everywhere
@Override
public void handleEvent(BaseEventType event) {
for (IAuditingEventHandler eventHandler : eventHandlers) {
eventHandler.handleEvent(event);
}
}
}
As you can see I have two classes with the method handleEvent that should only be called from one specific class MultiInstanceAuditingEventHandler. The rest of the code should be able to call only that one specific class MultiInstanceAuditingEventHandler.
The purpose of this is to have a proxy that knows which instances should be called and every user of that code has to call that proxy.
Is there a way to achieve that in Java? Maybe some pattern or some inheritance magic?
Thanks, Sven
Update
Seeing that there is not a good enough solution programming wise, maybe there are some test tools that do code analysis and can make sure that none of the classes methods are called?