2

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?

user969039
  • 511
  • 2
  • 7
  • 18
  • You didn't ask a question. What answer are you looking for? The error you show does not occur in the snippet you pasted. If you are indeed comparing strings with == or !=, you really do have a problem, as FindBugs points out. Change that to `string1.equals(string2)` instead. – mrjink Apr 07 '14 at 14:16
  • I have added a real question I pasted all the code needed. This code will generate the sonar-blocker => the real problem The only '='-sign I use are only for assignment. Most of them are used in the annotations of JPA. I don't know why Sonar thinks this is a comparison. – user969039 Apr 08 '14 at 11:47
  • The error is in `my.package.AddressJpaEntity._persistence_set(String, Object)` (as pointed out by FindBugs). That part is not in your question. Comparing strings with == or != should never be regarded as a false positive, because it actually is incorrect to do that. – mrjink Apr 08 '14 at 12:31
  • The _persistence_set is generated code from EclipseLink. You can see it in the compiled class-file. Therefore, I don't have control on this method. – user969039 Apr 10 '14 at 06:30
  • I guess [this](http://stackoverflow.com/q/20124985/2020834) should be of interest, then. It shows how to create an exclusion filter for FindBugs. It still think it's incorrect to use ==, though, but apparently, that's not something you can help. – mrjink Apr 10 '14 at 06:51
  • 1
    Okay. This worked! Thank you. You may write it done as an answer, so I can close this question. For further reference: I have created a findBugsExclusions.xml-file, uploaded it and referred to it via settings-->Java-->FindBugs-->"Excludes Filters" – user969039 Apr 16 '14 at 09:02

1 Answers1

0

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.

Community
  • 1
  • 1
mrjink
  • 1,131
  • 1
  • 17
  • 28