Implementation of clone method for Java Record

Implement the clone method in Record which is the 2nd preview in Java 15 so that you can make a deep copy. I don't think there is a particular scene to use this, but I may use it someday, so I'll use it as a memorandum. The point is to make a clone in the constructor without using super.clone ().

environment

Java 15(Oracle Open JDK) IntelliJ IDEA Community Edition Windows

Implementation

Before implementing clone

ExampleRecord.java

public record ExampleRecord(String name, int age, ExampleDto dto) {
}

ExampleDto.java

public class ExampleDto {
  private String name;
  private int age;
  private NodeDto node;
  // setter, getter,toString omitted
}

NodeDto.java

public class NodeDto {
  private String name;
  // setter, getter,toString omitted
}

After implementing clone

ExampleRecord.java

public record ExampleRecord(String name, int age, ExampleDto dto) {
  @Override
  public ExampleRecord clone() {
    try {
      return new ExampleRecord(this.name, this.age, this.dto.clone());
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }
    return null;
  }
}

ExampleDto.java

public class ExampleDto implements Cloneable {
  private String name;
  private int age;
  private NodeDto node;
  // setter, getter,toString omitted

  @Override
  public ExampleDto clone() throws CloneNotSupportedException {
    ExampleDto copy = (ExampleDto) super.clone();
    copy.node = this.node.clone();
    return copy;
  }
}

NodeDto.java

public class NodeDto implements Cloneable {
  private String name;
  // setter, getter,toString omitted

  @Override
  public NodeDto clone() throws CloneNotSupportedException {
    return (NodeDto) super.clone();
  }
}

try

Implementation

Main.java


public class Main {
  public static void main(String[] args) {
    ExampleDto dto = new ExampleDto();
    NodeDto nodeDto = new NodeDto();
    nodeDto.setName("node1");
    dto.setNode(nodeDto);
    dto.setName("dto1");
    dto.setAge(1);
    ExampleRecord exampleRecord = new ExampleRecord("record1", 2, dto);
    System.out.println("exampleRecord:"+exampleRecord);// 1
    dto.setName("dto2");
    nodeDto.setName("node2");
    System.out.println("exampleRecord:"+exampleRecord);// 2

    ExampleRecord clone = exampleRecord.clone();
    System.out.println("clone:"+clone);// 3
    dto.setName("dto3");
    nodeDto.setName("node3");
    System.out.println("clone:"+clone);// 4
  }
}

Execution result

1.

exampleRecord:ExampleRecord[name=record1, age=2, dto=ExampleDto{name='dto1', age=1, node=NodeDto{name='node1'}}]

2.

exampleRecord:ExampleRecord[name=record1, age=2, dto=ExampleDto{name='dto2', age=1, node=NodeDto{name='node2'}}]

3.

clone:ExampleRecord[name=record1, age=2, dto=ExampleDto{name='dto2', age=1, node=NodeDto{name='node2'}}]

4.

clone:ExampleRecord[name=record1, age=2, dto=ExampleDto{name='dto2', age=1, node=NodeDto{name='node2'}}]

In 1-> 2, the contents of Dto have changed, but in 3-> 4, it is a different instance, so dto.setName ("dto3"); andnodeDto.setName ("node3"); You can see that it is unaffected and cloned without any problems.

Recommended Posts

Implementation of clone method for Java Record
[Java] Implementation of Faistel Network
[Java] Timer processing implementation method
[Java] Summary of for statements
Implementation of gzip in java
Benefits of Java static method
Implementation of tri-tree in Java
Summary of file reading method for each Java file format
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
[For beginners] Summary of java constructor
Generics of Kotlin for Java developers
Implementation of like function in Java
Java method
java (method)
[Details] Implementation of consumer applications with Kinesis Client Library for Java
Java method
[Java] method
[Java] method
Implementation of DBlayer in Java (RDB, MySQL)
Rails [For beginners] Implementation of comment function
List of download destinations for oracle java
Features of spring framework for java developers
Comparison of thread implementation methods in Java and lambda expression description method
[Java] Handling of JavaBeans in the method chain
Summary of Java environment settings for myself [mac]
Do you need a memory-aware implementation of Java?
[Java] Dynamic method call by reflection of enum (enum)
For JAVA learning (2018-03-16-01)
The order of Java method modifiers is fixed
Clone Java List.
Java8 method reference
[Java] forEach method
[JQuery] Implementation procedure of AutoComplete function [Java / Spring]
Method name of static factory method learned from Java 8
[Java / Spring Boot] Spring security ④ --Implementation of login process
2017 IDE for Java
Implementation of GKAccessPoint
[Java / Spring Boot] Spring security ⑤ --Implementation of logout processing
Pre-introduction notes for JavaScript experienced learners of Java
A collection of simple questions for Java beginners
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Method name of method chain in Java Builder + α
java8 method reference
[Java] Random method
[Introduction to Java] Basics of java arithmetic (for beginners)
[Java] Overview of Java
[Java] split method
Java for statement
Review and implementation of CSV library for loading large amounts of data into MySQL (Java)
[Java] Implementation method memo to set WS-Security Username Token in SOAP Stub of axis2
[Java] How to use compareTo method of Date class
[Read Effective Java] Chapter 3 Item 12 "Considering Implementation of Comparable"
Get a list of MBean information for Java applications
Implementation of asynchronous processing for single tenant in Tomcat
[For beginners] Quickly understand the basics of Java 8 Lambda
Initialization of for Try to make Java problem TypeScript 5-4
Implementation example of simple LISP processing system (Java version)
Introduction to Java for beginners Basic knowledge of Java language ①
List of frequently used Java instructions (for beginners and beginners)
Links for each version (Japanese version) of Java SE API
How to execute WebCamCapture sample of NyARToolkit for Java