auto-generated java files in Eclipse

1.8k views Asked by At

How to understand which are the auto-generated files in a huge Java project developed in Eclipse?

I am a newbie. For example, little by little, I am discovering that many files are generated by using xcore plug-in. Other with xtend. Just going through the code and trying I am learning and that's ok. The question is: is there a way to understand which files to modify to automatically regenerate the others? My error was to start modifying all the files manually.

1

There are 1 answers

0
kapex On BEST ANSWER

Usually you set up different source directories for your code and generated code.

For example, a project using Xtend and EMF would have following source directories in its build path:

  • src contains all Java and Xtend files that you write
  • xtend-gen contains generated Java files created by Xtend
  • src-gen contains generated Java files created by EMF

In this setup you should only edit files in src. Files in xtend-gen will be updated automatically if you edit Xtend files in src. Files in src-gen will be updated if you regenerate the model.

I'm not that familiar with Xcore, but since it is based on EMF I think you just have to set the "model directory" property of the genmodel.

This should make the distinction between your code and generated code more clear. You may still feel the need to modify generated code sometimes. EMF actually supports this by adding special annotations in the generated file but I would not recommend this, because it's very hard to see if a file has been modified this way. If you really need to change the generated behavior, the first approach described in this article about properly overriding generated EMF code is better. Basically you extend and override methods of some generated classes and the factory and then use Eclipse extension points to replace the generated factory with your extended one.


If you are looking at a project of someone else and don't know which code generating tools are used:

You should try to ask the authors if possible or check if there is any documentation about building the project. Otherwise I guess you'll need to analyze the project structure to see which plugins are used to generate code. This might be a bit hard if you don't already know which plugins actually can generate code though.

  • Check the Eclipse "Project Nature" to see which plugins are used to build
  • Check build configuration (Ant, Maven, Gradle) for plugins that might generate code
  • Look for special files (xcore, genmodel, etc) and figure out to which
  • Check if there are any Annotation Processors configured and check if they create any files
  • (Xtend only) Check if there are Active Annotation and check if they create any files
  • Check if @Generated annotations is used in the code, which is used to mark generated files

If you have Identified all tools, then try to change the model destination directory and regenerate the code. Then compare the generated files with your original code - all duplicated files are likely generated.