# Immutable public static fields are not properly detected

**URL:** <https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [October 26, 2021, 10:18am UTC](https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092 "2021-10-26T10:18:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fbassi](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/fbassi/32/26846_2.png) [@fbassi](https://community.sonarsource.com/u/fbassi)\
**Post date:** [October 26, 2021, 10:18am UTC](https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092/1 "2021-10-26T10:18:20Z")

</div>

SonarQube 9.1

The detection of the immutability of classes fails on some classes (in particular collections)  
and java:S2386: Mutable fields should not be “public static” is improperly triggered.

example:

```auto
import java.util.AbstractList;
import java.util.List;

import javax.annotation.concurrent.Immutable;

public class Test {
	private Test() { }

	// ERROR -> This immutable static final fields triggers:
	// java:S2386: Mutable fields should not be "public static"
	public static final List<Object> WRONG_IMMUTABLE_FIELD = new ImmutableList<>();

	// OK -> This immutable field is properly detected and the rule java:S2386 is not triggered
	public static final List<Object> OK_IMMUTABLE_FIELD = List.of("P1");

	// OK -> This immutable field is properly detected and the rule java:S2386 is not triggered
	public static final OtherImmutableClass OK_IMMUTABLE_FIELD2 = new OtherImmutableClass();

	@Immutable
	public static class ImmutableList<T> extends AbstractList<T> {
		@Override public T get(int index) { return null; }
		@Override public int size() { return 0; }

		@Override
		public String toString() { return "This class is NOT properly detected as immutable"; }
	}

	public static class OtherImmutableClass {
		@Override
		public String toString() { return "This class is properly detected as immutable"; }
	}
}

```

---

<div class="post-metadata">

**Author:** ![alban.auzeill](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/alban.auzeill/32/36_2.png) [@alban.auzeill](https://community.sonarsource.com/u/alban.auzeill)\
**Post date:** [November 8, 2021, 4:00pm UTC](https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092/3 "2021-11-08T16:00:00Z")

</div>

Hi @fbassi,  
I created the [SONARJAVA-4071](https://jira.sonarsource.com/browse/SONARJAVA-4071) ticket to support @Immutable annotation.  
Thanks for the feedback,  
Alban

---

<div class="post-metadata">

**Author:** ![fbassi](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/fbassi/32/26846_2.png) [@fbassi](https://community.sonarsource.com/u/fbassi)\
**Post date:** [November 8, 2021, 4:16pm UTC](https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092/5 "2021-11-08T16:16:05Z")

</div>

Thanks a lot for the quick answer!  
– fb

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [November 15, 2021, 4:16pm UTC](https://community.sonarsource.com/t/immutable-public-static-fields-are-not-properly-detected/52092/6 "2021-11-15T16:16:15Z")

</div>

Closed on November 15, 2021, 4:16pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
