Jason Rich Darmawan
Jason Rich Darmawan

Reputation: 2081

How to avoid 'Private field is never assigned' which is caused by using MyBatis and Immutable POJO?

my goal is to avoid problem 'Private field is never assigned' without using @SupressWarnings or creating a defined constructor.

I am aware using annotation will lead to technical debt for the long run. However, I can't not justify the Java verbosity (although I love it at times when debugging a bug), this code is easier to read.

Method that I do not wish to use:

  1. SupressWarnings("unused") written above the class statement.
  2. Creating a defined constructor which is not necessary since MyBatis can modify the object attribute regardless there is a setters or not for example when you use @SelectKey.
  3. Creating a setter which will never be used.

This is the sample code for the model I am going to standardize for MyBatis.

model/NameModel.java

package com.example.mssqlserver.model;

import com.fasterxml.jackson.annotation.JsonInclude;

@SuppressWarnings("unused") // MyBatis does not need a defined constructor nor a setters.
@JsonInclude(JsonInclude.Include.NON_NULL) // filter: only non_null, alternative: spring.jackson.default-property-inclusion=NON_NULL in application.properties
public class NameModel {
  private Integer id;
  private String name;
  private String newid;

  public Integer getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public String getNewid() {
    return newid;
  }

  public boolean requestIsValid() {
    return !this.name.isEmpty();
  }
}

Upvotes: 0

Views: 3922

Answers (3)

Guillem Chesa
Guillem Chesa

Reputation: 61

This is normally an IDE warning, you can change the settings from the IDE to not receive this kind of warnings without tweaking your code. I don't know what IDE are you using but I think it might be IntelliJ. In that case, you can do the following:

Go to File > Settings > Editor > Inspections > Java > Declaration Redundancy > Unused declaration. And there, you can choose which elements you want to get this warning, on which privacy scope and you can even change the type of warning to a soft warning or just disable it.

In other IDEs there might be a similar option.

Upvotes: 1

Jati Srono
Jati Srono

Reputation: 21

the first is like this

public class MainActivity extends AppCompatActivity {
    private PDFView pdfView;

    public MainActivity(PDFView pdfView) {
        this.pdfView = pdfView;
    }
}

and I edit like this

public class MainActivity extends AppCompatActivity {
    private final PDFView pdfView;

    public MainActivity(PDFView pdfView) {
        this.pdfView = pdfView;
    }
}

and the problem solved. thanks

Upvotes: 1

Doron Cohen
Doron Cohen

Reputation: 61

javac will not complain on this.

javac -cp *.jar com/example/mssqlserver/model/NameModel.java

So it is likely be the IDE being used. It may have a configuration Errors/Warnings option for this specific case. Unfortunately couldn't find such an option in my Eclipse IDE - though I remember there was one - so cannot point the exact option.

A related question: Why no "field is never assigned" warning with @Mock

Upvotes: 0

Related Questions