OverrideMethod.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 caller The calling object. Null for static methods, "this" for instance methods. 39 */ 40 public void onInvoke(String signature, Object caller); 41 } 42 43 /** Map of method overriden. */ 44 private static HashMap<String, MethodListener> sMethods = new HashMap<String, MethodListener>(); 45 /** Default listener for all method not listed in sMethods. Nothing if null. */ 46 private static MethodListener sDefaultListener = null; 47 48 /** 49 * Sets the default listener for all methods not specifically handled. 50 * Null means to do nothing. 51 */ 52 public static void setDefaultListener(MethodListener listener) { 53 sDefaultListener = listener; 54 } 55 56 /** 57 * Defines or reset a listener for the given method signature. 58 * 59 * @param signature The signature of the method being invoked, composed of the 60 * binary class name followed by the method descriptor (aka argument 61 * types). Example: "com/foo/MyClass/InnerClass/printInt(I)V" 62 * @param listener The new listener. Removes it if null. 63 */ 64 public static void setMethodListener(String signature, MethodListener listener) { 65 if (listener == null) { 66 sMethods.remove(signature); 67 } else { 68 sMethods.put(signature, listener); 69 } 70 } 71 72 /** 73 * Invoke the specific listener for the given signature or the default one if defined. 74 * <p/> 75 * Note: this is not intended to be used by the LayoutLib Bridge. It is intended to be called 76 * by the stubbed methods generated by the LayoutLib_create tool. 77 * 78 * @param signature The signature of the method being invoked, composed of the 79 * binary class name followed by the method descriptor (aka argument 80 * types). Example: "com/foo/MyClass/InnerClass/printInt(I)V" 81 * @param caller The calling object. Null for static methods, "this" for instance methods. 82 */ 83 public static void invoke(String signature, Object caller) { 84 MethodListener i = sMethods.get(signature); 85 if (i != null) { 86 i.onInvoke(signature, caller); 87 } else if (sDefaultListener != null) { 88 sDefaultListener.onInvoke(signature, caller); 89 } 90 } 91} 92