summaryrefslogtreecommitdiffstats
path: root/variablespeed/tests/src/com/android/ex/variablespeed/DynamicProxy.java
blob: 429f2cc0303c0c76062e28401c15eadf848c0d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ex.variablespeed;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Contains a utility method for adapting a given interface against a real implementation.
 * <p>
 * This class is thead-safe.
 */
public class DynamicProxy {
    /**
     * Dynamically adapts a given interface against a delegate object.
     * <p>
     * For the given {@code clazz} object, which should be an interface, we return a new dynamic
     * proxy object implementing that interface, which will forward all method calls made on the
     * interface onto the delegate object.
     * <p>
     * In practice this means that you can make it appear as though {@code delegate} implements the
     * {@code clazz} interface, without this in practice being the case. As an example, if you
     * create an interface representing the {@link android.media.MediaPlayer}, you could pass this
     * interface in as the first argument, and a real {@link android.media.MediaPlayer} in as the
     * second argument, and now calls to the interface will be automatically sent on to the real
     * media player. The reason you may be interested in doing this in the first place is that this
     * allows you to test classes that have dependencies that are final or cannot be easily mocked.
     */
    // This is safe, because we know that proxy instance implements the interface.
    @SuppressWarnings("unchecked")
    public static <T> T dynamicProxy(Class<T> clazz, final Object delegate) {
        InvocationHandler invoke = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                try {
                    return delegate.getClass()
                            .getMethod(method.getName(), method.getParameterTypes())
                            .invoke(delegate, args);
                } catch (InvocationTargetException e) {
                    throw e.getCause();
                }
            }
        };
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[] { clazz }, invoke);
    }
}