jaroslav@990: /** jaroslav@990: * Back 2 Browser Bytecode Translator jaroslav@990: * Copyright (C) 2012 Jaroslav Tulach jaroslav@990: * jaroslav@990: * This program is free software: you can redistribute it and/or modify jaroslav@990: * it under the terms of the GNU General Public License as published by jaroslav@990: * the Free Software Foundation, version 2 of the License. jaroslav@990: * jaroslav@990: * This program is distributed in the hope that it will be useful, jaroslav@990: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@990: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@990: * GNU General Public License for more details. jaroslav@990: * jaroslav@990: * You should have received a copy of the GNU General Public License jaroslav@990: * along with this program. Look for COPYING file in the top folder. jaroslav@990: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@990: */ jaroslav@990: package org.apidesign.bck2brwsr.htmlpage; jaroslav@990: jaroslav@990: import java.lang.reflect.Method; jaroslav@990: import java.util.logging.Level; jaroslav@990: import java.util.logging.Logger; jaroslav@990: jaroslav@990: /** Represents callback from Knockout.js to Java. jaroslav@990: * jaroslav@990: * @author Jaroslav Tulach jaroslav@990: */ jaroslav@990: public final class KOFunction { jaroslav@990: private static final Logger LOG = Logger.getLogger(KOFunction.class.getName()); jaroslav@990: private final Object model; jaroslav@990: private final Method method; jaroslav@990: jaroslav@990: KOFunction(Object model, String method) throws NoSuchMethodException { jaroslav@990: this.method = model.getClass().getDeclaredMethod(method, Object.class, Object.class); jaroslav@990: this.method.setAccessible(true); jaroslav@990: this.model = model; jaroslav@990: } jaroslav@990: jaroslav@990: public void call(Object data, Object ev) { jaroslav@990: try { jaroslav@990: this.method.invoke(model, data, ev); jaroslav@990: } catch (Throwable ex) { jaroslav@990: LOG.log(Level.SEVERE, "Can't call " + method + " on " + model, ex); jaroslav@990: } jaroslav@990: } jaroslav@990: }