Can one specify Java warning settings per project in Eclipse? - java

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".

Related

How to make Eclipse package ONLY required libraries?

After developing a java app in Eclipse, I would like to deploy it by packaging it into a runnable .jar with only the minimum necessary by the main method and its dependencies.
I have several packages in my workspace I work with too, but do not need to be in the resulting .jar file.
When I try to export, it clearly says that the required libs will be there, but also the other independent packages are inside too. (With the Export option happen exactly the same)
I choose to export only the Main class of the com.project... package, but also the test.project... has been packaged.
In the project I have both com... and test... packages obviously.
How could I force it to truly package only the required ones?
Thanks in advance.
TEMPORARY ANSWER (2019-07-03):
Seems that, for now, there is no way to achieve this automatically, thus the answer from #arnonuem seems a good workaround.
If better news, please feel free to improve this thread.
Thank you all.
I would create an ANT file for this specific task. There you can freely customize which packages should be compiled into the jar and which not.
Please inspire yourself reading this example.
For a general overview what i am talking about you could take a quick look into this.
https://howtodoinjava.com/ant/ant-build-jar-file-example/
Please focus on
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"
includes="src/path/to/TheClassToBeIncluded.java" />
For more detailed information on how to include or exclude files or packages you should refer to the documentation over here:
https://ant.apache.org/manual/Tasks/javac.html
At the bottom of this page there is a list with valid build parameters.
First of all, we have to distinguish build and export.
Build transfers .java source code into .class byte code and mostly copies other resources. While doing that it usually merges all source folders into a single bin folder.
Export runs a build (or relies on Build Automatically) and then modifies the resulting .class files and other resources. Usually it packages them into one or more .jar files.
Therefore, our solution includes two steps:
build everything that is unwanted for export into a separate folder (or more)
export from a specific folder (or more)
For step 1, refer to this answer on Stack Overflow.
In a nutshell: Go to Project Properties > Java Build Path > Source tab > Allow output folder for source folders. This enables you to configure a specific output folder for each source folder in the centered viewer.
For step 2, we need to understand that eclipse's Runnable Jar File Export relies on a Launch Configuration.
So before exporting, go to Run > Run Configurations..., select the Classpath tab, remove the default User Entries and hit Advanced.... Now you can Add Folders containing your built classes.
You might want to use separate Run Configurations for internal testing and exporting.
I have struggled with this problem on and off for years, supposing it was just me who was failing to find the right solution. Possible solutions always seem to involve detailed manual configuration e.g. configuration of the build path, or selection of the folders from which classes are exported into a jar, or learning ant, but which still requires manual configuration. The problem is that the inter-dependencies between classes (and packages) are complex - imagine drawing a network diagram from import statements. Manual configuration is time-consuming, error-prone and, I think, infeasible except in simple cases. I am a bit stunned. If there is no automatic solution for selecting necessary classes, I suppose people are regularly exporting their entire code base and that the world is full of bloated jars ... (and, incidentally, without obfuscation, the entire source code base is thereby made available through reverse engineering).

auto-generated java files in Eclipse

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.

Could someone explain the what .settings folder of an eclipse java project contain?

So whenever someone creates a project in Eclipse it creates the following folder
.settings
-org.eclipse.jdt.core
( Its something related to eclipse jdt but what exactly is it? Does it specify the kind of compiler that should be used? )
The .settings directory contains – or at least should contain – vital information needed to successfully build your project inside Eclipse, such as the character encoding used for source code, Java compiler settings etc
The .settings folder is used by various plugins to set persistent 'Properties' as opposed to 'Preferences' to specify project specific settings that should be preserved.
The settings folder just contains a preferences file which describes the preferences of your project.
example : for a project in my workspace, the file org.eclipse.jdt.core.prefs it contains :
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
All project specific settings and what is enabled, what is not etc.
Edit :
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
From eclipse docs -When enabled in conjunction with a Java target platform lesser than or equal to "1.4", the compiler will no longer generate JSR instructions, but rather inline corresponding subroutine code sequences (mostly corresponding to try finally blocks). The generated code will thus get bigger, but will load faster on virtual machines since the verification process is then much simpler. This mode is adding support for the Java Specification Request 202 to pre-"1.5" Java target platforms.
For a Java target platform greater than or equal to "1.5", the inlining of the JSR bytecode instruction is mandatory and this option is ignored.

FindBugs command line: how to specify the project to be analyzed?

I tried to run FindBugs in command line and had troubles when specifying the project to be analyzed. I understand FindBugs works on bytecode (.jar, .class), so I wrote a HelloWorld program and made sure that it had some messy code that would be detected by FindBugs.
Then I tried:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin
which threw an exception:
java.lang.IllegalArgumentException: Can't read project from HelloWorld/bin
at edu.umd.cs.findbugs.Project.readProject(Project.java:774)
I also tried .class and .jar files, but nothing showed up:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin/Main.class
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.jar
I checked the FindBugs manual about the command line option "-project", it says
The project file you specify should be one that was created using the GUI interface. It will typically end in the extension .fb or .fbp
I don't understand this. Does it mean that some pre-processing is required and FindBugs cannot check arbitrary .jar or .class or project directly? How can I get this .fb or .fbp extension?
Thanks.
The procedure is described on the FindBugs website:
Make sure you download the FindBugs distribution which includes the GUI (called Swing interface).
Extract your downloaded ZIP and add its bin folder to your PATH.
Type findbugs to open the GUI, then click New Project
In the dialog:
Enter a project name, say HelloWorld.
Where it says Classpath for analysis, give it the Jar with your .class files or a directory where the .class files are (such as build/classes/main or whatever; the package structure must start in this directory).
Where it says Auxiliary classpath, list any libraries required to load your classes.
Source directories works just like Classpath for analysis, but for .java files. FindBugs uses this to show you where in the code your issues are.
You can select (cloud disabled) as bug store.
Click Analyze.
Now you can save the project configuration as a .fbp project file.
Next time, you can start the analysis by running
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.fbp
If you don't want to or cannot use the GUI, you can get the text-only version by adding the -textui option as first option after findbugs.jar. Output formats and behavior are configured via additional command line options.
However, most people use FindBugs integrated with their IDEs, or as part of a build process. Neither use case should require the GUI or command line versions. Take a look at the plugins for your IDE, it may save you a lot of time and they are really easy to use.

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