Back to Top

Wednesday, December 17, 2008

Curious Eclipse (Java?) bug...

It seems that watchpoints are not triggered if the field is changed using reflection. A simple test program to demonstrate this:

import java.lang.reflect.*;

public class ReflectMe {
 public static int foo = 1;             //*1
 
 public void test() throws Exception {
  foo = 2;                              //*2
  Class c = Class.forName("ReflectMe");
  Field f = c.getField("foo");
  f.setInt(this, 10);                   //*3
  System.out.println(foo);              //*4
 }

 public static void main(String[] args) throws Exception {
  ReflectMe r = new ReflectMe();
  r.test();
 }

}

One would expect that a watchpoint set on foo would be triggered on the four different marked lines. However I found that it isn't triggered on line #3. At this point it is unclear to me if this is a limitation of the JVM or Eclipse and I didn't find any relevant information, so I submitted a bugreport and hope that people smarter than me can figure this one out :-).

Update: just as I suspected, the problem is that the JVM has this behavior. Thank you for the great guys and gals working on Eclipse for such a speedy response.

PS. The code where I ran into this behavior was a configuration loader which used reflection to set the values read from the configuration file (ie. you specified something com.foo.Bar.exampleVariable = 10 and it used reflection to traverse the class hierarchy and assign the value). While very cool and has the advantage of settings "just working" with zero code for new fields, it also has some disadvantages (for example: are you sure that you want to expose your variable structures? using this method they effectively become part of your public interface) to which now I can add the problem that assignment is not observable via watchpoints (which is not a big problem if you do know what is going on - but it you don't, it can seem quite magical - and dismaying).

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.