-1

I have the following program which adds a method to itself when run. But I have to refresh it every time using the F5 button or the refresh option. Is there a way I could code the refresh in the program itself so that it refreshes itself after the modification? The project I am working on is a Java application and not an eclipse plugin so as far as I know the refreshLocal() method can't be used.

public class Demo {
public static void main(String[] args) throws IOException, CoreException {
    File file = new File("/home/kishan/workspace/Roast/src/Demo.java");

    if (file.exists()) {
        JavaClassSource javaClass = Roaster.parse(JavaClassSource.class,
                file);
        javaClass.addMethod().setPublic().setStatic(true)
                .setName("newMethod").setReturnTypeVoid()
                .setBody("System.out.println(\"newMethod created\");")
                .addParameter("String[]", "stringArray");
        FileWriter writer = new FileWriter(file);
        writer.write(javaClass.toString());
        writer.flush();
        writer.close();

    }

}
}

I have tried using the refreshLocal() method defined in the eclipse JDT but since my project is a Java application the ResourcePlugin.getWorkspace() method does not work giving me a "workspace closed" error.
Any suggestion is appreciated.

Kishan Kishore
  • 451
  • 6
  • 12

2 Answers2

0

You see, eclipse runs your Java class within its own dedicated JVM. Thus there is no direct programmatic way of enforcing a refresh within eclipse.

You could check this older question; maybe that could lead to a reasonable workarounds.

On the other hand you might step back and ask yourself why exactly you want to achieve that. Your workflow simply doesn't make much sense when looking at it; as in: when generating code that way, shouldn't that generated code better go in its own specific place?

If you intend to "generate" code frequently to then continue to use it in eclipse; well, that somehow smells like a strange idea.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I am trying to mimic the auto generate equals() and hashCode() option provided by eclipse. – Kishan Kishore Sep 30 '16 at 09:55
  • Also, I have already read the answer you pointed me to but it does not involve any code to refresh the file. There are some relevant questions but they don't quite work for me: http://stackoverflow.com/questions/5467902/how-to-refresh-eclipse-workspace-programatically http://stackoverflow.com/questions/11569910/how-to-call-eclipse-f5-refresh-programmatically-after-page-closes – Kishan Kishore Sep 30 '16 at 09:59
0

Eclipse has "Refresh using native hooks or polling" which might might help. You can find it under Window > Prefrences > General > Workspace.
See On Eclipse, what does "Preferences -> General -> Workspace -> Refresh using native hooks or polling" do?

Community
  • 1
  • 1
c0der
  • 18,467
  • 6
  • 33
  • 65