Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 413882

Summary: [hashcode/equals] @Nullable final field leads to compilation error in auto-generated hashcode and equals
Product: [Eclipse Project] JDT Reporter: Benoit Lacelle <benoit.lacelle>
Component: UIAssignee: Stephan Herrmann <stephan.herrmann>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: srikanth_sankaran, victor.noel
Version: 4.3   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug

Description Benoit Lacelle CLA 2013-07-28 16:01:13 EDT
I consider the following class with a nonnull final field and a nullable final field:

public final class URLWrapper {
	@Nonnull
	public final String url;
	@Nullable
	public final String userAgent;

	public URLWrapper(@Nonnull String url, @Nullable String userAgent) {
		this.url = url;
		this.userAgent = userAgent;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		result = prime * result + ((userAgent == null) ? 0 : userAgent.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		URLWrapper other = (URLWrapper) obj;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		if (userAgent == null) {
			if (other.userAgent != null)
				return false;
		} else if (!userAgent.equals(other.userAgent))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "URLWrapper [url=" + url + ", userAgent=" + userAgent + "]";
	}

}

The issue is the hashcode and equals method has been generated by eclipse and it leads to a compilation error "Potential null pointer access: The field userAgent is declared as @Nullable ..."

I guess the null analysis should consider the autogenerated hashcode and equals as valid code.

Thanks
Comment 1 Srikanth Sankaran CLA 2013-07-29 01:11:28 EDT
Stephan, thanks for following up.
Comment 2 Victor Noël CLA 2015-12-21 04:38:41 EST
The problem is even that since the fields are final, once their non-nullness has been tested, they shouldn't be considered potentially null!
Comment 3 Stephan Herrmann CLA 2015-12-22 13:24:45 EST
(In reply to Victor Noël from comment #2)
> The problem is even that since the fields are final, once their non-nullness
> has been tested, they shouldn't be considered potentially null!

In the current implementation 'final' is deliberately not considered by null analysis. For further reading cf. bug 484570 comment 3.
Comment 4 Stephan Herrmann CLA 2015-12-22 13:33:13 EST
Regarding the original request:

(1) warnings about redundant null checks are correct for @NonNull fields. We might, however, consider not generating these checks for @NonNull fields.

(2) warnings regarding potential null pointer access result from (deliberate) lack of flow analysis for fields. Even when enabling syntactic null analysis for fields, the same warnings are raised, which could be fixed by trivial code modifications (invert "?:", change order of if;s).

Moving to JDT/UI to consider adjusting the code patterns when fields have null annotations.
Comment 5 Eclipse Genie CLA 2020-03-04 05:26:29 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.