1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html 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 17 import org.as2lib.core.BasicClass; 18 import org.as2lib.env.reflect.ProxyFactory; 19 import org.as2lib.env.reflect.InvocationHandler; 20 21 /** 22 * {@code InterfaceProxyFacotry} creates proxies for interfaces. It can only be 23 * used in conjunction with interfaces, not classes. 24 * 25 * <p>It offers a higher performance than the {@code TypeProxyFactory} which can 26 * also be used with classes. 27 * 28 * @author Simon Wacker 29 * @see org.as2lib.env.reflect.TypeProxyFactory 30 */ 31 class org.as2lib.env.reflect.InterfaceProxyFactory extends BasicClass implements ProxyFactory { 32 33 /** 34 * Creates proxies for interfaces. 35 * 36 * <p>You can cast the returned proxy to the passed-in {@code interfaze}. 37 * 38 * <p>{@code null} will be returned if the passed-in {@code interfaze} is {@code null} 39 * or {@code undefined}. 40 * 41 * <p>The returned proxy catches method invocations by using {@code __resolve}. 42 * 43 * <p>Note that also methods that are not declared on the passed-in {@code interfaze} 44 * but that are invoked on the returned proxy, get forwarded to the passed-in 45 * {@code handler}. 46 * 47 * @param interfaze the interface to create the proxy for 48 * @param handler the handler to invoke on method invocations on the returned proxy 49 * @return the created interface proxy 50 */ 51 public function createProxy(interfaze:Function, handler:InvocationHandler) { 52 if (!interfaze) return null; 53 var result:Object = new Object(); 54 result.__proto__ = interfaze.prototype; 55 result.__constructor__ = interfaze; 56 result.__resolve = function(methodName:String):Function { 57 return (function() { 58 return handler.invoke(this, methodName, arguments); 59 }); 60 }; 61 return result; 62 } 63 64 }