Last active
December 19, 2015 16:39
-
-
Save takawitter/5985742 to your computer and use it in GitHub Desktop.
kawaでJavaオブジェクトのメソッドを呼び出す方法は複数あるけど、匿名クラス(やpublicではないクラス)の場合挙動が変わってくる。そのまとめ。kawaはのバージョンは1.13。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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