Returning 'data' may exposed internal array?

前端 未结 2 1310
甜味超标
甜味超标 2021-01-24 15:39

Consider this

public class Data {

    private final SomeField[] fields;
    .....

    public SomeField[] getFields() {
        return map == null ? null : map.         


        
2条回答
  •  天涯浪人
    2021-01-24 16:43

    To solve this problem you must avoid to user ternary operator. Instead of this, you must use if operator.

    Example:

    public CustomMap[] getMap() { CustomMap[] obj = null;

    if (map != null){
       obj = map.clone();
    }
    return obj;
    

    }

    OR

    public CustomMap[] getMap() {
        CustomMap[] obj = map == null ? null : map.close();
        return obj;
    }
    

    I solve my problem using the abouve code. I think that is mandatory to create a new object explicit. I think.

    Regards!

提交回复
热议问题