01: /*
02: * $Id: LuaInvocationHandler.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
03: * Copyright (C) 2003-2007 Kepler Project.
04: *
05: * Permission is hereby granted, free of charge, to any person obtaining
06: * a copy of this software and associated documentation files (the
07: * "Software"), to deal in the Software without restriction, including
08: * without limitation the rights to use, copy, modify, merge, publish,
09: * distribute, sublicense, and/or sell copies of the Software, and to
10: * permit persons to whom the Software is furnished to do so, subject to
11: * the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be
14: * included in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: */
24:
25: package org.keplerproject.luajava;
26:
27: import java.lang.reflect.InvocationHandler;
28: import java.lang.reflect.Method;
29:
30: /**
31: * Class that implements the InvocationHandler interface.
32: * This class is used in the LuaJava's proxy system.
33: * When a proxy object is accessed, the method invoked is
34: * called from Lua
35: * @author Rizzato
36: * @author Thiago Ponte
37: */
38: public class LuaInvocationHandler implements InvocationHandler {
39: private LuaObject obj;
40:
41: public LuaInvocationHandler(LuaObject obj) {
42: this .obj = obj;
43: }
44:
45: /**
46: * Function called when a proxy object function is invoked.
47: */
48: public Object invoke(Object proxy, Method method, Object[] args)
49: throws LuaException {
50: synchronized (obj.L) {
51: String methodName = method.getName();
52: LuaObject func = obj.getField(methodName);
53:
54: if (func.isNil()) {
55: return null;
56: }
57:
58: Class retType = method.getReturnType();
59: Object ret;
60:
61: // Checks if returned type is void. if it is returns null.
62: if (retType.equals(Void.class)
63: || retType.equals(void.class)) {
64: func.call(args, 0);
65: ret = null;
66: } else {
67: ret = func.call(args, 1)[0];
68: if (ret != null && ret instanceof Double) {
69: ret = LuaState.convertLuaNumber((Double) ret,
70: retType);
71: }
72: }
73:
74: return ret;
75: }
76: }
77: }
|