0

I have implements spot bugs in my project and it is continuously giving me an error for following code -filed DATE.

What I need to do is exclude generating getters and setters for the createTime and UpdateTime fields. Because I am going to clone the date object.

@Getter
@Setter
@MappedSuperclass
public class BaseErrorMessageDto implements Serializable {
    @Getter(AccessLevel.PRIVATE)
    private Date createTime;
    @Getter(AccessLevel.PRIVATE)
    private Date updateTime;

    public Date getCreateTime() {
        return (Date) createTime.clone();
    }

    public Date getUpdateTime() {
        return (Date) updateTime.clone();
    }
}

Giving The following error

May expose internal representation by incorporating reference to mutable object This code stores a reference to an externally mutable object into the internal representation of the object.  If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Storing a copy of the object is better approach in many situations.

What I need to do is exclude generating getters and setters for the createTime and UpdateTime fields.

K Rajitha
  • 306
  • 7
  • 24
  • What is your question here? Seems you have already written your own getters that clone date objects. – Thiyagu Feb 05 '20 at 05:00
  • I need to prevent autogenerating getter and setter from Lombok. – K Rajitha Feb 05 '20 at 05:04
  • If you provide your implementation, I believe it will take precedence over what it generates. Are you still getting the spotbugs warning? – Thiyagu Feb 05 '20 at 05:14
  • Thank you for your support. @user7 I have implemented both getters and setters manually and clone the Date object as the following answer. – K Rajitha Feb 05 '20 at 05:27
  • You can use `@Getter(AccessLevel.PRIVATE)`, but you don't have to, writing your own getter suppresses the generation. There's still a setter having about the same problem. **The only good solution is to replace the obsolete `Date` non-sense by `java.time.Instant` or another such class.** It's much better, trust me. – maaartinus Feb 05 '20 at 10:57

2 Answers2

1

I think it show error because you set Access Level private in field. try to not set access level at cloning time it give problem.

  • Yes, that access level also not necessary here. I have removed access levels and implement getter and setter manually and clone the date object then everything works fine. My manually defined getters and setters going to override the Lombok autogenerated getters and setters. – K Rajitha Feb 05 '20 at 06:15
0

I have manually added getters and also setters and clone the object as follows. Then Spot bug warning disappears.

public Date getCreateTime() {
    return (Date) createTime.clone();
}

public Date getUpdateTime() {
    return (Date) updateTime.clone();
}

public void setCreateTime(Date createTime) {
    this.createTime = (Date) createTime.clone();
}

public void setUpdateTime(Date updateTime) {
    this.updateTime = (Date) updateTime.clone();
}
K Rajitha
  • 306
  • 7
  • 24