RSS Feed Subscribe to RSS Feed

 

Getting rid of IntelliJ warning: Local variable is redundant

When dealing with a piece of code like this

    String id = "abc";
    return id;

IntelliJ may give a warning message:

    Local variable 'id' is redundant

I can see IntelliJ’s point. The code could be rewritten as follows:

    return "abc";

However, sometimes it can be useful to use a variable name as a form of documentation.
Also, and more often, I don’t want to see this warning pop up for other people’s code! If they wish to use a “redundant” variable, who am I to argue? I certainly don’t want to be warned about it every time I do a commit.

To disable this warning, deselect the following:

Preferences -> Editor -> Inspections (-> Java) -> Data flow issues -> Redundant local variable -> Ignore immediately returned or thrown variables

Or you can setup custom handling for tests (e.g. weak warnings) under Severity by Scope.
(Based on IntelliJ 15)

Tags:

Running tests in IntelliJ for a multi module maven project

This post shows how to run all the tests in IntelliJ for a multi module maven project.

(more…)

Tags: , , ,

Getting rid of IntelliJ warning: Value ‘yourVariable’ is always ‘null’

IntelliJ was giving me a warning message like this:

Value ‘yourVariable’ is always ‘null’

I often set values to be null for tests (e.g. checking a method can deal with a null parameter), so I wanted to disable this warning.

To do so, deselect the following:

Preferences -> Editor -> Inspections -> Probable bugs -> Constant conditions and exceptions -> Warn when reading a value guaranteed to be constant.

Or you can setup custom handling for tests (e.g. weak warnings) under Severity by Scope.

(Based on IntelliJ 14)

Tags:

Keyboard shortcuts in IntelliJ

I attended a talk by Hadi Hariri at JavaOne last week. He introduced a whole bunch of IntelliJ keyboard shortcuts I was not aware of. Very useful talk. I have listed some of the most useful ones below. Or see the official reference card.

(more…)

Tags: ,

Live Templates in IntelliJ

As described here, IntelliJ’s live templates let you easily insert predefined code fragments into your source code.
I have posted some of my most used templates below, a link to my complete list of template files on GitHub (as a reference for myself when I setup new IntelliJ environments) and the steps I took to add the IntelliJ settings file to GitHub.
(more…)

Tags: , , , , , ,

How to add a project to GitHub

Although the GitHub docs contains good info on how to add an existing GitHub project to your local machine, how to add an existing (unversioned) project from your local machine to GitHub was a little less clear to me. Here are the steps I use.

(more…)

Tags: , , , ,

Using Java8 with Maven in IntelliJ (invalid target release: 1.8)

I recently started trying out the early access version of Java8. When I tried using it with maven in IntelliJ however, I got the following error when I ran the maven install command:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xyz:

Fatal error compiling: invalid target release: 1.8\

Although I saw some suggestions on stackoverflow here about hardcoded JAVA_HOME paths, the solution was just changing the maven specific version of Java that IntelliJ uses. This can be done via:

File > Settings > Project Settings > Maven > Runner

and selecting the 1.8 version of the JRE.

Tags: , , , ,

Add log4j logging for a unit test in IntelliJ

In IntelliJ

  • Click: Run -> Edit Configuration
  • Select your test configuration (or add a new one)
  • In VM parameters, add
    -Dlog4j.configuration=file:/C:/dev/config/log4j.xml
    (Or wherever your log4j properties file is)

That’s it. The test should now run with log4j logging (although obviously you need to have the necessary log4j jars available).
This example uses a windows file format, but will work equally well with *nix.

I use this approach for turning on MyBatis logging for individual tests. See here for a basic setup of a log4j.xml file to enable logging with MyBatis.

Tags: , , , ,