annotate src/luan/lib/JavaLib.java @ 63:ebe578282363

minor fix git-svn-id: https://luan-java.googlecode.com/svn/trunk@64 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 09 Jan 2013 22:54:50 +0000
parents 7c05f53ef6cb
children 177cfdc2bdb3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
1 package luan.lib;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
2
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3 import java.lang.reflect.Array;
50
272b6f1d7dac fix java accessibility
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
4 import java.lang.reflect.AccessibleObject;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
5 import java.lang.reflect.Member;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
6 import java.lang.reflect.Field;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
7 import java.lang.reflect.Method;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
8 import java.lang.reflect.Constructor;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
9 import java.lang.reflect.Modifier;
61
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
10 import java.lang.reflect.InvocationHandler;
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
11 import java.lang.reflect.Proxy;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
12 import java.util.Map;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
13 import java.util.HashMap;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
14 import java.util.List;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
15 import java.util.ArrayList;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
16 import java.util.Iterator;
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 51
diff changeset
17 import java.util.Collections;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
18 import luan.Luan;
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
19 import luan.LuanState;
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
20 import luan.LuanTable;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
21 import luan.MetatableGetter;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
22 import luan.LuanException;
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
23 import luan.LuanFunction;
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
24 import luan.LuanJavaFunction;
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
25 import luan.LuanElement;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
26
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
27
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
28 public final class JavaLib {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
29
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
30 public static void register(LuanState luan) {
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
31 luan.addMetatableGetter(mg);
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
32 LuanTable module = new LuanTable();
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
33 LuanTable global = luan.global();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
34 global.put("java",module);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
35 try {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
36 global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) );
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
37 module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) );
62
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
38 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class );
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
39 } catch(NoSuchMethodException e) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
40 throw new RuntimeException(e);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
41 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
42 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
43
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
44 private static final LuanTable mt = new LuanTable();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
45 static {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
46 add( mt, "__index", LuanState.class, Object.class, Object.class );
60
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
47 add( mt, "__newindex", LuanState.class, Object.class, Object.class, Object.class );
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
48 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
49
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
50 private static void add(LuanTable t,String method,Class<?>... parameterTypes) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
51 try {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
52 t.put( method, new LuanJavaFunction(JavaLib.class.getMethod(method,parameterTypes),null) );
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
53 } catch(NoSuchMethodException e) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
54 throw new RuntimeException(e);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
55 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
56 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
57
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
58 private static final MetatableGetter mg = new MetatableGetter() {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
59 public LuanTable getMetatable(Object obj) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
60 if( obj==null )
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
61 return null;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
62 return mt;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
63 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
64 };
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
65
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
66 public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
67 if( obj instanceof Static ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
68 if( key instanceof String ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
69 String name = (String)key;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
70 Static st = (Static)obj;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
71 Class cls = st.cls;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
72 if( "class".equals(name) ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
73 return cls;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
74 } else if( "new".equals(name) ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
75 Constructor<?>[] constructors = cls.getConstructors();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
76 if( constructors.length > 0 ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
77 if( constructors.length==1 ) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
78 return new LuanJavaFunction(constructors[0],null);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
79 } else {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
80 List<LuanJavaFunction> fns = new ArrayList<LuanJavaFunction>();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
81 for( Constructor constructor : constructors ) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
82 fns.add(new LuanJavaFunction(constructor,null));
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
83 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
84 return new AmbiguousJavaFunction(fns);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
85 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
86 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
87 } else {
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
88 List<Member> members = getStaticMembers(cls,name);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
89 if( !members.isEmpty() ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
90 return member(null,members);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
91 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
92 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
93 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
94 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for: "+obj);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
95 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
96 Class cls = obj.getClass();
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
97 if( cls.isArray() ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
98 if( "length".equals(key) ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
99 return Array.getLength(obj);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
100 }
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
101 Integer i = Luan.asInteger(key);
46
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
102 if( i != null ) {
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
103 return Array.get(obj,i);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
104 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
105 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java array: "+obj);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
106 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
107 if( key instanceof String ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
108 String name = (String)key;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
109 if( "instanceof".equals(name) ) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
110 return new LuanJavaFunction(instanceOf,new InstanceOf(obj));
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
111 } else {
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
112 List<Member> members = getMembers(cls,name);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
113 if( !members.isEmpty() ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
114 return member(obj,members);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
115 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
116 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
117 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
118 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java object: "+obj);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
119 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
120
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
121 private static Object member(Object obj,List<Member> members) throws LuanException {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
122 try {
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
123 for( Member m : members ) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
124 if( m instanceof AccessibleObject )
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
125 ((AccessibleObject)m).setAccessible(true);
50
272b6f1d7dac fix java accessibility
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 49
diff changeset
126 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
127 if( members.size()==1 ) {
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
128 Member member = members.get(0);
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
129 if( member instanceof Static ) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
130 return member;
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
131 } else if( member instanceof Field ) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
132 Field field = (Field)member;
46
a443637829c1 remove LuaNumber
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 44
diff changeset
133 return field.get(obj);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
134 } else {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
135 Method method = (Method)member;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
136 return new LuanJavaFunction(method,obj);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
137 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
138 } else {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
139 List<LuanJavaFunction> fns = new ArrayList<LuanJavaFunction>();
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
140 for( Member member : members ) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
141 Method method = (Method)member;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
142 fns.add(new LuanJavaFunction(method,obj));
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
143 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
144 return new AmbiguousJavaFunction(fns);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
145 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
146 } catch(IllegalAccessException e) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
147 throw new RuntimeException(e);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
148 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
149 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
150
60
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
151 public static void __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
152 if( obj instanceof Static ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
153 if( key instanceof String ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
154 String name = (String)key;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
155 Static st = (Static)obj;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
156 Class cls = st.cls;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
157 List<Member> members = getStaticMembers(cls,name);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
158 if( !members.isEmpty() ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
159 if( members.size() != 1 )
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
160 throw new RuntimeException("not field '"+name+"' of "+obj);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
161 setMember(obj,members,value);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
162 return;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
163 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
164 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
165 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for: "+obj);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
166 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
167 Class cls = obj.getClass();
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
168 if( cls.isArray() ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
169 Integer i = Luan.asInteger(key);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
170 if( i != null ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
171 Array.set(obj,i,value);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
172 return;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
173 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
174 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java array: "+obj);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
175 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
176 if( key instanceof String ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
177 String name = (String)key;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
178 List<Member> members = getMembers(cls,name);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
179 if( !members.isEmpty() ) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
180 if( members.size() != 1 )
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
181 throw new RuntimeException("not field '"+name+"' of "+obj);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
182 setMember(obj,members,value);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
183 return;
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
184 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
185 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
186 throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java object: "+obj);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
187 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
188
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
189 private static void setMember(Object obj,List<Member> members,Object value) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
190 Field field = (Field)members.get(0);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
191 try {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
192 field.set(obj,value);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
193 } catch(IllegalAccessException e) {
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
194 throw new RuntimeException(e);
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
195 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
196 }
a68ccb7aaa9c implement setting java field values
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 58
diff changeset
197
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
198 private static Map<Class,Map<String,List<Member>>> memberMap = new HashMap<Class,Map<String,List<Member>>>();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
199
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
200 private static synchronized List<Member> getMembers(Class cls,String name) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
201 Map<String,List<Member>> clsMap = memberMap.get(cls);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
202 if( clsMap == null ) {
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
203 clsMap = new HashMap<String,List<Member>>();
58
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
204 for( Method method : cls.getMethods() ) {
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
205 String s = method.getName();
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
206 List<Member> list = clsMap.get(s);
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
207 if( list == null ) {
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
208 list = new ArrayList<Member>();
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
209 clsMap.put(s,list);
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
210 }
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
211 list.add(method);
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
212 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
213 for( Class c : cls.getClasses() ) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
214 String s = c.getSimpleName();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
215 List<Member> list = clsMap.get(s);
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
216 if( list == null ) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
217 list = new ArrayList<Member>();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
218 clsMap.put(s,list);
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
219 }
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
220 list.add(new Static(c));
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
221 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
222 for( Field field : cls.getFields() ) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
223 String s = field.getName();
58
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
224 try {
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
225 if( !cls.getField(s).equals(field) )
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
226 continue; // not accessible
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
227 } catch(NoSuchFieldException e) {
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
228 throw new RuntimeException(e);
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
229 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
230 List<Member> list = clsMap.get(s);
58
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
231 if( list != null )
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
232 throw new RuntimeException("can't add field '"+s+"' to "+cls+" because these are already defined: "+list);
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
233 list = new ArrayList<Member>();
d6c454b7f58c make concat work with any type;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 56
diff changeset
234 clsMap.put(s,list);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
235 list.add(field);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
236 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
237 memberMap.put(cls,clsMap);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
238 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
239 List<Member> rtn = clsMap.get(name);
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 51
diff changeset
240 if( rtn==null )
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 51
diff changeset
241 rtn = Collections.emptyList();
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 51
diff changeset
242 return rtn;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
243 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
244
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
245 private static synchronized List<Member> getStaticMembers(Class cls,String name) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
246 List<Member> staticMembers = new ArrayList<Member>();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
247 for( Member m : getMembers(cls,name) ) {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
248 if( Modifier.isStatic(m.getModifiers()) )
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
249 staticMembers.add(m);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
250 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
251 return staticMembers;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
252 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
253
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
254 static final class Static implements Member {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
255 final Class cls;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
256
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
257 Static(Class cls) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
258 this.cls = cls;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
259 }
56
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
260
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
261 @Override public String toString() {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
262 return cls.toString();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
263 }
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
264
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
265 @Override public Class<?> getDeclaringClass() {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
266 return cls.getDeclaringClass();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
267 }
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
268
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
269 @Override public String getName() {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
270 return cls.getName();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
271 }
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
272
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
273 @Override public int getModifiers() {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
274 return cls.getModifiers();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
275 }
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
276
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
277 @Override public boolean isSynthetic() {
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
278 return cls.isSynthetic();
c84274b18f0c various
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 55
diff changeset
279 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
280 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
281
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
282 public static Static getClass(LuanState luan,String name) throws LuanException {
51
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
283 Class cls;
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
284 try {
51
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
285 cls = Class.forName(name);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
286 } catch(ClassNotFoundException e) {
51
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
287 try {
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
288 cls = Thread.currentThread().getContextClassLoader().loadClass(name);
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
289 } catch(ClassNotFoundException e2) {
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
290 throw new LuanException(luan,LuanElement.JAVA,e);
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
291 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
292 }
51
28dfd91a816c various small issues
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 50
diff changeset
293 return new Static(cls);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
294 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
295
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
296 public static void importClass(LuanState luan,String name) throws LuanException {
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
297 luan.global().put( name.substring(name.lastIndexOf('.')+1), getClass(luan,name) );
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
298 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
299
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
300 static class AmbiguousJavaFunction extends LuanFunction {
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
301 private final Map<Integer,List<LuanJavaFunction>> fnMap = new HashMap<Integer,List<LuanJavaFunction>>();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
302
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
303 AmbiguousJavaFunction(List<LuanJavaFunction> fns) {
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
304 for( LuanJavaFunction fn : fns ) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
305 Integer n = fn.getParameterTypes().length;
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
306 List<LuanJavaFunction> list = fnMap.get(n);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
307 if( list==null ) {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
308 list = new ArrayList<LuanJavaFunction>();
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
309 fnMap.put(n,list);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
310 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
311 list.add(fn);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
312 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
313 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
314
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
315 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
48
64ecb7a3aad7 rename Lua to Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 46
diff changeset
316 for( LuanJavaFunction fn : fnMap.get(args.length) ) {
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
317 try {
55
9381b23ea9e1 various fixes
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 51
diff changeset
318 return fn.rawCall(luan,args);
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
319 } catch(IllegalArgumentException e) {}
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
320 }
49
8ede219cd111 add WebShell
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 48
diff changeset
321 throw new LuanException(luan,LuanElement.JAVA,"no method matched args");
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
322 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
323 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
324
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
325 private static class InstanceOf {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
326 private final Object obj;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
327
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
328 InstanceOf(Object obj) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
329 this.obj = obj;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
330 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
331
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
332 public boolean instanceOf(Static st) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
333 return st.cls.isInstance(obj);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
334 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
335 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
336 private static final Method instanceOf;
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
337 static {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
338 try {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
339 instanceOf = InstanceOf.class.getMethod("instanceOf",Static.class);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
340 instanceOf.setAccessible(true);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
341 } catch(NoSuchMethodException e) {
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
342 throw new RuntimeException(e);
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
343 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
344 }
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
345
61
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
346
62
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
347 public static Object proxy(final LuanState luan,Static st,final LuanTable t,final Object base) throws LuanException {
61
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
348 return Proxy.newProxyInstance(
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
349 st.cls.getClassLoader(),
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
350 new Class[]{st.cls},
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
351 new InvocationHandler() {
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
352 public Object invoke(Object proxy,Method method, Object[] args)
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
353 throws Throwable
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
354 {
63
ebe578282363 minor fix
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 62
diff changeset
355 if( args==null )
ebe578282363 minor fix
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 62
diff changeset
356 args = new Object[0];
61
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
357 String name = method.getName();
62
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
358 Object fnObj = t.get(name);
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
359 if( fnObj==null && base!=null )
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
360 return method.invoke(base,args);
7c05f53ef6cb improve java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 61
diff changeset
361 LuanFunction fn = luan.checkFunction(LuanElement.JAVA,fnObj);
61
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
362 return Luan.first(luan.call(fn,LuanElement.JAVA,name,args));
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
363 }
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
364 }
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
365 );
183d470be7e8 add java.proxy
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 60
diff changeset
366 }
37
8a57ebfdfd78 add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
367 }