Home  >  Article  >  In Java, how to copy an object value to a new object and change the data without affecting another reference variable

In Java, how to copy an object value to a new object and change the data without affecting another reference variable

PHPz
PHPzforward
2024-02-06 10:42:091082browse
Question content

I need to print the request body with masked data in the console. So I want to separate the request object that is masked for printing to the console from the request object that is used for business without masking.

So I cloned the request object (underwriterequest request) to a new object (underwriterequest requestmasking) and then used the method to mask the data.

But when I set the shielding ID to new object, the reference object (underwriterrequest request) also has shielding.

I don't want to block (underwriterequest request) because the object must be used in business logic.

Why does it affect the reference object? How can I mask data without affecting the reference object?

Thanks.

public @ResponseBody ResponseEntity<UnderwriteResponse> execute(@RequestBody UnderwriteRequest request) {

        UnderwriteRequest requestMasking = maskingData(request);
}






public static UnderwriteRequest maskingData(UnderwriteRequest request) throws CloneNotSupportedException {
    UnderwriteRequest requestMasking = (UnderwriteRequest) request.clone();

    try {
            
            if(request != null) {

                if(request.getRequestBody().getPersonalData().getIdCard() != null && !request.getRequestBody().getPersonalData().getIdCard().isEmpty()) {
            
                        maskIdCard(requestMasking, request);
                
        }
        }catch (Exception e) {
             log.info(e);
    }

    return requestMasking;
}





public static void maskIdCard(UnderwriteRequest requestMasking, UnderwriteRequest request) {
            String maskChar = "X";
            String maskString = StringUtils.repeat( maskChar, 9);
            String idcard = request.getRequestBody().getPersonalData().getIdCard();
                    requestMasking.getRequestBody().getPersonalData().setIdCard(StringUtils.overlay(idcard, maskString, 0, 9));

}



@Getter
@Setter
public class UnderwriteRequest implements Cloneable{


    private RequestHeader requestHeader;
    private RequestBrmsBody requestBrmsBody;
    private RequestBody requestBody;

    private RESPONSE_STATUS status = RESPONSE_STATUS.FAILED;
    private String message;
    
    @Override
    public Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
    
    

    
}

I use the clone() method to copy the object value to a new object. Because I think when I change the data in the new object, it won't affect the reference object. https://www.geeksforgeeks.org/clone-method-in-java-2/


Correct Answer


It seems that you are using SpringBoot framework. You can take a look at the BeanUtils.copyProperties(); method. This method assigns the same properties to the first and second objects via reflection. There won’t be the problem you mentioned

The above is the detailed content of In Java, how to copy an object value to a new object and change the data without affecting another reference variable. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete