OverrideMethod.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.tools.layoutlib.create;
18
19import java.util.HashMap;
20
21/**
22 * Allows stub methods from LayoutLib to be overriden at runtime.
23 */
24public final class OverrideMethod {
25
26    /**
27     * Interface to allow a method invocation to be listend upon.
28     */
29    public interface MethodListener {
30        /**
31         * A stub method is being invoked.
32         * <p/>
33         * Known limitation: caller arguments are not available. Return value cannot be set.
34         *
35         * @param signature The signature of the method being invoked, composed of the
36         *                  binary class name followed by the method descriptor (aka argument
37         *                  types). Example: "com/foo/MyClass/InnerClass/printInt(I)V".
38         * @param isNative True if the method was a native method.
39         * @param caller The calling object. Null for static methods, "this" for instance methods.
40         */
41        public void onInvoke(String signature, boolean isNative, Object caller);
42    }
43
44    /** Map of method overridden. */
45    private static HashMap<String, MethodListener> sMethods = new HashMap<String, MethodListener>();
46    /** Default listener for all method not listed in sMethods. Nothing if null. */
47    private static MethodListener sDefaultListener = null;
48
49    /**
50     * Sets the default listener for all methods not specifically handled.
51     * Null means to do nothing.
52     */
53    public static void setDefaultListener(MethodListener listener) {
54        sDefaultListener = listener;
55    }
56
57    /**
58     * Defines or reset a listener for the given method signature.
59     *
60     * @param signature The signature of the method being invoked, composed of the
61     *                  binary class name followed by the method descriptor (aka argument
62     *                  types). Example: "com/foo/MyClass/InnerClass/printInt(I)V"
63     * @param listener The new listener. Removes it if null.
64     */
65    public static void setMethodListener(String signature, MethodListener listener) {
66        if (listener == null) {
67            sMethods.remove(signature);
68        } else {
69            sMethods.put(signature, listener);
70        }
71    }
72
73    /**
74     * Invoke the specific listener for the given signature or the default one if defined.
75     * <p/>
76     * Note: this is not intended to be used by the LayoutLib Bridge. It is intended to be called
77     * by the stubbed methods generated by the LayoutLib_create tool.
78     *
79     * @param signature The signature of the method being invoked, composed of the
80     *                  binary class name followed by the method descriptor (aka argument
81     *                  types). Example: "com/foo/MyClass/InnerClass/printInt(I)V".
82     * @param isNative True if the method was a native method.
83     * @param caller The calling object. Null for static methods, "this" for instance methods.
84     */
85    public static void invoke(String signature, boolean isNative, Object caller) {
86        MethodListener i = sMethods.get(signature);
87        if (i != null) {
88            i.onInvoke(signature, isNative, caller);
89        } else if (sDefaultListener != null) {
90            sDefaultListener.onInvoke(signature, isNative, caller);
91        }
92    }
93}
94