How can I easily fix Checkstyle errors? - java

Is there a way to have an IDE fix Checkstyle errors automatically without having to fix each manually?

If you are using IntelliJ, you can install the CheckStyle IDEA plugin.
Then import the CheckStyle settings into the editor, and Reformat like normal (Ctrl+Alt+L is default shortcut key).

If you're using Eclipse, yes. You can't correct all problems though. There are two ways:
Right click on the java file in Package Explorer or whatever, and select 'Apply Checkstyle Corrections'.
Click on the error in the problems view, and select 'Quick fix'. This corrects the problem.

Your question is rather vague, but I think what you're asking is if CheckStyle can automatically reformat code that it's checking to fix code layout problems that it finds.
The direct answer here is "no."
However, there are a number of "Java source code formatters" (google that, choose one) that will do what I believe you're asking for.

There is a way to do this every time you save your file but not all warnings in Checkstyle can be fixed automatically.
You can have Checkstyle generate a formatter for you by right clicking on your package in the Package/Project explorer and selecting Checkstyle->Create Formatter-Profile. This will create a new formatter called eclipse-cs [your-package-name] based off of your Checkstyle configuration. You'll find it in the "Active profile" drop-down in the formatter preferences of Eclipse.
You can then set the formatter to run each save by going to Preferences->[Language of Choice]->Editor->Save Actions. Check the "Perform the selected actions on save" and "Format source code" boxes.

I created a feature request for the Eclipse checkstyle plugin to also support quick fixes from within the code editor:
https://sourceforge.net/p/eclipse-cs/feature-requests/150/

I used jalopy. Create Tool to fix the Check style issues.

Related

Importing a project into Eclipse and deploy it in your local

I have a project in Accurev for which I created a workspace. I did File > Import > Existing Projects into Workspace. After the import, the first thing that I have done is Configure Build path (Project>properties>Java Build Path). Still, I can find a lot of errors. I'm new to Java and I'm searching the Java errors in google and fixing them. Please suggest, How can I solve the similar errors for multiple files.
For Example,
The Empty block should be documented (I have it in 3 files)
The parameter customerExpression should not be assigned ( Multiple files )
The import java.util.List is never used ( Multiple files ). There are some other imports which were used inside the company are never used.
Apart from Java Problems, I do have
Classpath Dependency Validator Message
EJB Validator Message
HTML, JSP Problems etc which I feel can be solved once I solve Java Problems. I was told that Java problems are important. COrrect me if I'm wrong.
Please suggest.
Thanks
Like #Carlos Heuberger said these are seems like warnings not errors they won't cause any problem for your project to run. People usually don't care that much of warnings.
If you want to get rid of those warnings eclipse have quick fix actions for some of them. To do that head one of the warnings and right click then select quick fix. This should pop out a screen which summarize the action that eclipse will take.
Or you can simply hide them.
If you want to hide all the validation warnings go Window>Preferences>Validation then click Disable All button. This will disable all validation messages in your workspace.
For java warnings go through Windows>Preferences>Java>Compiler>Errors/Warnings and change the warning options according to your preferences. In my workspace under the Code Style section Undocumented empty block is setted to ignore so i don't get that warning in my workspace unlike yours.
Hope that helps.

Eclipse → AndroidStudio: Where can I see my errors?

I'm trying to migrate from Eclipse to Android Studio. There are a few things that bug me, but one stands out:
Q1: Is there no way to do automatic builds and have a list of problems displayed immediately like in Eclipse?
I know, there are many refactoring tools, but my often-used practise is to just insert a character into the name of a method and variable and check problems-view to see where it's being used.
Q2: How can replace or replicate this workflow in Android Studio?
One thing you can do is deactivate the external build. To do so click on "compiler settings icon" in the "Messages Make" panel that appears when you have an error. You can also open the compiler settings by going to File -> Settings -> Compiler. (Thanx to #maxgalbu for this tip).
enter image description here
Uncheck "Use External build"
enter image description here
And you will see the errors in the console
EDIT: After returning to "internal build" again you may get some errors, you can solve them this way: Android Studio: disabling "External build" to display error output create duplicate class errors

Eclipse kepler - Disable javascript validation

I just installed the new eclipse kepler (eclipse 4.3). It shows me hundreds of errors and warnings from third party javascript libraries.
I tried to disable the javasscript validation completely by unchecking "Enable JavaScript semantic validation" in "Preferences/JavaScript/Validator", but with no effect - Errors and warnings are still shown.
Is there another option to disable them or is this just a bug?
Can other people confirm this issue or is it just me? :)
Edit 1: Lavie Tobey pointed out, i should set all validations to "ignore" -> did not help
Edit 2:
Jim Garrison pointed out, i should disable the builder for the projects -> Actually I don't have a javascript builder in there. Disabled for testing purposes all of them, but did not help:
You can go Project properties/Javascript/Include Path/source and exclude the third party components.
There are two places you have to look. You have found the first. The second is in each project's Properties dialog, accessed by right-clicking on the project in Package Explorer and selecting "Properties". There, under Builders you may find a "Javascript Validation" entry, which you must disable.
You have to do this for every project that contains a Javascript Validation builder.
Try this: Open the .project in notepad and remove the javascript validator
I've found that in previous versions of Eclipse, the only way to really get these to go away is to enable it and change every case to "Ignore".
You can switch off (and configure) validation in Preferences/Validation or (for each project) Properties/Validation. However, this doesn't seem to force a cleanup of the messages.
Like it was said : be sure to go to PROJECT properties (right click on project, properties), and not Workspace (Eclipse/preference).
I searched like 20 min before hitting myself ;)
I solved this by choosing a PHP profile for my javascript instead.
Maybe you don't get the same functionality but for me this profile is just fine. Crashes, hangings & frustration went away.
I had the same issue for XML Validation. I deactivated the validators globally and in the projects itself, nothing worked for me. There wasn't even the slightest hint that anything changed. I even uninstalled XML Editor and Validation Plugins without any effect.
In the end the problem was solved in copy-pasting the XML files with a validation error, deleting the old files and renaming the copies files to the original ones. Somehow Eclipse did not re-validate the old files, but the new ones were okay.

How do I modify Eclipse code formatting?

When I reformat code with Eclipse, it turns method headers like this:
public void myMethod(String param) {
into method headers like this:
public void myMethod(
String param) {
When I was brought on here I'd never used Eclipse before, and I imported project settings provided by someone else. I have seen that on small new projects I've worked on Eclipse does not do this, so it must be in the settings I've imported. But I've gone through every panel I can find, as well as every hidden file I can find in the workspace, and I can't figure out what is causing this.
How do I turn it off? I don't want a newline before parameters in my method signatures, and I can't imagine why anyone would!
Have a look at Window>Preferences>Java>Code Style>Formatter.
There you can configure almost everything. Your case is found at
Line Wrapping>Method Declarations>Parameters.
In my version of Eclipse, I found the option under Window->Preferences->Java->Code Style->Formatter, then clicked the Edit.. button.
In the new window, go to the Line Wrapping tab and find Method Declarations. You want to change the Line wrapping policy. The Force split check box seems to do the same.
Go into preferences: Java -> Code Style -> Formatter
Restore Defaults or edit what's there.
Additionally, Code Style -> Code Templates will allow you to define generate code formatting.
Note that this is for Eclipse Workspace as a whole, the same Preferences can be accessed under the project preferences if you want to get more fine grained at a per project level.
In eclipse you can define your code formatting use Code Style ,
for MAC System :--> Eclipse --> Preference -- > java--> code Style
for Window System :--> Window->Preferences->Java->Code Style->Formatter
The best way to change your format is to go to Window-> Preferences-> Java-> Code Style-> Formatter. Inside the formatter window, eclipse has a couple of built in styles, but the best way is create a new profile of your own by pressing on New. You can change the format as you please on the edit tab
You can change parentheses and curly braces positioning along with other stuff.

Eclipse/Java code completion not working

I've downloaded, unzipped and setup Eclipse 3.4.2 with some plugins (noteable, EPIC, Clearcase, QuantumDB, MisterQ).
Now I find when I'm editing Java projects the code completion is not working. If I type String. and press ctrl+space a popup shows "No Default Proposals" and the status bar at the bottom shows "No completions available".
Any ideas?
Try restoring the default options in 'Windows > Preferences > Java > Editor > Content Assist > Advanced'
An example of the kind of data you see in this preference screen, however not necessarily what you currently have.
(From Vadim in this blog post " Content Assist Duplicates in Eclipse (Mylyn)":
if have duplicate Mylyn entries, uncheck the duplicate entries that do not contain "(Mylyn)" in their name)
The Eclipse help page defines the default list to restore:
Select the proposal kinds contained in the 'default' content assist list:
Other Java Proposals,
SWT Template Proposals,
Template Proposals,
Type Proposals
I'm adding an answer here in case someone else finds this on Google. Same symptoms; different problem. For me, the type caches had become corrupt.
From http://mschrag.blogspot.co.nz/2009/01/open-type-cant-find-your-class.html
Quit Eclipse
Go to workspace/.metadata/.plugins/org.eclipse.jdt.core
Remove *.index and savedIndexNames.txt
Restart Eclipse and search Ctrl+T for the offending type. The indexes will be rebuilt.
In case someone comes here and want to activate the autocomplete function, go to
Preferences -> Java -> Editor -> Content Assist.
Then in the Auto Activation section fill in Auto activation triggers for Java:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._
For those running Xfce + having IBus plugin activated, there might be keyboard shortcut conflict.
See more info on my blog: http://peter-butkovic.blogspot.de/2013/05/keyboard-shortcut-ctrlspace-caught-in.html
UPDATE:
as suggested by #nhahtdh's comment, adding the some more info to answer directly: IBus plugin in Xfce uses by default Ctrl+Space shortcut for keyboard layout switching. To change it, go to: Options and change it to whatever else you prefer.
Check the lib of your project. It may be that you have include two such jar files in which same class is available or say one class in code can be refrenced in two jar files. In such case also eclipse stops assisting code as it is totally confused.
Better way to check this is go to the file where assist is not working and comment all imports there, than add imports one by one and check at each import if code-assist is working or not.You can easily find the class with duplicate refrences.
Another solution which worked for me is to go to Java--> Appearence --> Type Filters and do disable all
None of these worked for me.
I was experiencing this issue in only once particular class. What finally worked for me was to delete the offending class and recreate it. Problem solved... mystery not so much!
If you have installed Google Toolbar for IE, may be you can face the same problem. Because, the toolbar capture the shortcut ctrl+Space.
I had this problem and like #Marc, only on a particular class. I discovered that I needed to designate Open With = Java Editor. As a Eclipse newbie I hadn't even realized that I was just using a plain text editor.
In the package explorer, right-click the file and chose "Open With".
I faced this problem, and spent hours trying to figure out the issue. tried to follow the steps mentioned in the different answers above, the solution I found is on the same lines as Mona suggested, but slightly different. Tried to add as a comment to Mona's answer but no option was available.
Issue with my eclipse was, classpath somehow got corrupted and all the jars and dependent projects were missing. after taking the latest .classpath from repository it worked fine.
Check that you did not filter out many options inside the Window > Preferences > Java > Appearance > Type Filters
Items in this list will not be appear in quick fix, be autocompleted, or appear in other various places like the Open Type dialog.
I also face this issue but it is resolved in different way.
Steps that I follow may be helpful for others.
Right click on project (the one you are working on)
Go to Properties > Java Build Path > JRE System Library
Click Edit... on the right
Choose the JRE 7
Once you have you configuration checked and completion is still not working:
make sure you have the right directory structure.
Do you see the right icon beside the file?:
It will tell you how the file will be treated by Eclipse:
I am posting this answer as I had that story with with Maven webapp artifact. By default Maven-WebApp does not create folder for sources and I put my Java into resources, wondering for 5 minutes what was going on... :)
Running STS on Java Spring Boot projects, here's what works for me :
Maybe this helps other people who come across the same issue.
My setup: old Gradle project (version Gradle 2.12) made by someone else, imported using the Gradle Import Wizard into STS (Eclipse Oxygen.2 (4.7.2)).
Code completion did not work either (and I still have hollow Js at the Java files), but at least I got the code completion to work by doing:
right click on the project folder > Properties > Gradle > Configure Workspace Settings > Java > Editor > Content Assist > Advanced
check "Java Proposals in upper window.
2x Apply & Close
I have run into this problem since upgrading to Eclipse 2019-09. Based on some of the suggestions above, this is what worked for me.
I had to go to Eclipse -> Preferences -> Java -> Editor -> Content Assist -> Advanced.
I found out that if I turn on any of the key binding proposals, Java Non-Type, Java, Java (Task-Focused) or Java Type proposal, then I was able to use auto complete. If I turned them all on, then not only did auto complete work, but I got duplicate methods listed. I am guessing, but I will probably used Java Type Proposals. Any clarification of what differs for these four types would be appreciated.
In my case, Intellisense had only disappeared in a few classes in one project. It turned out this was because of a missing library on the build path (although it worked previously).
So definitely check all the errors or problems in Eclipse and try to find if a library may be missing
For those who use the latest 3-19 eclipse build:
It just happened to me when upgrading from Oxygen to 3-19 eclipse version, so I assume the auto-complete feature does not migrated correctly during the upgrade process.
The only solution that worked for me was to create a new eclipse workspace, and import the project/s to it. It might take a few minutes, but it worth it - comparing to the time spent on other solutions...
I ran into this and it ended up being I was opening the file with the text editor and not the java editor.
For me the issue was a conflict between several versions of the same library. The Eclipse assist was using an older version than maven.
I had to go to the .m2 directory and delete the unwanted lib version + restart eclipse.
I experience problems on Eclipse Neon when editing a file which does not belong to the project directory. When I copy the same file to the project root directory, not even to the src directory, the completion starts working.
When the file is opened from a different directory, only completion for JRE works. That is for example: java. completes, but junit. does not.
Just in case anyone got to a desperate point where nothing works... It happened to us that the content assist somehow shrunk so no suggestion was shown, just the "Press Ctrl+Space for non-Java..." could be seen.
So, it was just a matter of dragging the corner of the content assist to enlarge the pop-up.
I know, embarrassing. Hope it helps.
Note: this was an Ubuntu server with Xfce4 using Eclipse Oxygen.
If you're experiencing this in an enum, or when initializing an array with anonymous classes, it's a known bug in Eclipse. See Eclipse content assist not working in enum constant parameter list.
We can change the settings as per our requirement.
Suppose we want to make java proposal as highest priority we need to do changes as shown below.
Windows > Preferences > Java > Editor > Content Assist > Advanced
Choose Java proposal and click on up button
For me in Sep 2021 it was an odd Eclipse bug. I had a multi-line string inside an annotation in my Class. This caused just that particular class to fail when trying to code complete (even though the class compiled just fine).

Categories