Checking if code was altered
<p>I want to call a few costly update methods whenever my code changes. I hit ctrl-s in Eclipse, this triggers a file save and a hot code replacement, my program checks to see that the file was saved, spends about 5 seconds crunching numbers, and then updates the screen.</p>

<p>I'm using this thing, which I call a few times per second:</p>

<pre><code>public static long lastSourceUpdate=0;
private static boolean wasUpdated() {
    File source = new File("/home/user/workspace/package/AClass.java"); 
    long t = source.lastModified();
    if (t&gt;lastSourceUpdate+2000) { // wait for hcr
        lastSourceUpdate=t;
        return true;
    }
    return false;
}
</code></pre>

<p>There are problems with this approach:</p>

<ol>
<li>Checking the file is unreliable, since compilation and hot code replace can finish a few seconds after the file changes. That's why there's a 2000ms delay above. Though the method returns true, the code I just altered isn't updated - or worse yet, Eclipse updates it halfway through the number-crunching, and the result is hopelessly scrambled.</li>
<li>Checking files is a hack in any case, it should check classes. The disk probably doesn't need to get involved.</li>
<li>It only checks one class, but I sometimes want to check a whole package, or failing that, any changes to the project at all. When a file changes, the package directory's lastModified is not changed. A recursive scan of the folders/packages would work, but isn't very elegant if the package is huge.</li>
<li>Looks ugly.</li>
</ol>

<p>So, what is the best way to check for when code changes? Perhaps reflection? A serialVersionUID check? It's not like classes themselves have a compilationDate field - or do they? Is there some secret value that Eclipse updates? Is there a file that Eclipse changes with every save?</p>

<p>Thanks for checking this out.</p>
