I am working on an Eclipse plugin that changes the compiled class files of a Java project based on some Annotations in these class files.
For example lets say I have an annotation @AddSomeField
and I have a Java source file like this:
@AddSomeField
public class TestClass {}
Then my plugin might produce the byte code corresponding to the following source code
public class TestClass {public static int SOME_FIELD = 0;}
and replace the class file generated by JDT with the corresponding class file. This is done with the help of ASM in a separate build process each time the JDT build has been finished (the plugin provides an Eclipse builder).
Now, all of this works fine but I encountered the following problem: The JDT Editor is not able to see the added field and shows an SOME_FIELD cannot be resolved or is not a field
error when I try to reference the field like e.g.:
public class Main{
public static void main(String[] args) {
System.out.println(TestClass.SOME_FIELD);
}
}
This error is only reported in the JDT Editor but not by the compiler. Therefore no errors are shown on the source file itself and also running the above main method works fine without any errors.
Is there any way to make the JDT editor see these changed class files or does there exist any other workaround to change class files after the compilation?
If you want to reproduce the problem yourself, you can do the following:
- Create a Java project with the modified
TestClass.java
class shown above. - Compile the project and save the
TestClass.class
file somewhere else - Remove the static field from
TestClass.java
and clean the project - Replace the
TestClass.class
file with the modified version - Add the
Main.java
class to your project - You should see the error only in the editor but not on the java file
- Run the
Main
class - should work fine without any errors