Home > Java > javaTutorial > Detailed explanation of examples of java object copying

Detailed explanation of examples of java object copying

Y2J
Release: 2017-04-26 10:03:14
Original
2120 people have browsed it

This article is a detailed analysis and introduction to java object replication. Friends who need it can refer to it

Java itself provides the ability to copy objects. There is a clone method in the java.lang.Object class. This method is a protected method. Subclasses need to override this method and declare it as a public type. They also need to implement the Cloneable interface to provide the ability to copy objects. clone() is a native method. Generally speaking, the efficiency of native methods is It is much higher than the non-native method in Java. If you are more concerned about performance, consider this method first. There are many examples of this kind of copying on the Internet, so I won’t go into detail here. Another method to be used here is through Java. The reflection mechanism copies objects. This method may be less efficient than clone(), and does not support deep copying and copying collection types, but its versatility will be much improved. The following is the code for copying:

The code is as follows:

private <T> T getBean(T TargetBean, T SourceBean) {        if (TargetBean== null) return null;        Field[] tFields = TargetBean.getClass().getDeclaredFields();        Field[] sFields = SourceBean.getClass().getDeclaredFields();        try {            for (Field field : tFields ) {                String fieldName = field.getName();                if (fieldName.equals("serialVersionUID")) continue;                if (field.getType() == Map.class) continue;
                if (field.getType() == Set.class) continue;
                if (field.getType() == List.class) continue;                for (Field sField : sFields) {                    if(!sField .getName().equals(fieldName)){                        continue;                    }                    Class type = field.getType();                    String setName = getSetMethodName(fieldName);                    Method tMethod = TargetBean.getClass().getMethod(setName, new Class[]{type});                    String getName = getGetMethodName(fieldName);                    Method sMethod = SourceBean.getClass().getMethod(getName, null);                    Object setterValue = voMethod.invoke(SourceBean, null);                    tMethod.invoke(TargetBean, new Object[]{setterValue});                }            }        } catch (Exception e) {            throw new Exception("设置参数信息发生异常", e);        }        return TargetBean;}
Copy after login

This method receives two parameters, one is the source object to be copied - the object to be copied, and the other is the target object to be copied - Object copy. Of course, this method can also be used between two different objects. At this time, as long as the target object and the object have one or more attributes of the same type and name, the attribute values ​​of the source object will be assigned to the attributes of the target object. .

The above is the detailed content of Detailed explanation of examples of java object copying. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template