comparison src/luan/lib/JavaLib.java @ 64:177cfdc2bdb3

add type assertions git-svn-id: https://luan-java.googlecode.com/svn/trunk@65 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 17 Jan 2013 19:29:58 +0000
parents ebe578282363
children 5a93129995e1
comparison
equal deleted inserted replaced
63:ebe578282363 64:177cfdc2bdb3
82 fns.add(new LuanJavaFunction(constructor,null)); 82 fns.add(new LuanJavaFunction(constructor,null));
83 } 83 }
84 return new AmbiguousJavaFunction(fns); 84 return new AmbiguousJavaFunction(fns);
85 } 85 }
86 } 86 }
87 } else if( "assert".equals(name) ) {
88 return new LuanJavaFunction(assertClass,new AssertClass(cls));
87 } else { 89 } else {
88 List<Member> members = getStaticMembers(cls,name); 90 List<Member> members = getStaticMembers(cls,name);
89 if( !members.isEmpty() ) { 91 if( !members.isEmpty() ) {
90 return member(null,members); 92 return member(null,members);
91 } 93 }
336 private static final Method instanceOf; 338 private static final Method instanceOf;
337 static { 339 static {
338 try { 340 try {
339 instanceOf = InstanceOf.class.getMethod("instanceOf",Static.class); 341 instanceOf = InstanceOf.class.getMethod("instanceOf",Static.class);
340 instanceOf.setAccessible(true); 342 instanceOf.setAccessible(true);
343 } catch(NoSuchMethodException e) {
344 throw new RuntimeException(e);
345 }
346 }
347
348
349 private static class AssertClass {
350 private final Class cls;
351
352 AssertClass(Class cls) {
353 this.cls = cls;
354 }
355
356 public Object assertClass(LuanState luan,Object v) throws LuanException {
357 if( !cls.isInstance(v) ) {
358 String got = v.getClass().getSimpleName();
359 String expected = cls.getSimpleName();
360 throw new LuanException(luan,LuanElement.JAVA,"bad argument #1 ("+expected+" expected, got "+got+")");
361 }
362 return v;
363 }
364 }
365 private static final Method assertClass;
366 static {
367 try {
368 assertClass = AssertClass.class.getMethod("assertClass",LuanState.class,Object.class);
369 assertClass.setAccessible(true);
341 } catch(NoSuchMethodException e) { 370 } catch(NoSuchMethodException e) {
342 throw new RuntimeException(e); 371 throw new RuntimeException(e);
343 } 372 }
344 } 373 }
345 374