Skip to content

Instantly share code, notes, and snippets.

@takawitter
Last active December 19, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawitter/5985742 to your computer and use it in GitHub Desktop.
Save takawitter/5985742 to your computer and use it in GitHub Desktop.
kawaでJavaオブジェクトのメソッドを呼び出す方法は複数あるけど、匿名クラス(やpublicではないクラス)の場合挙動が変わってくる。そのまとめ。kawaはのバージョンは1.13。
import kawa.standard.Scheme;
public class InvokeJavaFromScheme {
public static void main(String[] args) throws Throwable{
Scheme s = Scheme.getInstance();
// objにRunnableを実装した匿名クラスのインスタンスを設定。
// runメソッドはRunnableインタフェースのメソッドだから、問題なく呼べるはず。
s.define("obj", new Runnable(){
public void run() {
System.out.println("hello");
};
});
// s.eval("(invoke obj 'run)"); // だめ。IllegalAccessExceptionが投げられる。
// s.eval("(obj:run)"); // だめ。IllegalAccessExceptionが投げられる。
s.eval("(java.lang.Runnable:run obj)"); // 問題なく呼べる。
s.eval("(*:run (as java.lang.Runnable obj))"); // 問題なく呼べる。
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment