1

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.

I'm using this thing, which I call a few times per second:

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>lastSourceUpdate+2000) { // wait for hcr
        lastSourceUpdate=t;
        return true;
    }
    return false;
}

There are problems with this approach:

  1. 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.
  2. Checking files is a hack in any case, it should check classes. The disk probably doesn't need to get involved.
  3. 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.
  4. Looks ugly.

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?

Thanks for checking this out.

mk.
  • 11,360
  • 6
  • 40
  • 54
  • 1
    Are you coding this eclipse functionality? OR using eclipse and want to get rid of this? If the later you can disable build on save and build it your self ( click on build icon ) when the time comes. – OscarRyz Jun 17 '11 at 19:14
  • 2
    Maybe you can implement this as an Eclipse Builder which fires after all other builds have done their jobs? You'll get a full changeset what changed so you don't have to rely on expensive file checking. – MRalwasser Jun 17 '11 at 19:17
  • This isn't an eclipse-specific issue. I want the java program to know when its code has been changed, which can occur via Eclipse debug's hot-code replacement, or other means. @MR can an Eclipse Builder easily interact with my program? – mk. Jun 18 '11 at 01:49

1 Answers1

6

Instead of comparing last modified dates, try comparing MD5 hashes of the file.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • This won't work at all. Modification dates are exactly as reliable as MD5 hashes. It would suffer from exactly the same problems - the file would be updated before the hot code replace occurs. It's even worse, because you have to read the whole file to check. – mk. Jun 18 '11 at 01:43