人気ブログランキング | 話題のタグを見る
Studio Jupiter - [blog]
http://botema.exblog.jp
/ Studio Jupiter / my blog / Instagram NEW /

PDE JUnit ではprotectedメソッドをテストできない
 
Eclipseプラグインのクラスローダーは、異なるプラグインからprotectedメンバへのアクセスを許さないようです。
したがって、プラグインプロジェクトとテストプロジェクトを別にしていると、protectedメソッドテストができません。

someplugin
 └foo.bar.SomeThing.java
someplugin-test
 └foo.bar.SomeThingTest.java

SomeThing に
 protected void protectedMethod() みたいなメソッドがあるとき、

SomeThingTest で
 public void testProtectedMethod(){
  SomeThing st = new SomeThing();
  st.protectedMethod(); // ここでIllegalAccessError が発生します。
 }

PDE JUnit ではなく単体のJUnitを使う場合は、パッケージが同じなのでprotectedでもアクセスできるんですが、プラグインとしてEclipseランタイム上で動作する場合は、アクセスできません。

リフレクションを使ったらアクセスできました。
 public void testProtectedMethod(){
  Method method =
   SomeThing.class.getDeclaredMethod("protectedMethod", null);
  SomeThing st = new SomeThing();
  method.invoke(st, null);
 }
by botema | 2005-07-29 10:28 | Java プログラミング
<< ブラウザのアドレス欄でJava...   アプリケーションが読み書きする... >>