auto-generated java files in Eclipse - java

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.

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.

Related

Eclipse class resolution for automatically generated classes in classes folder

We are using a custom "eclipse builder" that generates output class files in the project classes folder (Those are some automatic utility classes).
We are not able to tell eclipse to take these automatically generated classes into consideration to help with our coding. The classes remain invisible to eclipse.
We also tried to manually edit the .classpath file. Even this did not work (only when we renamed the classes folder to something else).
It sounds like this is a scenario that is not uncommon: have custom builders generate some extra code/classes in the classes directory, and then include this into the normal coding process. Btw: in our case there is also a normal build step (outside of eclipse) where the automatic classes are generated with no problems here.
Why eclipse does not see those classes? How can we tell eclipse to include this in its class resolution?
Update: Further context and information
Goal: How to use auto-generated classes (bytecode) without generating source-code (in eclipse)
Description: We are trying to use classes for coding, which do not exist as sources, but whose bytecode is auto-generated (similar to how Lombok generates new methods).
This didn't work:
We use a custom builder that runs before the java builder, and let it write the .class files to the default output folder. But the java builder doesn't consider these classes at all and complains that the referenced class cannot be found.
Apparently the default output folder for java builder (typically "classes") cannot be at the same time input for the java builder. Thus, the solution is to have another folder like "classes_generated", and mark this a library input for the java builder. This can also be exported to be visible to other projects.
Thus, the "problem" is simply that the default output folder for the java builder cannot be used as input for the java builder. Maybe even for a good reason? But, there are workarounds for this.

How to save java source code in an xml file, and edit/compile it in an IDE?

We have an java application in which the user can write/execute their own java code and use imports from compiled jars - i.e. they write it, and it is compiled and run by the application. They can also save this code (along with various other information that they are using) - currently this is saved to a human-readable xml file.
I want to be able to use those save xml files in an IDE (principally, Intellij), so that if the user changes things in their compiled jar in the IDE, these changes can also be picked up in the save xml file.
For example, if a save file used a class from the compiled jar, it may have the following import:
import com.company.project.package.subpackage.MyClass;
Let's say that class was moved, so the import was:
import com.company.project.package.subpackage.subsub.MyClass;
...this would change all the save xml files that used that class and import - just as the IDE would for all the other usages in the compiled project.
(This, and other examples, arises because the compiled jar is both constantly in development and in use using the aforementioned application.)
At the moment, if I were to add the save xml files to a sub-project in the IDE, the user can edit the save files manually, possibly taking advantage of 'find/replace' or 'search for usage in text' functions. This is better than nothing, but still a rather involved/complicated process. Also, there is no checking that the code in the save files are consistent with the code in the compiled project.
One approach that I am considering is a script or a test class that would unpack the save xml file, writing the java code to java files, and then try and compile (and possibly running/testing) those java files.
A further step would be to write a maven plugin (we use maven for our build cycle) or an ant script (ant still has its uses...) to do this, and possibly make this part of our build process - i.e. you cannot compile the project without ensuring all of the save xml files in its sub-project also compile.
Does this seem like a reasonable approach?
Are there alternative approaches that anyone could suggest?
..saving as a .java file is not the solution I'm looking for.
Save it as a Zip with 1 (or more) XML files as well as any source (in paths according to package) that is required. You could even include other files easily, a manifest, help files etc.
This has a number of advantages:
It allows source & include files to be a different encoding to the XML
It consolidates all the necessary parts of the project into one file, without any 'jumping though hoops' to make one format fit inside another.
It allows different compression levels as appropriate to the data (e.g. text/XML compresses well, whereas a serialized image does not).

Surpress Time Generated Comment when Autogenerating Java Classes from Hibernate

I am using Eclipse and JBoss Tools to generate Java classes from existing Hibernate mappings. Whenever the classes get generated, they have a comment at the top of the class that indicates when the class was auto-generated.
I really want to prevent this from happening since it is a nuisance when these classes are under configuration management. I've looked through all the settings I can think of and online and haven't found out how to do this yet. It seems like it should be very simple, but it hasn't been.
Anyone know how to suppress this comment from being created?
You can edit the tempalte files in the jar file of jboss tools so it does not generate that info. As far as I can remember it uses velocity templates, so you just need to delete that bit from the template files and it wont generate those comments.
When I was using eclipse to generated the POJO's I had the same problem (when working with GIT reporitory). I build a simple program in .Net in order to clean the comments. After I run the hibernate configuration, I run the .exe (it must be located in the same folder of the POJO's files)
You can find the code and an .exe app in https://github.com/jaimeimarin/HibernateHeadersCleaner . If you are working with git remember to add an exception in order to commit the tool.
#Add this line to your .gitignore file
!HibernateHeadersCleaner.exe

Can one specify Java warning settings per project in Eclipse?

I have a Java project which is a combination of human-written Java code and Java code generated by axis2.
The axis2 generated code provokes thousands of warnings from the Java compiler (either javac or the one built into Eclipse). Examples of warnings: dead code, use of raw list and array types predating Java generics, etc. (more at http://www.coderanch.com/t/501752/Web-Services/java/Axis-Generate-without-Warnings). I'd like to silence and ignore these specific warnings in the generated code, but not the human-written code.
I've seen How to add -Xlint:unchecked option or any javac option in Eclipse? and that allows me to disable the relevant warnings via Window->Preferences, but workspace-wide, which is not what I want. Is there a way to do this on a per-project basis?
If not, how do people deal with generated code without ignoring warnings that would be useful to humans?
You can enable lots of different options on a per-project basis. Assuming you're using Helios, here's how to configure warnings on a particular project:
Right-click the project (or select the project, then Alt+Enter)
Java Compiler → Errors/Warnings
Check "Enable project specific settings" (does the missing hyphen bug you? It bugs me)
Configure away!
The multiple projects approach suggested by Matt is the right solution (since you can remove all warnings for a given project)
To try to do so within the same project has been:
ignored in 2003 (bug 46391)
in discussion (with a proposed path) since 2009 (bug 220928)
So far, there isn't a way to "ignore warnings from certain source folders".
This doesn't directly address your question. But the approach we've taken to dealing with warnings in generated code is not to store the generated code in the filesystem. Instead, we store the generated code in a jar which can be included in the service package. The basic process is as follows:
Create a temporary directory and run wsdl2java to generate code, placing its output in the temporary directory.
Compile the code into a set of class files.
Store the compiled classes and the resource files into a jar. Store the source code in a zip.
Remove the temporary directory.
Once you've done this, add the jar to your classpath, then edit your build configuration and add the zipfile as a "source attachment". Eclipse will show you the correct source when you navigate to one of the generated classes, but it won't parse the code for warnings. The jar with the generated code in it can be added to your packaged service; with an AAR package, it goes in a subdirectory called "lib".

Issue with classpath in Java project

I'm currently working on a Java project which uses Tibco rendezvous control.
I have the tibrvj.jar file on my build path. But I'm not able to see the classes which are there in the package "com.tibco.tibrv".
This package is in the jar which is on my build path.
When I try to open the class in that package , the message displayed on class editor is : "Source not found. the jar file tibrvj.jar has no source attachment".
Is there anything that could be done to set it right ?
This looks like an IDE specific message. Which IDE are you using?
The jar in your classpath only contains the java classes (bytecode) and not the source files (.java files). It looks like you are trying to open the class in your IDE/editor. Since you do not have the java files corrsponding to these classes, the editor is comlaining.
As these are Tibco specific classes, the only option is to "decompile" the classes to see the corresponding java code. Consider using a decompiler like DJ Java Decompiler
It depends on what you mean by "I'm not able to see the classes." If you mean you're getting build failures, that's possibly a matter of not actually having it properly in your build path (how are you building?) or maybe not having the right imports.
If you mean you can't see the source code, you'll need to get hold of it somehow (is Tibco open source?) and then configure your IDE so that it knows where the source is. Of course you don't need to source just to be able to build your app which uses Tibco.
If you could give more information about exactly which problem you're facing, along with what IDE you're using (if any) we could probably help more.

Categories