Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug type EI_EXPOSE_REP false positive for enums #2356

Closed
icegood opened this issue Feb 20, 2023 · 3 comments · Fixed by #2514
Closed

Bug type EI_EXPOSE_REP false positive for enums #2356

icegood opened this issue Feb 20, 2023 · 3 comments · Fixed by #2514
Assignees

Comments

@icegood
Copy link

icegood commented Feb 20, 2023

Hi all. Have the next code:

fragment 1

import android.os.Parcelable;
public enum FavoriteOperationType implements Parcelable {

fragment 2

private final FavoriteOperationType mType;

fragment 3

public FavoriteOperationType getType() {
        return mType;
}

obtained
Bug type EI_EXPOSE_REP FavoriteParameters.getType() may expose internal representation by returning FavoriteParameters.mType

on ver. 4.7.3

In java enums are supposed to be immutable. Please, fix

@welcome
Copy link

welcome bot commented Feb 20, 2023

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

@icegood
Copy link
Author

icegood commented Feb 20, 2023

clarification: for 'pure enum' bug doesn't occur, however for 'custom' enums for example implement android.Parcelable it occurs. Full code:

package ++++;

import android.os.Parcel;
import android.os.Parcelable;

public enum B implements Parcelable {
    B1,
    B2,
    B3;

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(toInteger());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public int toInteger() {
        return this.ordinal();
    }

    public static B fromInteger(int value) {
        return values()[value];
    }
}

and consumer:

package ++++;

import androidx.annotation.NonNull;

public class A {
    private B mB;

    public A(@NonNull B b) {
        this.mB = b;
    }

    B getmB() {
        return mB;
    }

}

maisonobe added a commit to Hipparchus-Math/hipparchus that referenced this issue Apr 24, 2023
These false positives correspond to regular object oriented design.
SpotBugs has just become too stringent (see
spotbugs/spotbugs#1601,
       spotbugs/spotbugs#1771,
spotbugs/spotbugs#2083,
       spotbugs/spotbugs#2344,
spotbugs/spotbugs#2356,
       spotbugs/spotbugs-gradle-plugin#731)
@gtoison
Copy link
Contributor

gtoison commented Aug 9, 2023

This is the same issue as #1797 the class is deemed mutable because it has a method named "writeXYZ". Spotbugs is a bit too eager to flag mutable classes

gtoison added a commit to gtoison/spotbugs that referenced this issue Aug 15, 2023
Enums and Records might be recognised as mutable when they have methods
that might be setters. For instance write() or delete() are assumed to
be setters

Fixes spotbugs#2356
hazendaz pushed a commit that referenced this issue Aug 15, 2023
Enums and Records might be recognised as mutable when they have methods
that might be setters. For instance write() or delete() are assumed to
be setters

Fixes #2356
@hazendaz hazendaz self-assigned this Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants