akshattandon / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

@EqualsAndHashCode have null checks for final @NonNull fields. #786

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please discuss feature requests first on
https://groups.google.com/forum/#!forum/project-lombok

What steps will reproduce the problem?  In the following class, the hashCode 
and equals methods generate null checks on the final @NonNull field.

@EqualsAndHashCode
@RequiredArgsConstructor
public class Immutable<T> {
    @NonNull
    private final T field;
}

What is the expected output? What do you see instead?

The generated hashCode and equals methods have null checks when accessing the 
final @NonNull field.  I expect no null checks for accessing the final field.  
Findbugs generates dodgy code warnings for these methods.

What version of the product are you using? On what operating system?
1.14, Linux

Original issue reported on code.google.com by c...@honton.org on 13 Feb 2015 at 6:47

GoogleCodeExporter commented 9 years ago
This is not only a problem for final variables, see the example below.
The superfluous null checks are negatively influencing our branching code 
coverage numbers calculated by CKJM, so a fix would be appreciated!

@EqualsAndHashCode
@Data
public class bla {
    @Getter
    @NonNull
    private String field;
}

Original comment by mike.ros...@gmail.com on 16 Mar 2015 at 1:53

GoogleCodeExporter commented 9 years ago
Duplicate of 681.

There's no guarantee that `field` is not null as you're free to add your own 
constructor or setter or use reflection or whatever to change it. So the null 
check is necessary.

The only meaningful thing is to make your tool honor some kind of `@Generated` 
annotation. See also issue 702.

Original comment by Maaarti...@gmail.com on 24 Mar 2015 at 4:52

GoogleCodeExporter commented 9 years ago
Thanks Maaartinus

Original comment by r.spilker on 24 Mar 2015 at 8:58