12637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin/*
22637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin * Copyright (c) 2016 Mockito contributors
32637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin * This program is made available under the terms of the MIT License.
42637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin */
52637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinpackage org.mockito.internal.creation.bytebuddy;
62637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
72637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.lang.reflect.Method;
82637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.util.concurrent.Callable;
92637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.util.concurrent.ConcurrentHashMap;
102637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinimport java.util.concurrent.ConcurrentMap;
112637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
122637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffinpublic abstract class MockMethodDispatcher {
132637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
142637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    private static final ConcurrentMap<String, MockMethodDispatcher> INSTANCE = new ConcurrentHashMap<String, MockMethodDispatcher>();
152637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
162637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public static MockMethodDispatcher get(String identifier, Object mock) {
172637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        if (mock == INSTANCE) { // Avoid endless loop if ConcurrentHashMap was redefined to check for being a mock.
182637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            return null;
192637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        } else {
202637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin            return INSTANCE.get(identifier);
212637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        }
222637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    }
232637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
242637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public static void set(String identifier, MockMethodDispatcher dispatcher) {
252637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin        INSTANCE.putIfAbsent(identifier, dispatcher);
262637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    }
272637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
282637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public abstract Callable<?> handle(Object instance, Method origin, Object[] arguments) throws Throwable;
292637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
302637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public abstract boolean isMock(Object instance);
312637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
322637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public abstract boolean isMocked(Object instance);
332637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin
342637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin    public abstract boolean isOverridden(Object instance, Method origin);
352637d96c202372854a7c71466ddcc6e90fc4fc53Paul Duffin}
36