0

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?

sveri
  • 1,372
  • 1
  • 13
  • 28
  • 7
    can't you put them in same package and give only package level access to handleEvent methods? – Adi Sep 09 '14 at 13:35
  • 1
    What would happen if you couldn't prevent this? You can check this at runtime, however in general it is not worth the extra complexity to do it. – Peter Lawrey Sep 09 '14 at 13:44
  • Package protection was the only solution that came to our mind. The disadvantage is that it's still possible to call the methods from within that package. However, one might create a single package for it. – sveri Sep 09 '14 at 14:08
  • What we want to do is to keep other coders in our team from making the mistake to only call a single implementation instead of every available implementation of that interface. – sveri Sep 09 '14 at 14:09

2 Answers2

0

If you have the your classes in the same package, you could remove the public modifier from your method (in interface and in implementation) to make it package-private. (See Controlling Access to Members of a Class)

Or, if you want to restrict the access to only from MultiInstanceAuditingEventHandler, you could read the stack trace at runtime, but you would have to do it in every implementing method individually. (Also this feels like cheating and I would discourage this option.)

kajacx
  • 12,361
  • 5
  • 43
  • 70
0

The "package private" option has been described by another Answer. (And you had apparently already considered it.

Another option would be to declare EDAEventHandler and DBEventHandler within the MultiInstanceAuditingEventHandler class, and declare them as private static.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216