Findbugs中的BUG:May expose internal representation by returning reference to mutable object 引发问题说明

在使用IDEA的findbugs的插件检测model层类的时候发生如下错误:

May expose internal representation by returning reference to mutable object
Returning a reference to a mutable object value stored in one of the object's fields exposes 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. Returning a new copy of the object is better approach in many situations.

这里提示的BUG的测试结果如下:

实体类BusGeologicalDrilling部分代码:

public class BusGeologicalDrilling{
 private Date enddt;

// 此处省略构造函数


//下面为IDE自动为我们生成的get和set方法

 public Date getEnddt() {
        return   enddt  ;
    }

 public void setEnddt(Date enddt) {
        this.enddt =   enddt  ;
    }
}

部分测试代码如下:

 BusGeologicalDrilling busGeologicalDrilling=new BusGeologicalDrilling();
 Date date=new Date();
 busGeologicalDrilling.setEnddt(date);
 System.out.println(busGeologicalDrilling.getEnddt().toString());
 date.setYear(5);
 System.out.println(busGeologicalDrilling.getEnddt().toString());

测试结果如下:

通过结果对比发现:

日期的对象在经过改变后,原本之前赋予实体的enddt属性的值也跟着被修改掉了,这样显然是不对的,这里涉及到引用传递值的问题。此处不再深究。

解决方案:

一:修改实体的属性的get和set方法如下:

public class BusGeologicalDrilling{
 private Date enddt;

// 此处省略构造函数


//下面为IDE自动为我们生成的get和set方法

 public Date getEnddt() {
        return (Date)enddt.clone();
    }

 public void setEnddt(Date enddt) {
        this.enddt = (Date)enddt.clone();
    }
}

备注:这种方式虽然可以解决引用值传递的问题,但是当在属性上使用【com.fasterxml.jackson.annotation包下面的JsonFormat】如:注解控制Date的格式或者时区,如果当前属性绑定的是非格式或者空值会发生绑定错误,所以此种方法并不完美,如下:

 @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
 private Date enddt;

二、使用官方推荐的Date获取方式为实体进行赋值,测试如下:

上面测试我们看到是没有任何问题的,原因在于Calendar类和Date的实体获取机制不同导致。

 

 

 

在 JSX ,`ctx.expose`通常用于将组件的某些方法或属性暴露给其子组件。这意味着子组件可以通过`props`接收到父组件的方法或属性,并在需要时调用它们。这样做可以实现组件之间的通信和数据共享。 例如,假设我们有一个父组件`Parent`和一个子组件`Child`,我们想在`Child`组件调用`Parent`组件的方法`handleClick`。我们可以在`Parent`组件使用`ctx.expose`将`handleClick`方法暴露给`Child`组件,如下所示: ```jsx import { createContext } from 'react'; const MyContext = createContext(); function Parent() { const handleClick = () => { console.log('Clicked'); }; return ( <MyContext.Provider value={{ handleClick }}> <Child /> </MyContext.Provider> ); } function Child() { return ( <MyContext.Consumer> {({ handleClick }) => ( <button onClick={handleClick}>Click me</button> )} </MyContext.Consumer> ); } ``` 在上面的示例,我们首先使用`createContext`创建了一个名为`MyContext`的上下文对象。然后,在`Parent`组件,我们定义了一个名为`handleClick`的方法,并使用`MyContext.Provider`将其作为值传递给`MyContext`上下文对象。最后,我们在`Child`组件使用`MyContext.Consumer`订阅`MyContext`上下文对象,并通过解构获取了`handleClick`方法,并将其作为`button`的`onClick`事件处理程序传递。这样,当我们在`Child`组件单击`button`时,`handleClick`方法将在`Parent`组件被调用,并输出`Clicked`到控制台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值