SonarQube (Sonar) + EclipseLink: incorrect error 'Comparison of String parameter using == or !='

StackOverflow https://stackoverflow.com/questions/22530570

  •  18-06-2023
  •  | 
  •  

質問

I have the following class, using EclipseLink JPA:

package my.package;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Version;

import my.package.api.Address;

@Entity(name = "Address")
@SequenceGenerator(name = "sequence", sequenceName = "seq_address")
public class AddressJpaEntity implements Address {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
    private Long id;
    @Version
    private Long version;
    private String street;

    public AddressJpaEntity() {
    }

    public AddressJpaEntity(String street) {
        this.street = street;
    }

    @Override
    public Long getId() {
        return id;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public String getStreet() {
        return street;
    }

    @Override
    public void setStreet(String street) {
        this.street = street;
    }

}

When I do a SonarQube-run, I get a lot of the following (incorrect) errors:

Bad practice - Comparison of String parameter using == or !=

This code compares a java.lang.String parameter for reference equality using the == or != operators. Requiring callers to pass only String constants or interned strings to a method is unnecessarily fragile, and rarely leads to measurable performance gains. Consider using the equals(Object) method instead.

findbugs | ES_COMPARING_PARAMETER_STRING_WITH_EQ Comparison of String parameter using == or != in my.package.AddressJpaEntity._persistence_set(String, Object)

For now, I solved it by setting the issues as false positive, but we will add more similar classes in the future and I don't want to do this each time.

How can I make Sonar not mark these errors, without using 'False positive' all the time?

役に立ちましたか?

解決

Please check this post for a solution. It shows how to create an exclusion filter for FindBugs.

In this case, you'll want to ignore ES_COMPARING_PARAMETER_STRING_WITH_EQ warnings.

I still think it's incorrect to use ==, though, but apparently, that's not something you can help.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top