0

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:

  1. Create a Java project with the modified TestClass.java class shown above.
  2. Compile the project and save the TestClass.class file somewhere else
  3. Remove the static field from TestClass.java and clean the project
  4. Replace the TestClass.class file with the modified version
  5. Add the Main.java class to your project
  6. You should see the error only in the editor but not on the java file
  7. Run the Main class - should work fine without any errors
Balder
  • 8,623
  • 4
  • 39
  • 61
  • I believe the JDT editor always works from the the .java source file when it is available so you are going to struggle to get this to work. – greg-449 Jan 22 '15 at 13:19
  • There is a [standardized annotation processing procedure](http://openjdk.java.net/groups/compiler/doc/compilation-overview/) that works compiler independent. I suggest having a look at it. See also [`Filer.createClassFile`](http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Filer.html#createClassFile(java.lang.CharSequence,%20javax.lang.model.element.Element...)). – Holger Feb 20 '15 at 17:14

0 Answers0